这篇文章将为大家详细讲解有关RecyclerView中怎么实现水平列表,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联是一家集网站建设,鱼峰企业网站建设,鱼峰品牌网站建设,网站定制,鱼峰网站建设报价,网络营销,网络优化,鱼峰网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
2、activity_horizontallistview.xml
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 mDatas1; private List mDatas2; private List 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 { private LayoutInflater mInflater; private List mDatas; public GalleryAdapter(Context context, List 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中怎么实现水平列表
URL分享:
http://bzwzjz.com/article/igjsjg.html