以前的左右滑动效果采用自定义滚动视图或linearlayout者来实现,recyclerview可以很好的做这个功能,一般的需求就是要么一个独立的左右滑动效果,要么在一个列表里的中间部分一个左右滑动效果
而列表里面也容易,只是需要解决一点小问题,个人认为值得一提的就是高度问题,一般的人采用固定死的高度,可是在列表里面展示和机型的不同,固定死的话很难保证美观,动态的高度才能解决问题的所在
首先在一个列表控件布局上添加一个recyclerview控件
& lt; android.support.v7.widget.RecyclerView android: id=癅 + id/plan_recycler” android: layout_width=" match_parent " android: layout_height=" wrap_content "/祝辞
然后是适配器适配器布局
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android:取向=按怪薄盿ndroid: layout_width=皐rap_content” android: layout_height=" match_parent " android:填充=癅dimen/dimen_20dp”比; & lt; ImageView android: id=癅 + id/img_icon” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_gravity=" center_horizontal " android: src=" https://www.yisu.com/zixun/@drawable bbs_plan_mofa "/比; & lt; TextView android: id=癅 + id/tv_content” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_gravity=" center_horizontal " android: layout_marginTop=" @dimen/dimen_8dp” android: textSize=" 15 sp” 输入textColor=癅color/color_323232”android:/比; & lt;/LinearLayout>
接下来写适配器
进口android.content.Context; 进口android.support.v7.widget.RecyclerView; 进口android.view.LayoutInflater; 进口android.view.View; 进口android.view.ViewGroup; 进口android.widget.TextView; 进口com.xulu.loanmanager.R; 进口并不知道; 进口butterknife.BindView; 进口butterknife.ButterKnife;/* * *由LiuZhen> 私人空间initScrollList () { 最后RecyclerView planRecycler=(RecyclerView) headView.findViewById (R.id.plan_recycler); LinearLayoutManager LinearLayoutManager=new LinearLayoutManager (BBSDetailActivity.this); linearLayoutManager.setOrientation (LinearLayoutManager.HORIZONTAL); planRecycler.setLayoutManager (linearLayoutManager); List,列表=new ArrayList<的在();=new BBSPlanAdapter (BBSDetailActivity BBSPlanAdapter适配器。列表,这新BBSPlanAdapter.CallBack () { @Override 公共空间获得高度(int) { ViewGroup。LayoutParams params=planRecycler.getLayoutParams (); 参数个数。身高=身高; planRecycler.setLayoutParams (params); } @Override 公共空间ItemClick (int位置){ Toast.makeText (BBSDetailActivity.this”、“+位置,Toast.LENGTH_SHORT),告诉(); } }); planRecycler.setAdapter(适配器); }
很简单,完全替代自定义,效果如下,如果没有测量这一步可能会出现高度不适合,要么是看不到textview的文字,因为太低了,要么就是太高了,不美观。