个人觉得 RecyclerView组件确实值得学习并用到我们的项目中去,前面学了相关的内容,今天再补充一些相关的东东。
1,实现对RecyclerView中的数据进行添加和删除操作,还是一样直接上代码:
public class MainActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private List<TestData> mDatas;
private TestAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
initViews();
}
private void initDatas() {
mDatas = new ArrayList<TestData>();
TestData data = new TestData();
for (int i = ‘A‘; i < ‘z‘; i++) {
data.setTitle("Title" + (char) i);
data.setContent("Content" + (char) i);
mDatas.add(data);
}
}
private void initViews() {
mRecyclerView = (RecyclerView) findViewById(R.id.test_rcv);
mAdapter = new TestAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);
LinearLayoutManager ll = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_add://添加操作
if (mAdapter != null) {
mAdapter.add(1);// 我们添加在第2个位置,对应position=1
}
break;
case R.id.action_del://删除操作
if (mAdapter != null) {
mAdapter.delete(1);// 我们删除第2个位置,对应position=1
}
break;
}
return super.onOptionsItemSelected(item);
}
}
------------Adapter----------------------------------
public class TestAdapter extends Adapter<TestViewHolder> {
private Context mContext;
private List<TestData> mDatas;
public TestAdapter(Context mContext, List<TestData> mDatas) {
this.mContext = mContext;
this.mDatas = mDatas;
}
/**添加数据*/
public void add(int pos) {
TestData data = new TestData();
data.setContent("这是增加");
data.setTitle("添加");
mDatas.add(pos, data);
notifyItemInserted(pos);// 一定是调用 notifyItemInserted而不是调用 notifyDataSetChanged()
}
/**删除数据*/
public void delete(int pos) {
mDatas.remove(pos);
notifyItemRemoved(pos);// 一定是调用notifyItemRemoved而不是调用 notifyDataSetChanged()
}
@Override
public TestViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
View v = LayoutInflater.from(mContext).inflate(R.layout.lv_item, null);
TestViewHolder holder = new TestViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final TestViewHolder paramVH, final int paramInt) {
paramVH.titleTv.setText(mDatas.get(paramInt).getTitle());
paramVH.contentTv.setText(mDatas.get(paramInt).getContent());
}
@Override
public int getItemCount() {
return mDatas.size();
}
}
class TestViewHolder extends ViewHolder {
ImageView iconIv;
TextView titleTv;
TextView contentTv;
public TestViewHolder(View holder) {
super(holder);
iconIv = (ImageView) holder.findViewById(R.id.item_iv);
titleTv = (TextView) holder.findViewById(R.id.item_title);
contentTv = (TextView) holder.findViewById(R.id.item_content);
}
}
2,给RecyclerView中的Item添加分隔线:
实现addItemDecoration(RecyclerView.ItemDecoration itemDecoration)方法,我们需要写个类去继承ItemDecoration,现实现分隔功能。然后只需要一句代码:
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));//DividerItemDecoration在网上可以找到哦,还有很多相关动画实现的,可以参考:https://github.com/yqritc/RecyclerView-FlexibleDivider
3,部分小总结:
a,在给RecyclerView设置LinearLayoutManager时,
LinearLayoutManager ll = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);//第三个参数如果写成true,则我们item的pos顺序与展示的是反向的
mRecyclerView.setLayoutManager(ll);
b,我们同样可以给RecyclerView实现绚丽的分隔线,比如:https://github.com/yqritc/RecyclerView-FlexibleDivider
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android学习笔记:超能RecyclerView组件使用总结
原文地址:http://blog.csdn.net/true100/article/details/47832349