Android使用RecyclerView實現(xiàn)展開查看更多、收起功能
效果圖:

在項目中有時候會有需求說明列表能夠點擊查看更多和點擊收起的功能,這個時候很自然的就會想到RecyclerView,但是有時候RecyclerView會存在和ScrollView的沖突問題,所以一般情況下,會再RecyclerView的外層包裹一層就能解決問題
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"/><android.support.v7.widget.RecyclerViewandroid:id="@+id/goods_list"android:layout_width="match_parent"android:layout_height="wrap_content" /></RelativeLayout>
但是有的時候在這個時候打開界面的時候會存在直接定位到RecyclerView的情況,這個是因為焦點的問題,我們只需要將焦點定位到根布局就可以解決問題了
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:focusable="true"android:focusableInTouchMode="true"android:orientation="horizontal"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><android.support.v7.widget.RecyclerViewandroid:id="@+id/goods_list"android:layout_width="match_parent"android:layout_height="wrap_content" /></RelativeLayout></LinearLayout>
在我們需要焦點存在的地方加上
android:focusable="true"android:focusableInTouchMode="true"
就能將問題解決了,接下來就是需要我們自定adapter去實現(xiàn)所需要的功能了
private class OrderListGoodsAdapter extends BaseQuickAdapter<String, BaseViewHolder> {private boolean mIsShowOnlyCount;private int mCount = 3;//設(shè)置最多展示幾條數(shù)據(jù)public OrderListGoodsAdapter() {super(R.layout.item_layout);}protected void convert(BaseViewHolder helper, String item) {}/*** 設(shè)置是否僅顯示的條數(shù)* 默認(rèn)全部顯示*/public void setShowOnlyThree(boolean isShowOnlyThree) {setShowOnlyCount(isShowOnlyThree, 3);}/*** 設(shè)置顯示的條數(shù)*/public void setShowOnlyCount(boolean isShowOnlyThree, int count) {mIsShowOnlyCount = isShowOnlyThree;mCount = count;notifyDataSetChanged();}public int getItemCount() {return mIsShowOnlyCount ? super.getItemCount() > mCount ? mCount : super.getItemCount() : super.getItemCount();}}
在定義了adapter之后,在我們所需要的地方首先需要設(shè)置一個boolean值去判斷初始的時候是否只展示我們需要的三條數(shù)據(jù)
private boolean mIsShowOnlyThree = true;//是否只展示三條數(shù)據(jù)接下來我們?nèi)カ@取到我們的控件
TextView tvShowAllGoods;//展示的文字LinearLayout showAllGoods;//展示的布局

在定義了adapter之后,需要的就是初始化adapter并將數(shù)據(jù)放入到adapter當(dāng)中
//初始化商品列表adapter = new OrderListGoodsAdapter();adapter.setNewData(infoList);if (infoList.size() > 3) {adapter.setShowOnlyThree(mIsShowOnlyThree);showAllGoods.setVisibility(View.VISIBLE);tvShowAllGoods.setText("共" + infoList.size() + "個商品/點擊展開");} else {showAllGoods.setVisibility(View.GONE);}LinearLayoutManager layoutManager = new LinearLayoutManager(this);layoutManager.setSmoothScrollbarEnabled(true);layoutManager.setAutoMeasureEnabled(true);recyclerView.setLayoutManager(layoutManager);recyclerView.setHasFixedSize(true);recyclerView.setNestedScrollingEnabled(false);recyclerView.setAdapter(adapter);
在初始化之后再加上點擊事件的操作,我們所需要的功能也就實現(xiàn)了
mIsShowOnlyThree = !mIsShowOnlyThree;adapter.setShowOnlyThree(mIsShowOnlyThree);//改變箭頭的方向Drawable drawable = getResources().getDrawable(mIsShowOnlyThree ? R.drawable.ar_down : R.drawable.ar_up);drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());tvShowAllGoods.setCompoundDrawables(null, null, drawable, null);tvShowAllGoods.setText("共" + infoList.size() + (mIsShowOnlyThree ? "個商品/點擊展開" : "個商品/點擊收起"));

到這里就結(jié)束啦
評論
圖片
表情
