Skip to content

Commit

Permalink
Optimization of multiple item use code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadCym committed Apr 22, 2016
1 parent 1fde41f commit 64c74fa
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.baserecyclerviewadapterhelper.adapter.MultipleItemAdapter;
import com.chad.baserecyclerviewadapterhelper.adapter.MultipleItemQuickAdapter;
import com.chad.baserecyclerviewadapterhelper.data.DataServer;
/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
Expand All @@ -18,7 +18,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_multiple_item_use);
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
MultipleItemAdapter multipleItemAdapter = new MultipleItemAdapter(this,DataServer.getStrData(),R.layout.image_view,R.layout.text_view);
MultipleItemQuickAdapter multipleItemAdapter = new MultipleItemQuickAdapter(this,DataServer.getMultipleItemData());
mRecyclerView.setAdapter(multipleItemAdapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
@Deprecated
public class MultipleItemAdapter extends BaseQuickAdapter<String> {
private static final int TEXT_TYPE = 1;
private int mTextLayoutResId;
Expand Down Expand Up @@ -49,7 +50,6 @@ protected void convert(BaseViewHolder helper, String item) {
}

public class TextViewHolder extends BaseViewHolder {

public TextViewHolder(View itemView) {
super(itemView.getContext(), itemView);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.chad.baserecyclerviewadapterhelper.adapter;

import android.content.Context;

import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.entity.MultipleItem;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem> {

public MultipleItemQuickAdapter(Context context, List data) {
super(context, data);
addItmeType(MultipleItem.TEXT, R.layout.text_view);
addItmeType(MultipleItem.IMG, R.layout.image_view);
}

@Override
protected void convert(BaseViewHolder helper, MultipleItem item) {
switch (helper.getItemViewType()) {
case MultipleItem.TEXT:
helper.setImageUrl(R.id.tv, item.getContent());
break;
case MultipleItem.IMG:
helper.setImageUrl(R.id.iv, item.getContent());
break;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public SectionAdapter(Context context, int layoutResId, int sectionHeadResId, Li
@Override
protected void convertHead(BaseViewHolder helper,final MySection item) {
helper.setText(R.id.header, item.header);
if(!item.isMroe())helper.setVisible(R.id.more,false);
else
helper.setVisible(R.id.more,item.isMroe());
helper.setOnClickListener(R.id.more, new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chad.baserecyclerviewadapterhelper.data;


import com.chad.baserecyclerviewadapterhelper.entity.MultipleItem;
import com.chad.baserecyclerviewadapterhelper.entity.MySection;
import com.chad.baserecyclerviewadapterhelper.entity.Status;
import com.chad.baserecyclerviewadapterhelper.entity.Video;
Expand Down Expand Up @@ -80,6 +81,21 @@ public static List<String> getStrData() {
}
return list;
}
public static List<MultipleItem> getMultipleItemData() {
List<MultipleItem> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MultipleItem multipleItem = new MultipleItem();
String str = HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK;
multipleItem.setItemType(MultipleItem.IMG);
if (i % 2 == 0) {
str = CYM_CHAD;
multipleItem.setItemType(MultipleItem.TEXT);
}
multipleItem.setContent(str);
list.add(multipleItem);
}
return list;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.chad.baserecyclerviewadapterhelper.entity;

import com.chad.library.adapter.base.entity.MultiItemEntity;

/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
public class MultipleItem extends MultiItemEntity {
public static final int TEXT = 1;
public static final int IMG = 2;

private String content;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.chad.library.adapter.base;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
public abstract class BaseMultiItemQuickAdapter<T extends MultiItemEntity> extends BaseQuickAdapter {
protected List<T> mData;
private SparseArray<Integer> layouts;

/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
* some initialization data.
* @param context The context.
* @param data A new list is created out of this one to avoid mutable list
*/
public BaseMultiItemQuickAdapter(Context context, List<T> data) {
super(context, data);
this.mData = data == null ? new ArrayList<T>() : new ArrayList<T>(data);
this.mContext = context;
}

@Override
protected int getDefItemViewType(int position) {
return mData.get(position).getItemType();
}


@Override
protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
int layoutId = getLayoutId(viewType);
View view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return new BaseViewHolder(mContext, view);
}

private int getLayoutId(int viewType) {
return layouts.get(viewType);
}

protected void addItmeType(int type, int layoutResId) {
if (layouts == null) {
layouts = new SparseArray<>();
}
layouts.put(type, layoutResId);
}

@Override
protected void convert(BaseViewHolder helper, Object item) {
convert(helper, (T) item);
}

protected abstract void convert(BaseViewHolder helper, T item);


}


Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.List;



/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
Expand All @@ -45,24 +44,23 @@ public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<RecyclerV
/**
* Use with {@link #openLoadAnimation}
*/
public static final int ALPHAIN = 1 << 1;
public static final int ALPHAIN = 0x00000001;
/**
* Use with {@link #openLoadAnimation}
*/
public static final int SCALEIN = 1 << 2;
public static final int SCALEIN = 0x00000002;
/**
* Use with {@link #openLoadAnimation}
*/
public static final int SLIDEIN_BOTTOM = 1 << 3;
public static final int SLIDEIN_BOTTOM = 0x00000003;
/**
* Use with {@link #openLoadAnimation}
*/
public static final int SLIDEIN_LEFT = 1 << 4;
public static final int SLIDEIN_LEFT = 0x00000004;
/**
* Use with {@link #openLoadAnimation}
*/
public static final int SLIDEIN_RIGHT = 1 << 5;

public static final int SLIDEIN_RIGHT = 0x00000005;


protected static final String TAG = BaseQuickAdapter.class.getSimpleName();
Expand All @@ -87,9 +85,9 @@ public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<RecyclerV
@AnimationType
private BaseAnimation mCustomAnimation;
private BaseAnimation mSelectAnimation = new AlphaInAnimation();
protected static final int HEADER_VIEW = 1 << 6;
protected static final int LOADING_VIEW = 1 << 7;
protected static final int FOOTER_VIEW = 1 << 8;
protected static final int HEADER_VIEW = 0x00000111;
protected static final int LOADING_VIEW = 0x00000222;
protected static final int FOOTER_VIEW = 0x00000333;
private View mHeaderView;
private View mFooterView;

Expand Down Expand Up @@ -127,6 +125,10 @@ public BaseQuickAdapter(Context context, int layoutResId, List<T> data) {
this.mContext = context;
this.mLayoutResId = layoutResId;
}
public BaseQuickAdapter(Context context, List<T> data) {
this.mData = data == null ? new ArrayList<T>() : new ArrayList<T>(data);
this.mContext = context;
}

public void remove(int position) {
mData.remove(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class BaseSectionQuickAdapter<T extends SectionEntity> extends B

protected int mSectionHeadResId;
protected List<T> mData;
protected static final int SECTION_HEADER_VIEW = 0x00000004;
protected static final int SECTION_HEADER_VIEW = 0x00000444;

/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.chad.library.adapter.base.entity;

/**
* 项目名称:base-adapter-helper-recyclerview-master
* 类描述:
* 创建人:Chad
* 创建时间:16/4/22 上午11:04
*/
public abstract class MultiItemEntity {
protected int itemType;

public int getItemType() {
return itemType;
}

public void setItemType(int itemType) {
this.itemType = itemType;
}
}

0 comments on commit 64c74fa

Please sign in to comment.