Skip to content

Commit

Permalink
添加ButterKnife
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed May 2, 2018
1 parent 3542440 commit 71147d3
Show file tree
Hide file tree
Showing 26 changed files with 153 additions and 119 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/build
/captures
.externalNativeBuild

fastadapter/publish.gradle
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation project(':library')
implementation 'com.jakewharton:butterknife:8.8.1'
implementation project(':fastadapter')

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/aizuzi/adapter/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.aizuzi.adapter.FastAdapter;
import com.zuzi.adapter.FastAdapter;
import com.aizuzi.adapter.demo.viewholder.EmptyHolder;
import com.aizuzi.adapter.demo.viewholder.ImageViewHolder;
import com.aizuzi.adapter.demo.viewholder.TextViewHolder;
Expand All @@ -28,7 +28,7 @@ protected void onCreate(Bundle savedInstanceState) {
private void setAdapter(){
FastAdapter fastAdapter = new FastAdapter(this);

for (int i = 0; i < 10; i++) {
for (int i = 0; i < 100; i++) {
fastAdapter.addItem(TextViewHolder.class,"Text ViewHolder:"+i);
fastAdapter.addItem(ImageViewHolder.class,"your image path");
fastAdapter.addItem(EmptyHolder.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.aizuzi.adapter.demo.viewholder;

import android.view.View;
import butterknife.ButterKnife;
import com.zuzi.adapter.FastViewHolder;

/**
* @author liyi
* create at 2018/4/28
**/
public abstract class BaseViewHolder<T> extends FastViewHolder<T> {

public BaseViewHolder(View itemView) {
super(itemView);
}

@Override protected void onCreate() {
super.onCreate();
ButterKnife.bind(this, itemView);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.aizuzi.adapter.demo.viewholder;

import com.aizuzi.adapter.FastBaseViewHolder;
import android.view.View;
import com.aizuzi.adapter.demo.R;
import com.zuzi.adapter.FastViewHolder;
import com.zuzi.adapter.RecyclerItemLayoutId;

/**
* @author liyi
* create at 2018/4/26
**/
public class EmptyHolder extends FastBaseViewHolder {
@RecyclerItemLayoutId(R.layout.item_empty)
public class EmptyHolder extends BaseViewHolder {

public EmptyHolder(View itemView) {
super(itemView);
}

@Override protected void onCreate() {
super.onCreate();
Expand All @@ -17,8 +23,4 @@ public class EmptyHolder extends FastBaseViewHolder {
@Override public void refreshItem(Object bean) {

}

@Override public int getLayoutId() {
return R.layout.item_empty;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.aizuzi.adapter.demo.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.aizuzi.adapter.FastBaseViewHolder;
import com.aizuzi.adapter.demo.R;
import com.zuzi.adapter.FastViewHolder;
import com.zuzi.adapter.RecyclerItemLayoutId;

/**
* @author liyi
* create at 2018/4/26
**/
public class ImageViewHolder extends FastBaseViewHolder<String> {
@RecyclerItemLayoutId(R.layout.item_image)
public class ImageViewHolder extends BaseViewHolder<String> {

private ImageView mImageView;

public ImageViewHolder(View itemView) {
super(itemView);
}

@Override protected void onCreate() {
super.onCreate();
mImageView = (ImageView) findViewById(R.id.image_view);
Expand All @@ -23,7 +29,4 @@ public class ImageViewHolder extends FastBaseViewHolder<String> {

}

@Override public int getLayoutId() {
return R.layout.item_image;
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package com.aizuzi.adapter.demo.viewholder;

import android.view.View;
import android.widget.TextView;
import com.aizuzi.adapter.FastBaseViewHolder;
import com.aizuzi.adapter.demo.R;
import com.zuzi.adapter.FastViewHolder;
import com.zuzi.adapter.RecyclerItemLayoutId;

/**
* @author liyi
* create at 2018/4/26
**/
public class TextViewHolder extends FastBaseViewHolder<String> {
@RecyclerItemLayoutId(R.layout.item_text)
public class TextViewHolder extends BaseViewHolder<String> {

private TextView mTextView;

public TextViewHolder(View itemView) {
super(itemView);
}

@Override protected void onCreate() {
super.onCreate();
mTextView = (TextView) findViewById(R.id.text_view);
mTextView = findViewById(R.id.text_view);
}

@Override public void refreshItem(String bean) {
mTextView.setText(bean);
}

@Override public int getLayoutId() {
return R.layout.item_text;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_empty.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_image.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content">

<TextView
android:id="@+id/text_view"
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0-beta2'



classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions library/build.gradle → fastadapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

//apply from: "publish.gradle"

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.aizuzi.adapter;
package com.zuzi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -26,23 +27,23 @@ public class FastAdapter

private LayoutInflater mLayoutInflater;

private HashMap<Class<? extends FastBaseViewHolder>,Integer> itemMaps = new HashMap<>();
private HashMap<Class<? extends FastViewHolder>, Integer> itemMaps = new HashMap<>();

public FastAdapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
}

public <TD extends FastBaseViewHolder<T>,T> void addItem(Class<TD> clazz) {
addItem(clazz,null);
public <TD extends FastViewHolder<T>, T> void addItem(Class<TD> clazz) {
addItem(clazz, null);
}

public <TD extends FastBaseViewHolder<T>,T> void addItem(Class<TD> clazz,T t) {
public <TD extends FastViewHolder<T>, T> void addItem(Class<TD> clazz, T t) {

Integer itemType = itemMaps.get(clazz);

if(itemType == null || itemType <=0){
if (itemType == null || itemType <= 0) {
itemType = obtainItemType();
itemMaps.put(clazz,itemType);
itemMaps.put(clazz, itemType);
}

FastItemBean fastItemBean = new FastItemBean();
Expand All @@ -54,20 +55,21 @@ public <TD extends FastBaseViewHolder<T>,T> void addItem(Class<TD> clazz,T t) {
notifyDataSetChanged();
}

private int obtainItemType(){
private int obtainItemType() {
int type = new Random().nextInt(999999);
if(itemMaps.containsValue(type)){
if (itemMaps.containsValue(type)) {
return obtainItemType();
}
return type;
}

private Class<? extends FastBaseViewHolder> getClassByType(int type){
private Class<? extends FastViewHolder> getClassByType(int type) {
//if(itemMaps.containsValue(type))return null;
Iterator<Map.Entry<Class<? extends FastBaseViewHolder>,Integer>> iterator = itemMaps.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<Class<? extends FastBaseViewHolder>,Integer> item = iterator.next();
if(item.getValue() == type){
Iterator<Map.Entry<Class<? extends FastViewHolder>, Integer>> iterator =
itemMaps.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Class<? extends FastViewHolder>, Integer> item = iterator.next();
if (item.getValue() == type) {
return item.getKey();
}
}
Expand All @@ -78,17 +80,32 @@ private Class<? extends FastBaseViewHolder> getClassByType(int type){

try {

FastBaseViewHolder fastBaseViewHolder = getClassByType(viewType).newInstance();
View itemView = mLayoutInflater.inflate(fastBaseViewHolder.getLayoutId(), null);
//FastViewHolder fastViewHolder = (FastViewHolder) ConstructorUtils.invokeConstructor(fastBaseViewHolder.get(), itemView);
FastViewHolder fastViewHolder = new FastViewHolder(itemView);
Class<? extends FastViewHolder> fastBaseViewHolderClass = getClassByType(viewType);

fastBaseViewHolder.setFastViewHolder(fastViewHolder);
fastViewHolder.setFastBaseViewHolder(fastBaseViewHolder);
RecyclerItemLayoutId recyclerItemLayoutId =
fastBaseViewHolderClass.getAnnotation(RecyclerItemLayoutId.class);
if (recyclerItemLayoutId == null) {
throw new IllegalArgumentException("RecyclerItemLayoutId is null");
}
int layoutId = recyclerItemLayoutId.value();

fastBaseViewHolder.onCreate();
if (layoutId == 0) {
throw new IllegalArgumentException("layoutId is null");
}

return fastViewHolder;
View itemView = mLayoutInflater.inflate(layoutId, parent, false);

try {
FastViewHolder fastViewHolder =
fastBaseViewHolderClass.getConstructor(View.class).newInstance(itemView);
fastViewHolder.onCreate();
return fastViewHolder;
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
Expand All @@ -98,7 +115,7 @@ private Class<? extends FastBaseViewHolder> getClassByType(int type){
}

@Override public void onBindViewHolder(FastViewHolder holder, int position) {
holder.getFastBaseViewHolder().refreshItem(mDatas.get(position).getData());
holder.refreshItem(mDatas.get(position).getData());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.aizuzi.adapter;
package com.zuzi.adapter;

/**
* @author liyi
* create at 2018/4/23
* @author liyi
* create at 2018/4/23
**/
public class FastItemBean<T> implements MultiItemEntity{
public class FastItemBean<T> implements MultiItemEntity {

private int itemType;

private String itemName;

private Class<? extends FastBaseViewHolder> itemClass;
private Class<? extends FastViewHolder> itemClass;

private T data;

Expand Down Expand Up @@ -38,12 +38,12 @@ public void setData(T data) {
this.data = data;
}

public Class<? extends FastBaseViewHolder> getItemClass() {
public Class<? extends FastViewHolder> getItemClass() {
return itemClass;
}

public void setItemClass(
Class<? extends FastBaseViewHolder> itemClass) {
Class<? extends FastViewHolder> itemClass) {
this.itemClass = itemClass;
}
}
25 changes: 25 additions & 0 deletions fastadapter/src/main/java/com/zuzi/adapter/FastViewHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.zuzi.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* @author liyi
* create at 2018/4/25
**/
public abstract class FastViewHolder<T> extends RecyclerView.ViewHolder{

public FastViewHolder(View itemView) {
super(itemView);
}

protected void onCreate(){

}

public <T extends View> T findViewById(int id) {
return itemView.findViewById(id);
}

public abstract void refreshItem(T bean);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.aizuzi.adapter;
package com.zuzi.adapter;

/**
* @author liyi
Expand Down
Loading

0 comments on commit 71147d3

Please sign in to comment.