溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

RecyclerView中怎么實(shí)現(xiàn)水平列表

發(fā)布時(shí)間:2021-08-09 16:38:18 來(lái)源:億速云 閱讀:148 作者:Leah 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)RecyclerView中怎么實(shí)現(xiàn)水平列表,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

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);  //設(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<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();  }   /**   * 創(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);

關(guān)于RecyclerView中怎么實(shí)現(xiàn)水平列表就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI