使用RecyclerView實現(xiàn)水平列表
更新時間:2019年09月20日 09:51:22 作者:zhifanxu
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實現(xiàn)水平列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了RecyclerView實現(xiàn)水平列表的具體代碼,供大家參考,具體內(nèi)容如下
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);
//設(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);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android中RecyclerView實現(xiàn)多級折疊列表效果(二)
- Android中RecyclerView實現(xiàn)多級折疊列表效果(TreeRecyclerView)
- RecyclerView實現(xiàn)常見的列表菜單
- Android單個RecyclerView實現(xiàn)列表嵌套的效果
- Android RecyclerView實現(xiàn)數(shù)據(jù)列表展示效果
- Android使用RecyclerView實現(xiàn)自定義列表、點擊事件以及下拉刷新
- Android列表RecyclerView排列布局
- RecyclerView優(yōu)雅實現(xiàn)復(fù)雜列表布局
相關(guān)文章
Android創(chuàng)建與解析XML(三)——詳解Sax方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(三)——詳解Sax方式 ,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
Android Studio 4.0新特性及升級異常問題的解決方案
這篇文章主要介紹了Android Studio 4.0新特性及升級異常的相關(guān)問題,本文給大家分享解決方案,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
詳解如何在Android中實現(xiàn)懸浮Activity
本篇文章主要介紹詳解如何在Android中實現(xiàn)懸浮Activity,通過修改Activity的實現(xiàn)來適配平板設(shè)備,已達(dá)到代碼的最大利用率。有興趣的可以了解一下。2017-01-01
Android RecyclerView自定義上拉和下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView自定義上拉和下拉刷新效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02

