Android嵌套RecyclerView左右滑动替代自定义view
程序员文章站
2022-07-11 23:19:10
以前的左右滑动效果采用自定义scrollview或者linearlayout来实现,recyclerview可以很好的做这个功能,一般的需求就是要么一个独立的左右滑动效果,...
以前的左右滑动效果采用自定义scrollview或者linearlayout来实现,recyclerview可以很好的做这个功能,一般的需求就是要么一个独立的左右滑动效果,要么在一个列表里的中间部分一个左右滑动效果
而列表里面也容易,只是需要解决一点小问题,个人认为值得一提的就是高度问题,一般的人采用固定死的高度,可是在列表里面展示和机型的不同,固定死的话很难保证美观,动态的高度才能解决问题的所在
首先在一个列表控件布局上添加一个recyclerview控件
<android.support.v7.widget.recyclerview android:id="@+id/plan_recycler" android:layout_width="match_parent" android:layout_height="wrap_content"/>
然后是adapter适配器布局
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="@dimen/dimen_20dp"> <imageview android:id="@+id/img_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/bbs_plan_mofa"/> <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="15sp" android:textcolor="@color/color_323232"/> </linearlayout>
接下来写adapter
import android.content.context; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import com.xulu.loanmanager.r; import java.util.list; import butterknife.bindview; import butterknife.butterknife; /** * created by liuzhen on 2017/6/22. */ public class bbsplanadapter extends recyclerview.adapter<bbsplanadapter.myviewholder> { private list<string> list; private layoutinflater minflater; private context context=null; private int height; private boolean ismeasure = false; private callback callback; public bbsplanadapter(context context, list<string> list, callback callback) { this.context=context; this.list = list; minflater = layoutinflater.from(context); this.callback = callback; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = minflater.inflate(r.layout.item_bbsdetail_plan, parent, false); if (!ismeasure) { view.measure(view.measurespec.unspecified, view.measurespec.unspecified); height = view.getmeasuredheight(); callback.getheight(height); } myviewholder holder = new myviewholder(view); return holder; } public int getheight(){ return height; } @override public void onbindviewholder(myviewholder holder, final int position) { holder.itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { callback.itemclick(position); } }); } @override public int getitemcount() { return 6; } static class myviewholder extends recyclerview.viewholder{ @bindview(r.id.tv_content) textview tv_content; myviewholder(view view){ super(view); butterknife.bind(this,view); } } public interface callback{ void getheight(int height); void itemclick(int position); } }
重点是measure方法,得到测量的高度
接下来就可以直接使用了
private void initscrolllist(){ final recyclerview planrecycler = (recyclerview) headview.findviewbyid(r.id.plan_recycler); linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(bbsdetailactivity.this); linearlayoutmanager.setorientation(linearlayoutmanager.horizontal); planrecycler.setlayoutmanager(linearlayoutmanager); list<string> list = new arraylist<>(); bbsplanadapter adapter = new bbsplanadapter(bbsdetailactivity.this, list, new bbsplanadapter.callback() { @override public void getheight(int height) { viewgroup.layoutparams params = planrecycler.getlayoutparams(); params.height = height; planrecycler.setlayoutparams(params); } @override public void itemclick(int position) { toast.maketext(bbsdetailactivity.this,""+position,toast.length_short).show(); } }); planrecycler.setadapter(adapter); }
很简单,完全替代自定义view,效果如下,如果没有测量这一步可能会出现高度不适合,要么是看不到textview的文字,因为太低了,要么就是太高了,不美观。
以上所述是小编给大家介绍的android嵌套recyclerview左右滑动替代自定义view,希望对大家有所帮助