Skip to content

Commit

Permalink
feat: inject view
Browse files Browse the repository at this point in the history
  • Loading branch information
nukc committed Apr 17, 2017
1 parent 595c23d commit 7356447
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

[中文](https://github.com/nukc/StateView/blob/master/README-zh.md)

StateView is a lightweight view that absorb characteristics of ViewStub. It can occupy less memory that the initia state is not visible and does not occupy the position of the layout. When the operation show empty / retry / loading view,
it will be added to the layout.
StateView is an invisible, zero-sized View that can be used to lazily inflate loadingView/emptyView/retryView at runtime.

<img src="https://raw.githubusercontent.com/nukc/stateview/master/art/custom.gif">

Expand All @@ -16,7 +15,7 @@ it will be added to the layout.
add the dependency to your build.gradle:

```groovy
compile 'com.github.nukc.stateview:library:1.1.0'
compile 'com.github.nukc.stateview:library:1.2.1'
```

##Usage
Expand All @@ -34,6 +33,7 @@ Can be directly used in java.
```

```java
// if view is not ViewGroup, StateView will be inject to view.getPatent()
mStateView = StateView.inject(View view);

mStateView = StateView.inject(View view, boolean hasActionBar);
Expand Down Expand Up @@ -101,7 +101,7 @@ setLoadingResource(@LayoutRes int loadingResource)

The MIT License (MIT)

Copyright (c) 2016 Nukc
Copyright (c) 2016, 2017 Nukc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ StateView 一个轻量级的控件, 继承自 `View`, 吸收了 `ViewStub` 的


```groovy
compile 'com.github.nukc.stateview:library:1.1.0'
compile 'com.github.nukc.stateview:library:1.2.1'
```

## 使用方法

直接在代码中使用:

- 注入到Activity
- 注入到 Activity
```java
mStateView = StateView.inject(Activity activity);
```

- 注入到ViewGroup
- 注入到 ViewGroup
```java
mStateView = StateView.inject(ViewGroup parent);

mStateView = StateView.inject(ViewGroup parent, boolean hasActionBar);
```

```java
// 如果 View 不是 ViewGroup,则会注入到 View 的 parent 中
mStateView = StateView.inject(View view);

mStateView = StateView.inject(View view, boolean hasActionBar);
Expand Down Expand Up @@ -96,6 +97,10 @@ setLoadingResource(@LayoutRes int loadingResource)

## ChangeLog

#### Version 1.2.1
修改 inject 方法,如果使用 ```StateView.inject(View view)``` 传入的 view 不是 ViewGroup,则会尝试注入到 view 的父容器中,
另外增加判断 ViewGroup 是 SwipeRefreshLayout/NestedScrollView 的情况。

#### Version 1.1.0
fix [issues #6](https://github.com/nukc/StateView/issues/6)

Expand Down Expand Up @@ -157,7 +162,7 @@ inject(ViewGroup parent),用于添加到ViewGroup中

The MIT License (MIT)

Copyright (c) 2016,2017 Nukc
Copyright (c) 2016, 2017 Nukc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile project(':library')
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:design:25.1.1'
compile 'com.android.support:support-v4:25.1.1'
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support:design:25.3.0'
compile 'com.android.support:support-v4:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
testCompile 'junit:junit:4.12'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
android:name=".FragmentActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity android:name=".CustomActivity" />
<activity android:name=".RefreshActivity" />
<activity android:name=".InjectViewActivity"></activity>
</application>

</manifest>
64 changes: 64 additions & 0 deletions app/src/main/java/com/github/nukc/sample/BaseActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.github.nukc.sample;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.github.nukc.stateview.StateView;

/**
* @author Nukc.
*/

public abstract class BaseActivity extends AppCompatActivity{

private StateView mStateView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(setContentView());
mStateView = StateView.inject(injectTarget());

mStateView.setOnRetryClickListener(new StateView.OnRetryClickListener() {
@Override
public void onRetryClick() {
//do something
}
});
}

protected abstract @LayoutRes int setContentView();

protected abstract View injectTarget();

@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_inject, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.show_empty:
mStateView.showEmpty();
break;
case R.id.show_retry:
mStateView.showRetry();
break;
case R.id.show_loading:
mStateView.showLoading();
break;
case R.id.show_content:
mStateView.showContent();
break;
}
return super.onOptionsItemSelected(item);
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/github/nukc/sample/InjectViewActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.nukc.sample;

import android.view.View;

public class InjectViewActivity extends BaseActivity {

@Override
protected int setContentView() {
return R.layout.activity_inject_view;
}

@Override
protected View injectTarget() {
return findViewById(R.id.text_view);
}

}
14 changes: 14 additions & 0 deletions app/src/main/java/com/github/nukc/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,19 @@ public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CustomActivity.class));
}
});

findViewById(R.id.btn_refresh_view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, RefreshActivity.class));
}
});

findViewById(R.id.btn_inject_view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, InjectViewActivity.class));
}
});
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/github/nukc/sample/RefreshActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.nukc.sample;

import android.view.View;

public class RefreshActivity extends BaseActivity {

@Override
protected int setContentView() {
return R.layout.activity_refresh;
}

@Override
protected View injectTarget() {
return findViewById(R.id.refresh_layout);
}
}
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="50dp">
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">

<Button
android:id="@+id/btnInEmpty"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_inject_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="inject view"
tools:context="com.github.nukc.sample.InjectViewActivity" />
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Custom View"/>

<Button
android:id="@+id/btn_refresh_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Inject Refresh View"/>

<Button
android:id="@+id/btn_inject_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Inject View"/>
</LinearLayout>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_refresh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.github.nukc.sample.RefreshActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inject SwipeRefreshLayout" />

</android.support.v4.widget.SwipeRefreshLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textAllCaps">false</item>
</style>

</resources>
2 changes: 1 addition & 1 deletion bintray.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def siteUrl = 'https://github.com/nukc/StateView' // 项目的主页
def gitUrl = 'https://github.com/nukc/StateView.git' // Git仓库的url

group = "com.github.nukc.stateview"
version = "1.1.0"
version = "1.2.1"

install {
repositories.mavenInstaller {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Aug 17 19:49:10 CST 2016
#Mon Apr 17 09:23:43 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 15
versionName "1.1.0"
versionCode 17
versionName "1.2.1"
}
buildTypes {
release {
Expand All @@ -24,7 +24,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:appcompat-v7:25.3.0'
}

apply from: '../bintray.gradle'
Loading

0 comments on commit 7356447

Please sign in to comment.