Android学习笔记:超能RecyclerView组件使用总结

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习笔记:超能RecyclerView组件使用总结相关的知识,希望对你有一定的参考价值。

   个人认为 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组件使用总结的主要内容,如果未能解决你的问题,请参考以下文章

P4345 [SHOI2015]超能粒子炮改

bzoj4591 [Shoi2015]超能粒子炮·改

[SHOI2015]超能粒子炮·改

bzoj4591[Shoi2015]超能粒子炮·改 Lucas定理

P4345 [SHOI2015]超能粒子炮·改 Lucas

Bzoj4591 [Shoi2015]超能粒子炮·改