Android使用RecyclerView实现展开查看更多、收起功能
效果图:

在项目中有时候会有需求说明列表能够点击查看更多和点击收起的功能,这个时候很自然的就会想到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去实现所需要的功能了
private class OrderListGoodsAdapter extends BaseQuickAdapter<String, BaseViewHolder> {private boolean mIsShowOnlyCount;private int mCount = 3;//设置最多展示几条数据public OrderListGoodsAdapter() {super(R.layout.item_layout);}protected void convert(BaseViewHolder helper, String item) {}/*** 设置是否仅显示的条数* 默认全部显示*/public void setShowOnlyThree(boolean isShowOnlyThree) {setShowOnlyCount(isShowOnlyThree, 3);}/*** 设置显示的条数*/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之后,在我们所需要的地方首先需要设置一个boolean值去判断初始的时候是否只展示我们需要的三条数据
 private boolean mIsShowOnlyThree = true;//是否只展示三条数据接下来我们去获取到我们的控件
TextView tvShowAllGoods;//展示的文字LinearLayout showAllGoods;//展示的布局

在定义了adapter之后,需要的就是初始化adapter并将数据放入到adapter当中
//初始化商品列表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);
在初始化之后再加上点击事件的操作,我们所需要的功能也就实现了
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 ? "个商品/点击展开" : "个商品/点击收起"));

到这里就结束啦
评论
