1.写一个一竖的自定义布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/horizontalImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ace" android:layout_margin="5dp"/> <TextView android:id="@+id/horizontalTextViewName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里显示名称" android:textColor="@color/colorBlack" android:textSize="20sp" android:layout_gravity="center"/> <TextView android:id="@+id/horizontalTextViewContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里显示描述内容描述内容描述内容描述内容" android:textColor="@color/colorblue" android:layout_gravity="left" android:maxLines="3" android:maxLength="10" android:ellipsize="end"/> </LinearLayout>
预览效果:
2.写一个适配器的类
package com.example.lenovo.myrecyclerview.RecyclerViewToolkit; 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 com.example.lenovo.myrecyclerview.R; import java.util.List; /** * Created by lenovo on 2018/5/2. */ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> { private List<ListData> mDataList; static class ViewHolder extends RecyclerView.ViewHolder{ ImageView imageAvatar; TextView nameText; TextView contentsText; public ViewHolder(View itemView) { super(itemView); imageAvatar = (ImageView)itemView.findViewById(R.id.horizontalImageView); nameText =(TextView) itemView.findViewById(R.id.horizontalTextViewName); contentsText = (TextView)itemView.findViewById(R.id.horizontalTextViewContent); } } public ListAdapter(List<ListData> listDatas){ mDataList = listDatas; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_horizontal_list_view, parent,false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { ListData listData = mDataList.get(position); holder.imageAvatar.setImageResource(listData.getImageView()); holder.nameText.setText(listData.getNameText()); holder.contentsText.setText(listData.getContentsText()); } @Override public int getItemCount() { return mDataList.size(); } }
3.在RecyclerView 横排列列表布局 活动类中,将RecyclerView 排列方式修改成横向。
package com.example.lenovo.myrecyclerview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.example.lenovo.myrecyclerview.RecyclerViewToolkit.ListAdapter; import com.example.lenovo.myrecyclerview.RecyclerViewToolkit.ListData; import java.util.ArrayList; import java.util.List; public class RecyclerViewActivity extends AppCompatActivity { private List<ListData> listDatas = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view); addingData(); RecyclerView recyclerView = (RecyclerView)findViewById(R.id.RecyclerView); //在此处修改布局排列方向 LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layoutManager); ListAdapter listAdapter = new ListAdapter(listDatas); recyclerView.setAdapter(listAdapter); } public void addingData(){ ListData ace = new ListData(R.drawable.ace,"ace", "波特卡斯·D·艾斯:" + "烧烧果实的前任能力者,绰号“火拳”,实力强大。"); listDatas.add(ace); ListData arlong = new ListData(R.drawable.arlong,"arlong", "恶龙海贼团:”由一群离开王下七武海之一甚平统治的鱼人所组成的海贼团,海贼船名为“鲨鱼˙斯帕布号”," + "船头放置著和恶龙一样的锯齿状鼻子,由于总是到处上岸杀戮,成为恐怖和霸权的象征。"); listDatas.add(arlong); ListData barbe_blanche = new ListData(R.drawable.barbe_blanche,"barbe_blanche", "爱德华·纽盖特:外号“白胡子”。“四皇”之一(生前),白胡子海贼团的船长,实力极其强劲,被称为「世界最强男人」。" + "能力是被誉为最强超人系的震震果实,武器为薙刀。"); listDatas.add(barbe_blanche); //以下略。。。。。。。。。。 } }
运行效果: