Android學習筆記:超能RecyclerView組件使用總結

來源:互聯網
上載者:User

標籤:recyclerview   android開發   

   個人覺得 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組件使用總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.