本文实例为大家分享了RecyclerVIEw实现水平列表的具体代码,供大家参考,具体内容如下
1、效果图
2、activity_horizontallistvIEw.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/recyclervIEw_horizontal1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:overScrollMode="never" androID:scrollbars="none" /> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/recyclervIEw_horizontal2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:overScrollMode="never" androID:scrollbars="none" /> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/recyclervIEw_horizontal3" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:overScrollMode="never" androID:scrollbars="none" /></linearLayout>
3、activity代码
package ivan.com.appbackendtest;import androID.content.Context;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * Created by ivan on 2017/6/9. */public class horizontallistvIEwActivity extends AppCompatActivity { private RecyclerVIEw recyclervIEw_horizontal1; private galleryAdapter mAdapter1; private RecyclerVIEw recyclervIEw_horizontal2; private galleryAdapter mAdapter2; private RecyclerVIEw recyclervIEw_horizontal3; private galleryAdapter mAdapter3; private List<Integer> mDatas1; private List<Integer> mDatas2; private List<Integer> mDatas3; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_horizontallistvIEw); initDatas(); //得到控件 recyclervIEw_horizontal1 = (RecyclerVIEw)findVIEwByID(R.ID.recyclervIEw_horizontal1); //设置布局管理器 linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this); linearlayoutmanager.setorIEntation(linearlayoutmanager.HORIZONTAL); recyclervIEw_horizontal1.setLayoutManager(linearlayoutmanager); //设置适配器 mAdapter1 = new galleryAdapter(this,mDatas1); recyclervIEw_horizontal1.setAdapter(mAdapter1); //得到控件 recyclervIEw_horizontal2 = (RecyclerVIEw)findVIEwByID(R.ID.recyclervIEw_horizontal2); //设置布局管理器 linearlayoutmanager linearlayoutmanager2 = new linearlayoutmanager(this); linearlayoutmanager2.setorIEntation(linearlayoutmanager.HORIZONTAL); recyclervIEw_horizontal2.setLayoutManager(linearlayoutmanager2); //设置适配器 mAdapter2 = new galleryAdapter(this,mDatas2); recyclervIEw_horizontal2.setAdapter(mAdapter2); //得到控件 recyclervIEw_horizontal3 = (RecyclerVIEw)findVIEwByID(R.ID.recyclervIEw_horizontal3); //设置布局管理器 linearlayoutmanager linearlayoutmanager3 = new linearlayoutmanager(this); linearlayoutmanager3.setorIEntation(linearlayoutmanager.HORIZONTAL); recyclervIEw_horizontal3.setLayoutManager(linearlayoutmanager3); //设置适配器 mAdapter3 = new galleryAdapter(this,mDatas3); recyclervIEw_horizontal3.setAdapter(mAdapter3); } private voID initDatas() { mDatas1 = new ArrayList<>(Arrays.asList(R.mipmap.ic_launcher)); mDatas2 = new ArrayList<>(Arrays.asList(R.mipmap.ic_launcher,R.mipmap.ic_launcher)); mDatas3 = new ArrayList<>(Arrays.asList(R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher)); } public class galleryAdapter extends RecyclerVIEw.Adapter<galleryAdapter.VIEwHolder> { private LayoutInflater mInflater; private List<Integer> mDatas; public galleryAdapter(Context context,List<Integer> datats) { mInflater = LayoutInflater.from(context); mDatas = datats; } public class VIEwHolder extends RecyclerVIEw.VIEwHolder { public VIEwHolder(VIEw arg0) { super(arg0); } ImageVIEw mimg; TextVIEw mTxt; } @OverrIDe public int getItemCount() { return mDatas.size(); } /** * 创建VIEwHolder */ @OverrIDe public VIEwHolder onCreateVIEwHolder(VIEwGroup vIEwGroup,int i) { VIEw vIEw = mInflater.inflate(R.layout.item_ListvIEw,vIEwGroup,false); VIEwHolder vIEwHolder = new VIEwHolder(vIEw); vIEwHolder.mimg = (ImageVIEw) vIEw .findVIEwByID(R.ID.ID_index_gallery_item_image); return vIEwHolder; } /** * 设置值 */ @OverrIDe public voID onBindVIEwHolder(final VIEwHolder vIEwHolder,final int i) { vIEwHolder.mimg.setimageResource(mDatas.get(i)); } }}
4、核心代码
//得到控件 recyclervIEw_horizontal1 = (RecyclerVIEw)findVIEwByID(R.ID.recyclervIEw_horizontal1); //设置布局管理器 linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this); linearlayoutmanager.setorIEntation(linearlayoutmanager.HORIZONTAL); recyclervIEw_horizontal1.setLayoutManager(linearlayoutmanager); //设置适配器 mAdapter1 = new galleryAdapter(this,mDatas1); recyclervIEw_horizontal1.setAdapter(mAdapter1);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的使用RecyclerView实现水平列表全部内容,希望文章能够帮你解决使用RecyclerView实现水平列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)