一、概述
在github中搜索recyclerview关键字,点赞最多的就是(BaseRecyclerViewAdapterHelper)这个库,所以抽空写了个demo看下效果!
二、普通列表
FirstActivity.java
package com.czhappy.brvahdemo.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czhappy.brvahdemo.model.FirstData;
import com.czhappy.brvahdemo.R;
import com.czhappy.brvahdemo.adapter.FirstAdapter;
import com.czhappy.brvahdemo.utils.Utils;
import com.czhappy.brvahdemo.view.ItemDivider;
import java.util.ArrayList;
import java.util.List;
/**
* author: chenzheng
* created on: 2017/8/30 8:49
* description:
*/
public class FirstActivity extends AppCompatActivity {
private List<FirstData> mDataList;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
initView();
initData();
initAdapter();
}
private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//添加分割线
mRecyclerView.addItemDecoration(new ItemDivider().setDividerWith(Utils.dip2px(this, 0.5f)).setDividerColor(0xffdddddd));
}
@SuppressWarnings("unchecked")
private void initAdapter() {
final BaseQuickAdapter firstAdapter = new FirstAdapter(R.layout.item_first_list, mDataList);
//firstAdapter.openLoadAnimation();
firstAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
mDataList.get(position).name = "更新--"+position;
firstAdapter.setNewData(mDataList);
}
});
mRecyclerView.setAdapter(firstAdapter);
}
private void initData() {
mDataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
FirstData item = new FirstData();
item.drawable = R.mipmap.ic_launcher;
item.name = "张珊珊所所所所aaaaa343543543"+i;
mDataList.add(item);
}
}
}
FirstAdapter.java
package com.czhappy.brvahdemo