一、思考
1、使用简单,一个方法就可以解决。
2、满足主流需求,
2.1、添加 HeadView和FootView
2.2、RecyclerView中实现 list套list
3、点击事件,添加监听。
4、刷新数据,单个item 和 多个listItem。
二、代码实现
1、BaseRecyclerAdapter.java 通用的RecyclerView适配器,实现 :点击事件,添加监听;刷新数据,单个item 和 多个listItem。
package xxx.xxx.xxx;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import xxx.xxx.xxx.interfaces.OnItemClickListenerRV;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.type;
/**
* 作者:wblobin
* 日期:2016/11/16 09:59
* 邮箱:563084073@qq.com
*/
public abstract class BaseRecyclerAdapter<T,VH extends BaseAdapterHelper>
extends RecyclerView.Adapter<BaseAdapterHelper> {
public static final int TYPE_BODY = 0; // item
private final Context context;
private final int layoutResId;
private final ArrayList<T> dataTs;
/**
* 构造方法
* @param context 上下文.
* @param layoutResId item布局文件.
* @param dataTs 泛型T的list数据,T是工厂类(一个存储数据的类,我个人喜欢称之为工厂类,也有bean类说法)
*/
public BaseRecyclerAdapter(Context context, int layoutResId, ArrayList<T> dataTs) {
this.context = context;
this.layoutResId = layoutResId;
this.dataTs = dataTs;
}
/**
* 创建View,如果是HeaderView或者是FooterView,直接在Holder中返回
* */
@Override
public BaseAdapterHelper onCreateViewHolder(ViewGroup parent, int viewType) {
return new BaseAdapterHelper(LayoutInflater.from(context).inflate(layoutResId, parent, false));
}
@Override
public int getItemViewType(int position) {
return TYPE_BODY;
}
@Override
public void onBindViewHolder(final BaseAdapterHelper holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListenerRV!=null)
onItemClickListenerRV.onItemClick(v,position,type);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if(onItemClickListenerRV==null){
return false;
}
onItemClickListenerRV.onItemLongClick(view,position,type);
return true;
}
});
convert((VH) holder, position);
}
@Override
public int getItemCount() {
return dataTs ==null?0: dataTs.size();
}
protected abstract void convert(VH helper, int position);
public List<T> getData() {
return dataTs;
}
/**
* 添加多条数据
* 在 position 位置插入了 count 个新项目
* */
public void addDatas(List<T> dataTs) {
int size = this.dataTs.size();
if (dataTs != null) {
this.dataTs.addAll(dataTs);
notifyItemRangeInserted(size, dataTs.size());
}
}
/**
* 添加一条数据,position默认尾部,可以指定
* */
public void addItemData(T item) {
dataTs.add(item);
notifyItemInserted(dataTs.size() - 1);
}
public void addItemData(int position,T item) {
position = position>=dataTs.size()? dataTs.size() : position;//防止溢出
dataTs.add(position,item);
notifyItemInserted(position);
}
/**
* 更新 position 位置的数据
* */
public void upItemData(int position, T item) {
dataTs.set(position, item);
notifyItemInserted(position);
}
/**
* 更新所有数据
* */
public void upAllData(List<T> dataTs) {
this.dataTs.clear();
this.dataTs.addAll(dataTs);
notifyDataSetChanged();
}
/**
* 初始化一个监听器, OnItemClickListener 和 OnLongClickListener,对列表的点击事件进行监听
* */
public OnItemClickListenerRV onItemClickListenerRV;
public void setOnItemClickListener(OnItemClickListenerRV listener) {
this.onItemClickListenerRV = listener;
}
}
2、OnItemClickListenerRV .java(RV表示RecyclerView)点击事件的添加监听接口。
package xxx.xxx.xxx;
import android.view.View;
/**
* 作者:wblobin
* 日期:2016/11/16 09:59
* 邮箱:563084073@qq.com
*/
public interface OnItemClickListenerRV {
void onItemClick(View view, int position );
void onItemLongClick(View view, int position );
}
3、BaseRecyclerAdapterHF.java (HF表示head foot),可以添加头部和尾部的适配器
package xxx.xxx.xxx;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 09:59
* 邮箱:563084073@qq.com
*/
public abstract class BaseRecyclerAdapterHF<T,VH extends BaseAdapterHelper>
extends BaseRecyclerAdapter<T,BaseAdapterHelper> {
public static final int TYPE_HEADER = 1; //说明是头,带有Header的
public static final int TYPE_FOOTER = 2; //说明是脚,带有Footer的
private final Context context;
private final int layoutResId;
private final ArrayList<T> dataTs;
/**
* 构造方法
* @param context 上下文.
* @param layoutResId item布局文件.
* @param dataTs 泛型T的list数据,T是工厂类(一个存储数据的类,我个人喜欢称之为工厂类,也有bean类说法)
*/
public BaseRecyclerAdapterHF(Context context, int layoutResId, ArrayList<T> dataTs) {
super(context,layoutResId,dataTs);
this.context = context;
this.layoutResId = layoutResId;
this.dataTs = dataTs;
}
/** 重写这个方法,很重要,是加入Header和Footer的关键,我们通过判断item的类型,从而绑定不同的view */
@Override
public int getItemViewType(int position) {
if (headerView != null && position == 0){//在‘头’存在的情况下,第一个item应该加载Header
return TYPE_HEADER;
}
if (footerView != null && position == getItemCount()-1){//在‘脚’存在的情况下,最后一个,应该加载Footer
return TYPE_FOOTER;
}
return super.getItemViewType(position);
}
/**
* 创建View,如果是HeaderView或者是FooterView,直接在Holder中返回
* */
@Override
public BaseAdapterHelper onCreateViewHolder(ViewGroup parent, int viewType) {
if(headerView != null && viewType == TYPE_HEADER) {
return new BaseAdapterHelper(headerView);
}
if(footerView != null && viewType == TYPE_FOOTER){
return new BaseAdapterHelper(footerView);
}
BaseAdapterHelper holder = new BaseAdapterHelper(LayoutInflater.from(context).inflate(layoutResId, parent, false));
return holder;
}
@Override
public int getItemCount() {
return dataTs ==null? getCount4HeadFoot() : dataTs.size()+getCount4HeadFoot();
}
public int getCount4HeadFoot(){
if(headerView == null && footerView == null){
return 0;
}else if(headerView != null && footerView != null){
return 2;
}else {
return 1;
}
}
public T getItem(int position) {
if(headerView != null) position = position-1;//这里加载数据的时候要注意,是从position-1开始,因为position==0已经被header占用了
if (position >= dataTs.size()) return null;
return dataTs.get(position);
}
/**
* HeaderView和FooterView的get和set函数
* */
private View headerView;//HeaderView,
private View footerView;// FooterView
public View getHeaderView() {
return headerView;
}
public void setHeaderView(View headerView) {
this.headerView = headerView;
}
public View getFooterView() {
return footerView;
}
public void setFooterView(View footerView) {
this.footerView = footerView;
}
/**
* 移除 HeaderView和FooterView
* */
public void removeHeaderView(){
setHeaderView(null);
notifyDataSetChanged();
}
public void removeFooterView(){
setFooterView(null);
notifyDataSetChanged();
}
}
4、BaseRecyclerAdapterT.java(T表示title)。
其实这逻辑比较简单,和BaseRecyclerAdapterHF类似,只要加一个标志进行区分,在getItemViewType()方法里面处理。
但是数据处理比较麻烦,要有通用性,开始我的泛型之旅。以前一直对泛型望而却步,这一块仅仅停留在复制粘贴套用,现在发现了泛型的强大。
listParent套listSubclass 对这两个做了抽取共性,各写了一个类。为使用BaseRecyclerAdapterT的工厂类写一个父类,而且必须继承。
package xxx.xxx.xxx;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import xxx.xxx.xxx.BaseItemParent;
import xxx.xxx.xxx.BaseItemSubclass;
import xxx.xxx.xxx.BaseItemUtil;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 09:59
* 邮箱:563084073@qq.com
*/
public abstract class BaseRecyclerAdapterT<TP extends BaseItemParent,TS extends BaseItemSubclass,VH extends BaseAdapterHelper>
extends BaseRecyclerAdapter<TP,BaseAdapterHelper> {
public static final int TYPE_TITLE = 3; //item子列表的标题
private Context context;
private int layoutParent;
private int layoutSubclass;
private ArrayList<TP> dataTParent;//TP =T_Parent
private ArrayList<TS> dataTSubclass = null;//TS =T_Subclass
private TS tSubclass;//TS =T_Subclass
/**
* 构造方法
* @param context 上下文.
* @param layoutParent item子list标题 布局文件.
* @param layoutSubclass item子list布局文件.
* @param dataTParent 泛型TP的list数据,TP和TS 是工厂类(一个存储数据的类,由变量极其set和get组成,我个人喜欢称之为工厂类,也有bean类说法)
* @param tSubclass 泛型TS,因为泛型无法new一个对象,这个参数就是直接传【new Bean()】;
*/
public BaseRecyclerAdapterT(Context context, int layoutParent, int layoutSubclass, ArrayList<TP> dataTParent, TS tSubclass ) {
this(context,layoutSubclass,dataTParent);
this.context = context;
this.layoutParent = layoutParent;
this.layoutSubclass = layoutSubclass;
this.dataTParent = dataTParent;
this.tSubclass = tSubclass;
dataTSubclass = new BaseItemUtil<TP,TS>().setDataSubclass(dataTParent,tSubclass);
}
public BaseRecyclerAdapterT(Context context, int layoutResId, ArrayList<TP> dataTs) {
super(context,layoutResId,dataTs);
}
public void setData( ArrayList<TP> dataTs ){
this.dataTParent = dataTs;
this.dataTSubclass = new BaseItemUtil<TP,TS>().setDataSubclass(dataTParent,tSubclass);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if ( dataTSubclass.get(position).getListItemType() == TYPE_TITLE){
return TYPE_TITLE;
}
return super.getItemViewType(position);
}
/**
* 创建View,如果是HeaderView或者是FooterView,直接在Holder中返回
* */
@Override
public BaseAdapterHelper onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_TITLE) {
return new BaseAdapterHelper(LayoutInflater.from(context).inflate(layoutParent, parent, false));
}
return new BaseAdapterHelper(LayoutInflater.from(context).inflate(layoutSubclass, parent, false));
}
@Override
public int getItemCount() {
return dataTSubclass==null?0:dataTSubclass.size();
}
public TS getItemSubclassItem(int position) {
return dataTSubclass.get(position);
}
public TP getItemParent(int position) {
if(dataTParent==null){
return null;
}
int itemI = dataTSubclass.get(position).getListItemI();
if(itemI>=dataTParent.size()){
itemI=dataTParent.size()-1;
}
return dataTParent.get(itemI);
}
}
5、BaseItemParent.java 是 listParent的父类【listParent套listSubclass】
package xxx.xxx.xxx;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 16:31
* 邮箱:563084073@qq.com
*/
public class BaseItemParent<T extends BaseItemSubclass> {
public ArrayList<T> listParent;
public ArrayList<T> getListParent() {
return listParent;
}
public void setListParent(ArrayList<T> listParent) {
this.listParent = listParent;
}
}
6、BaseItemSubclass.java 是 listSubclass的父类【listParent套listSubclass】
package xxx.xxx.xxx;
/**
* 作者:wblobin
* 日期:2016/11/16 16:31
* 邮箱:563084073@qq.com
*/
public class BaseItemSubclass {
/**
* 在listParent
* */
private int listPositionParent;
/**
* listSubclass 中的位子
* */
private int listPositionSubclass;
/**
* 用于区分listSubclass 中是title还是list
* */
private int listItemType;
public int getListPositionParent() {
return listPositionParent;
}
public void setListPositionParent(int listPositionParent) {
this.listPositionParent = listPositionParent;
}
public int getListPositionSubclass() {
return listPositionSubclass;
}
public void setListPositionSubclass(int listPositionSubclass) {
this.listPositionSubclass = listPositionSubclass;
}
public int getListItemType() {
return listItemType;
}
public void setListItemType(int listItemType) {
this.listItemType = listItemType;
}
}
7、BaseItemUtil.java 是一个工具类,就一个用途,把listParent转换成listSubclass。
其中有一个参数TS tSubclass, 这个参数很无奈,传 new TS(),只要创建就可以的不需要设置。
如果谁有更好的方案,请告知我QQ563084073
package xxx.xxx.xxx;
import xxx.xxx.xxx.Recycler.BaseRecyclerAdapterT;
import xxx.xxx.xxx.Recycler.BaseRecyclerAdapter;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 18:22
* 邮箱:563084073@qq.com
*/
public class BaseItemUtil<TP extends BaseItemParent, TS extends BaseItemSubclass> {
/**
* @param dataParent 获取的原始数据,listParent.
* @param tSubclass 这个其实传的很无奈,传 new TS(),只要创建就可以的不需要设置
* */
public synchronized ArrayList<TS> setDataSubclass(ArrayList<TP> dataParent,TS tSubclass){
if(dataParent==null){
return null;
}
int itemSubclassPosition = 0;
ArrayList<TS> result = new ArrayList<TS>();
for (int i=0; i<dataParent.size(); i++){
tSubclass.setListItemType(BaseRecyclerAdapterT.TYPE_TITLE);
tSubclass.setListPositionParent(i);
tSubclass.setListPositionSubclass(itemSubclassPosition);
result.add(itemSubclassPosition,tSubclass);
itemSubclassPosition ++;
ArrayList<TS> tSubclassDatas = dataParent.get(i).getListParent();
if(tSubclassDatas!=null) {
for (int j = 0; j < tSubclassDatas.size(); j++) {
tSubclassDatas.get(j).setListItemType(BaseRecyclerAdapter.TYPE_BODY);
tSubclassDatas.get(j).setListPositionParent(i);
tSubclassDatas.get(j).setListPositionSubclass(itemSubclassPosition);
itemSubclassPosition++;
}
result.addAll(tSubclassDatas);
}
}
return result;
}
}
三、实现添加头部和底部, Head 和 Foot
1、RecyclerHeadFootAdapter.java 看到了吧,RecyclrView适配器超级简单的,几句就解决了,一劳永逸。
package xxx.xxx.xxx;
import android.content.Context;
import android.view.View;
import xxx.xxx.xxx.R;
import xxx.xxx.xxx.BaseAdapterHelper;
import xxx.xxx.xxx.BaseRecyclerAdapterHF;
import xxx.xxx.xxx.BaseRecyclerAdapter;
import xxx.xxx.xxx.Strategy;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 15:14
* 邮箱:563084073@qq.com
*/
public class RecyclerHeadFootAdapter
extends BaseRecyclerAdapterHF<Strategy, BaseAdapterHelper> {
public RecyclerHeadFootAdapter(Context context, int layoutResId, ArrayList<Strategy> dataTs) {
super(context, layoutResId, dataTs);
}
@Override
protected void convert(final BaseAdapterHelper holder, int position) {
final int type = getItemViewType(position);
switch (type) {
case BaseRecyclerAdapterHF.TYPE_HEADER:
break;
case BaseRecyclerAdapterHF.TYPE_FOOTER:
break;
case BaseRecyclerAdapter.TYPE_BODY:
break;
}
}
}
2、RecyclerHeadFootFf.java 保留了主要的方法,一些无关理解的细节删除了。为了签订的保密协议,程序员都理解。
package xxx.xxx.xxx;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import xxx.xxx.xxx.R;
import xxx.xxx.xxx.BaseFragment;
import xxx.xxx.xxx.RecyclerHeadFootAdapter;
import xxx.xxx.xxx.Strategy;
import xxx.xxx.xxx.OnItemClickListenerRV;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 15:14
* 邮箱:563084073@qq.com
*/
public class RecyclerHeadFootFf
extends BaseFragment
implements SwipeRefreshLayout.OnRefreshListener,OnItemClickListenerRV {
private SwipeRefreshLayout swipeLayout;
private RecyclerView recyclerview;
private RecyclerHeadFootAdapter adapter;
private ArrayList<Strategy> lists;
private View footerView;
/**
* 这是一个下拉刷新功能, 保留了一个网络访问方法,里面其他的细节删除了,程序员都理解。
* 上拉加载更多,其实可以通过监听RecyclrView的滑动事件,滑动到底部,就是加载更多。
* */
@Override
public void onRefresh() {
getStrategyList( );
}
@Override
public void onItemClick(View view, int position ) {
/**
* 处理 list item的 点击事件,里面的细节删除了,程序员都理解。
* */
}
@Override
public void onItemLongClick(View view, int position ) {
/**
* 处理 list item的 长按事件,里面的细节删除了,程序员都理解。
* */
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a1008test_tablayout_recycler_f, container, false);
lists = new ArrayList<Strategy>();
swipeLayout.setOnRefreshListener(this);//添加 SwipeRefreshLayout.OnRefreshListener 下拉刷新的监听事件(android.support.v4.widget.SwipeRefreshLayout)
swipeLayout.setColorSchemeColors(getResources().getColor(R.color.primary2));
footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.a0000_list_footer, null, false);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
adapter = new RecyclerHeadFootAdapter(getActivity(),R.layout.a1008_strategy_item,lists );
adapter.setOnItemClickListener(this);//添加OnItemClickListenerRV(前面写了这个接口)RecyclrView的点击的监听事件
adapter.setFooterView(footerView);
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
public void getStrategyList( ){
/**
* 这个是一个网络操作,是真实的接口,里面的细节删除了,程序员都理解。
* */
}
}
四、listParent套listSubclass的BaseRecyclerAdapterT实践
1、RecyclerTitleAdapter.java。 listParent套listSubclass BaseRecyclerAdapterT适配器的使用
package xxx.xxx.xxx;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import xxx.xxx.xxx.R;
import xxx.xxx.xxx.RestModel;
import xxx.xxx.xxx.BaseAdapterHelper;
import xxx.xxx.xxx.BaseRecyclerAdapterT;
import xxx.xxx.xxx.BaseRecyclerAdapter;
import xxx.xxx.xxx.RssList;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 15:14
* 邮箱:563084073@qq.com
*/
public class RecyclerTitleAdapter
extends BaseRecyclerAdapterT<RssList, RestModel.RssNews, BaseAdapterHelper> {
private Context context;
private String timestamp="1479996668";//2016/11/24 22:11:8
public RecyclerTitleAdapter(Context context, int layoutResIdTitle, int layoutResId, ArrayList<RssList> rssList ) {
super(context, layoutResIdTitle, layoutResId, rssList, new RestModel.RssNews());
this.context = context;
}
public void setData( ArrayList<RssList> dataTs, String timestamp){
this.timestamp = timestamp;
super.setData(dataTs);
}
@Override
protected void convert(BaseAdapterHelper helper, final int position) {
switch (getItemViewType(position)){
case TYPE_TITLE:
final RssList rssListTiele = getItemParent(position);
helper.getTextView( R.id.tv_rss_name).setText("");
ImageView iv_rss_logo = helper.get( R.id.iv_rss_logo);
break;
case BaseRecyclerAdapter.TYPE_BODY:
RestModel.RssNews rssNewsItem = getItemSubclassItem(position);
TextView title = helper.getTextView(R.id.newsHead);
ImageView image = helper.get(R.id.newsImage);
break;
}
}
}
2、RecyclerTitleAf .java。listParent套listSubclass 适配器的实践
package xxx.xxx.xxx;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import xxx.xxx.xxx.R;
import xxx.xxx.xxx.BaseActivity;
import xxx.xxx.xxx.BaseRecyclerAdapterT;
import xxx.xxx.xxx.RecyclerTitleAdapter;
import xxx.xxx.xxx.OnItemClickListenerRV;
import xxx.xxx.xxx.data.RssList;
import xxx.xxx.xxx.RssOrderNews;
import java.util.ArrayList;
/**
* 作者:wblobin
* 日期:2016/11/16 15:14
* 邮箱:563084073@qq.com
*/
public class RecyclerTitleAf
extends BaseActivity
implements SwipeRefreshLayout.OnRefreshListener, OnItemClickListenerRV {
private SwipeRefreshLayout swipeLayout;
RecyclerView recyclerview;
private RecyclerTitleAdapter adapter;
private ArrayList<RssList> rssList;
@Override
public void onResume() {
super.onResume();
doNewsResponse();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a00test_orderlist_a);
initListView();
}
private void initListView() {
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
recyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
adapter = new RecyclerTitleAdapter(getContext(), R.layout.m1010_orderlist_item, R.layout.m1010_newslistitems, rssList );
adapter.setOnItemClickListener(this);
recyclerview.setAdapter(adapter);
}
@Override
public void onRefresh() {
doNewsResponse();
}
String timestamp;
private void showList(RssOrderNews newsResponse) {
timestamp = newsResponse.timestamp;
rssList = newsResponse.rss;
if (rssList == null) return;
for (int i = 0; i < rssList.size(); i++) {
//因为我采用的是retrofit自带的json解析,如果自己一步步的解析,这一句可以包含在里面
rssList.get(i).setListParent(rssList.get(i).news);
}
MyApplication.htlog.e("---RecyclerTitleA---showList---" + ",\tParent.Size()=" + rssList.size() );
if (adapter != null) {
adapter.setData(rssList, timestamp);
}
}
@Override
public void onItemClick(View view, int position ) {
onClickList(position);
}
private void onClickList(int position){
RestModel.RssNews rssNewsItem = adapter.getItemSubclassItem(position);
RssList rssListTitle = rssList.get(rssNewsItem.getListPositionParent());//也可以用rssListTitle = adapter.getItemParent(position);
if (rssNewsItem.getListItemType() == BaseRecyclerAdapterT.TYPE_TITLE) {
/**
* 点击listSubclass 的 title 事件
* 此处省略
* */
} else {
/**
* 点击listSubclass 的 list 事件
* 此处省略
* */
}
}
@Override
public void onItemLongClick(View view, int position ) {
}
private void doNewsResponse() {
/**
* 点击listSubclass 的 list 事件
* 此处省略
* */
showList(new RssOrderNews());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home: // 对用户按home icon的处理,本例只需关闭activity,就可返回上一activity,即主activity。
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
五、差点忘了RecyclerView.ViewHolder
1、RecyclerTitleAdapter.java。 listParent套listSubclass BaseRecyclerAdapterT适配器的使用
package xxx.xxx.xxx;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 作者:wblobin
* 日期:2016/11/16 09:59
* 邮箱:563084073@qq.com
*/
public class BaseAdapterHelper
extends RecyclerView.ViewHolder {
private SparseArray<View> views;
public BaseAdapterHelper(View itemView) {
super(itemView);
this.views = new SparseArray<View>();
}
public TextView getTextView(int viewId) {
return get(viewId);
}
public Button getButton(int viewId) {
return get(viewId);
}
public ImageView getImageView(int viewId) {
return get(viewId);
}
public View getView(int viewId) {
return get(viewId);
}
@SuppressWarnings("unchecked")
public <T extends View> T get(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
}
。
。
。
。
。
。
网友评论