本文實(shí)例為大家分享了RecyclerView實(shí)現(xiàn)水平列表的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)公司服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過(guò)十多年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專(zhuān)業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶(hù)要求對(duì)網(wǎng)站進(jìn)行成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶(hù)網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶(hù)企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。
1、效果圖
2、activity_horizontallistview.xml
<?xml version="1.0" encoding="utf-8"?>
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 ListmDatas1; 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); //設(shè)置布局管理器 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerview_horizontal1.setLayoutManager(linearLayoutManager); //設(shè)置適配器 mAdapter1 = new GalleryAdapter(this, mDatas1); recyclerview_horizontal1.setAdapter(mAdapter1); //得到控件 recyclerview_horizontal2 = (RecyclerView)findViewById(R.id.recyclerview_horizontal2); //設(shè)置布局管理器 LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this); linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerview_horizontal2.setLayoutManager(linearLayoutManager2); //設(shè)置適配器 mAdapter2 = new GalleryAdapter(this, mDatas2); recyclerview_horizontal2.setAdapter(mAdapter2); //得到控件 recyclerview_horizontal3 = (RecyclerView)findViewById(R.id.recyclerview_horizontal3); //設(shè)置布局管理器 LinearLayoutManager linearLayoutManager3 = new LinearLayoutManager(this); linearLayoutManager3.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerview_horizontal3.setLayoutManager(linearLayoutManager3); //設(shè)置適配器 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(); } /** * 創(chuàng)建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; } /** * 設(shè)置值 */ @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); //設(shè)置布局管理器 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerview_horizontal1.setLayoutManager(linearLayoutManager); //設(shè)置適配器 mAdapter1 = new GalleryAdapter(this, mDatas1); recyclerview_horizontal1.setAdapter(mAdapter1);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。