使用RecyclerView实现水平列表
来源:本站原创|时间:2020-01-10|栏目:Android|点击: 次
本文实例为大家分享了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,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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10Android实现双击返回键退出应用实现方法详解
- 01-10android实现记住用户名和密码以及自动登录
- 01-10android实现简单计算器功能
- 01-10Android 友盟第三方登录与分享的实现代码
- 01-10C++自定义API函数实现大数相乘算法
- 01-10如何给Flutter界面切换实现点特效
- 01-10android实现指纹识别功能
- 01-10Android实现圆形渐变加载进度条
- 01-10RecyclerView仿应用列表实现网格布局
- 01-10Android实现底部导航栏的主界面
阅读排行
本栏相关
- 01-10Android自定义View之绘制圆形头像功能
- 01-10Android实现双击返回键退出应用实现方
- 01-10android实现简单计算器功能
- 01-10android实现记住用户名和密码以及自动
- 01-10C++自定义API函数实现大数相乘算法
- 01-10Android 友盟第三方登录与分享的实现代
- 01-10android实现指纹识别功能
- 01-10如何给Flutter界面切换实现点特效
- 01-10Android实现圆形渐变加载进度条
- 01-10Emoji表情在Android JNI中的兼容性问题详
随机阅读
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?