Skip to content

Commit

Permalink
增加了一个数据的转换方法,修复了一个viewpager适配器的错误。现在允许返回不同种的item
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale committed Jan 26, 2016
1 parent 78aa8a1 commit fced5ff
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kale.adapter.viewpager;
package kale.adapter;

import android.support.v4.util.ArrayMap;
import android.support.v4.view.PagerAdapter;
Expand Down
12 changes: 9 additions & 3 deletions adapter/src/main/java/kale/adapter/CommonAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import kale.adapter.item.AdapterItem;
import kale.adapter.util.AdapterItemUtil;
import kale.adapter.util.IAdapter;

/**
* @author Jack Tony
Expand Down Expand Up @@ -94,7 +95,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
mInflater = LayoutInflater.from(parent.getContext());
}

AdapterItem<T> item;
AdapterItem item;
if (convertView == null) {
item = createItem(mType);
convertView = mInflater.inflate(item.getLayoutResId(), parent, false);
Expand All @@ -103,11 +104,16 @@ public View getView(int position, View convertView, ViewGroup parent) {
item.setViews();
if (DEBUG) convertView.setBackgroundColor(0xffff0000);
} else {
item = (AdapterItem<T>) convertView.getTag(R.id.tag_item);
item = (AdapterItem) convertView.getTag(R.id.tag_item);
if (DEBUG) convertView.setBackgroundColor(0xff00ff00);
}
item.handleData(mDataList.get(position), position);
item.handleData(convertData(mDataList.get(position)), position);
return convertView;
}

@NonNull
@Override
public Object convertData(T data) {
return data;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kale.adapter.viewpager;
package kale.adapter;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
Expand All @@ -8,27 +8,26 @@

import java.util.List;

import kale.adapter.IAdapter;
import kale.adapter.R;
import kale.adapter.item.AdapterItem;
import kale.adapter.util.IAdapter;

/**
* @author Jack Tony
* @date 2015/11/29
*/
public abstract class CommonPagerAdapter<T extends AdapterItem> extends BasePagerAdapter<View> implements IAdapter<T> {
public abstract class CommonPagerAdapter<T> extends BasePagerAdapter<View> implements IAdapter<T> {

private List<T> mData;
private List<T> mDataList;

LayoutInflater mInflater;

public CommonPagerAdapter(List<T> data) {
mData = data;
mDataList = data;
}

@Override
public int getCount() {
return mData.size();
return mDataList.size();
}

@Override
Expand All @@ -50,7 +49,8 @@ protected View getWillBeDestroyedView(View item, int position) {
public void setPrimaryItem(ViewGroup container, int position, @NonNull Object object) {
// 这里应该放置数据更新的操作
if (object != currentItem) {
((AdapterItem<T>) ((View) object).getTag(R.id.tag_item)).handleData(mData.get(position), position);
AdapterItem item = (AdapterItem) ((View) object).getTag(R.id.tag_item);
item.handleData(convertData(mDataList.get(position)), position);
}
super.setPrimaryItem(container, position, object);
}
Expand All @@ -60,19 +60,25 @@ protected View createItem(ViewPager viewPager, int position) {
if (mInflater == null) {
mInflater = LayoutInflater.from(viewPager.getContext());
}
AdapterItem<T> item = createItem(getItemType(position));
AdapterItem item = createItem(getItemType(position));
View view = mInflater.inflate(item.getLayoutResId(), null);
view.setTag(R.id.tag_item, item); // 万一你要用到这个item可以通过这个tag拿到
item.bindViews(view);
item.setViews();
return view;
}

@NonNull
@Override
public Object convertData(T data) {
return data;
}

/**
* 强烈建议返回string,int,bool类似的基础对象做type
*/
public Object getItemType(int position) {
return getItemType(mData.get(position));
return getItemType(mDataList.get(position));
}

@Override
Expand All @@ -82,17 +88,17 @@ public Object getItemType(T t) {

@Override
public void setData(@NonNull List<T> data) {
mData = data;
mDataList = data;
}

@Override
public List<T> getData() {
return mData;
return mDataList;
}

@Override
public T getItem(int position) {
return mData.get(position);
return mDataList.get(position);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kale.adapter.recyclerview;
package kale.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
Expand All @@ -8,9 +8,9 @@

import java.util.List;

import kale.adapter.IAdapter;
import kale.adapter.item.AdapterItem;
import kale.adapter.util.AdapterItemUtil;
import kale.adapter.util.IAdapter;

/**
* @author Jack Tony
Expand Down Expand Up @@ -72,7 +72,7 @@ public Object getItemType(T t) {

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RcvAdapterItem<>(parent.getContext(), parent, createItem(mItemType));
return new RcvAdapterItem(parent.getContext(), parent, createItem(mItemType));
}

@SuppressWarnings("unchecked")
Expand All @@ -83,31 +83,31 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
item.itemView.setBackgroundColor(item.isNew ? 0xffff0000 : 0xff00ff00);
item.isNew = false;
}
((RcvAdapterItem) holder).getItem().handleData(mDataList.get(position), position);
((RcvAdapterItem) holder).item.handleData(convertData(mDataList.get(position)), position);
}

@NonNull
@Override
public Object convertData(T data) {
return data;
}


///////////////////////////////////////////////////////////////////////////
// 内部用到的viewHold
///////////////////////////////////////////////////////////////////////////

private static class RcvAdapterItem<T> extends RecyclerView.ViewHolder {
private static class RcvAdapterItem extends RecyclerView.ViewHolder {

private AdapterItem<T> mItem;
protected AdapterItem item;

public boolean isNew = true; // debug中才用到

protected RcvAdapterItem(Context context, ViewGroup parent, AdapterItem<T> item) {
protected RcvAdapterItem(Context context, ViewGroup parent, AdapterItem item) {
super(LayoutInflater.from(context).inflate(item.getLayoutResId(), parent, false));
mItem = item;
mItem.bindViews(itemView);
mItem.setViews();
this.item = item;
this.item.bindViews(itemView);
this.item.setViews();
}

protected AdapterItem<T> getItem() {
return mItem;
}

}

}
4 changes: 2 additions & 2 deletions adapter/src/main/java/kale/adapter/item/AdapterItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public interface AdapterItem<T> {
/**
* 根据数据来设置item的内部views
*
* @param model 数据list内部的model
* @param t 数据list内部的model
* @param position 当前adapter调用item的位置
*/
void handleData(T model, int position);
void handleData(T t, int position);

}
11 changes: 0 additions & 11 deletions adapter/src/main/java/kale/adapter/item/BaseAdapterItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public abstract class BaseAdapterItem<T> implements AdapterItem<T> {

protected View root;

protected int currentPos;

@Override
public void bindViews(View root) {
this.root = root;
Expand All @@ -24,15 +22,6 @@ public void bindViews(View root) {
*/
protected abstract void bindViews();

@Override
public void handleData(T model, int position) {
currentPos = position;
}

public int getCurrentPosition() {
return currentPos;
}

public final <E extends View> E getView(int id) {
try {
return (E) root.findViewById(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kale.adapter;
package kale.adapter.util;

import android.support.annotation.NonNull;

Expand All @@ -25,5 +25,12 @@ public interface IAdapter<T> {
* 当缓存中无法得到所需item时才会调用
*/
@NonNull
AdapterItem<T> createItem(Object type);
AdapterItem createItem(Object type);

/**
* @param data 从原始的list中取得得数据
* @return 放入adapterItem的最终数据
*/
@NonNull
Object convertData(T data);
}
26 changes: 0 additions & 26 deletions adapter/src/main/java/kale/adapter/viewpager/ViewPagerAdapter.java

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/java/kale/commonadapter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;

import kale.adapter.CommonAdapter;
import kale.adapter.recyclerview.CommonRcvAdapter;
import kale.adapter.CommonRcvAdapter;
import kale.adapter.item.AdapterItem;
import kale.commonadapter.item.ButtonItem;
import kale.commonadapter.item.ImageItem;
Expand Down

0 comments on commit fced5ff

Please sign in to comment.