<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 3706字,需瀏覽 8分鐘

           ·

          2021-03-02 09:56

          效果圖:



          在項目中有時候會有需求說明列表能夠點擊查看更多和點擊收起的功能,這個時候很自然的就會想到RecyclerView,但是有時候RecyclerView會存在和ScrollView的沖突問題,所以一般情況下,會再RecyclerView的外層包裹一層就能解決問題

          <RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"/>         <android.support.v7.widget.RecyclerView        android:id="@+id/goods_list"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></RelativeLayout>


          但是有的時候在這個時候打開界面的時候會存在直接定位到RecyclerView的情況,這個是因為焦點的問題,我們只需要將焦點定位到根布局就可以解決問題了

          <LinearLayout     android:layout_width="match_parent"    android:layout_height="wrap_content"    android:focusable="true"    android:focusableInTouchMode="true"    android:orientation="horizontal">
          <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content">
          <android.support.v7.widget.RecyclerView android: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); }
          @Override 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(); }
          @Override 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)カ@取到我們的控件

          @Bind(R.id.tv_show_all_goods)TextView tvShowAllGoods;//展示的文字@Bind(R.id.show_all_goods)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é)束啦


          點擊這里留言交流哦


          瀏覽 267
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  老师的粉嫩小又紧水又多A片视频 | 日本黄色大片免费看视频 | 国产一区二区天堂 | 午夜青娱乐盛世 | 中文字幕第五页在线观看 |