欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android开发 RecyclerView 列表布局

程序员文章站 2022-03-13 17:11:17
...

创建一个一行的自定义布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:padding="10dp">
    <ImageView
        android:id="@+id/avatar"
        android:src="@drawable/ace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="15dp">
        <TextView
            android:id="@+id/nameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这里显示名字"
            android:textSize="30sp"
            android:textColor="@color/colorBlack"
            android:layout_marginBottom="15dp"/>
        <TextView
            android:id="@+id/contentsText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这里显示内容描述"
            android:textSize="15sp"
            android:textColor="@color/colorGray"
            android:maxLines="3" />
    </LinearLayout>

</LinearLayout>

效果图:

android开发 RecyclerView 列表布局

创建一个存放数据的Java类:

 */

public class ListData {
    private int imageView;
    private String nameText;
    private String contentsText;
    public ListData(int imageView,String nameText,String contentsText){
        this.imageView = imageView;
        this.nameText = nameText;
        this.contentsText = contentsText;
    }

    public int getImageView() {
        return imageView;
    }

    public String getContentsText() {
        return contentsText;
    }

    public String getNameText() {
        return nameText;
    }
}

创建一个适配器:

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);
            //注意这里可能需要import com.example.lenovo.myrecyclerview.R; 才能使用R.id
            imageAvatar = (ImageView)itemView.findViewById(R.id.avatar);
            nameText =(TextView) itemView.findViewById(R.id.nameText);
            contentsText = (TextView)itemView.findViewById(R.id.contentsText);

        }
    }
    public  ListAdapter(List<ListData> listDatas){
        mDataList = listDatas;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layoutlistview,
                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();
    }
}

在列表布局类中添加数据和适配器:

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);
        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);
        ListData baroque_works = new ListData(R.drawable.baroque_works,"baroque_works",
                "巴洛克工作社:巴洛克工作社的名义上的目的是建立理想王国," +
                        "真正目的是得到古代兵器“冥王”从而获得更强大的力量。");
        listDatas.add(baroque_works);
        ListData * = new ListData(R.drawable.*,"*",
                "布鲁克:人称鼻歌布鲁克。现为草帽海贼团音乐家。");
        listDatas.add(*);
        ListData buggy = new ListData(R.drawable.buggy,"buggy",
                "巴基:原为罗杰海贼团上的见习船员,与四皇之一的红发是旧友。" +
                        "现在是王下七武海的新成员,巴基是个极其有野心和欲望并肯为之去实现的男人。");
        listDatas.add(buggy);
        ListData chopper = new ListData(R.drawable.chopper,"chopper",
                "托尼托尼·乔巴:乔巴是草帽海贼团的船医,吃了人人果实的驯鹿,可用蓝波球进行八段身体变形。");
        listDatas.add(chopper);
        ListData franck = new ListData(R.drawable.franck,"franck",
                "弗兰奇:草帽海贼团船匠,性格豪放,喜欢唱歌,跳奇怪的舞,下身喜欢只穿一条短裤。身为改造人的弗兰奇,身体藏着各种兵器。");
        ListData hommes_poissons = new ListData(R.drawable.hommes_poissons,"hommes_poissons",
                "太阳海贼团:又称鱼人海贼团。由鱼人探险家费雪·泰格成立的海贼团," +
                        "成员多为人鱼和鱼人(但在航行期间曾经收留人类奴隶小女孩克尔拉)," +
                        "多数团员会在自己的身上,留下红色的太阳标志。");
        listDatas.add(hommes_poissons);
        ListData luffys_flag = new ListData(R.drawable.luffys_flag,"luffys_flag",
                "蒙奇·D·路飞:草帽海贼团、草帽大船团船长,极恶的世代之一。橡胶果实能力者," +
                        "悬赏金15亿贝里。梦想是找到传说中的One Piece,成为海贼王。");
        listDatas.add(luffys_flag);
        ListData luffys_flag_2 = new ListData(R.drawable.luffys_flag_2,"luffys_flag_2",
                "蒙奇·D·路飞:草帽海贼团、草帽大船团船长,极恶的世代之一。橡胶果实能力者," +
                        "悬赏金15亿贝里。梦想是找到传说中的One Piece,成为海贼王。");
        listDatas.add(luffys_flag_2);
        ListData nami = new ListData(R.drawable.nami,"nami",
                "娜美:草帽海贼团的航海士,路飞的第二个伙伴 ,悬赏6600万贝里");
        listDatas.add(nami);
        ListData nico = new ListData(R.drawable.nico,"nico",
                "妮可·罗宾:草帽一伙的考古学家,出生在西海的考古学之岛“奥哈拉”," +
                        "年仅8岁就被悬赏千万的奥哈拉幸存者。是拥有“花花果实”的恶魔果实能力者," +
                        "能让身体的任何部位像开花一样绽放在视线范围内的任何有形体的事物上并作出攻击或其他用途。");
        listDatas.add(nico);
        ListData sanji = new ListData(R.drawable.sanji,"sanji",
                "山治:草帽一伙厨师,金发,有着卷曲眉毛,永远遮住半边脸的家伙," +
                        "香烟不离口,最爱女人,很花心但很有风度,海贼中的绅士。");
        listDatas.add(sanji);
        ListData shanks = new ListData(R.drawable.shanks,"shanks",
                "shanks");
        listDatas.add(shanks);
        ListData ussop = new ListData(R.drawable.ussop,"ussop",
                "乌索普:。草帽海贼团狙击手,被称作“GOD·乌索普”,悬赏2亿贝里。" +
                        "司法岛事件中曾以“狙击王”的身份被悬赏3000万贝里。");
        listDatas.add(ussop);
        ListData vente_esclaves = new ListData(R.drawable.vente_esclaves,"vente_esclaves",
                "vente_esclaves");
        listDatas.add(vente_esclaves);
        ListData vivi = new ListData(R.drawable.vivi,"vivi",
                "奈菲鲁塔丽·薇薇:在B.W社中的名字是Miss Wednesday。" +
                        "是阿拉巴斯坦王国的公主,草帽一伙的朋友,坐骑是快跑鸭卡鲁。");
        listDatas.add(vivi);
        ListData zoro = new ListData(R.drawable.zoro,"zoro",
                "罗罗诺亚·索隆:草帽海贼团的剑士,使用三把刀战斗的三刀流剑士," +
                        "极恶的世代之一,也是二年前集结香波地群岛的十一超新星之一。");
        listDatas.add(zoro);






    }

}

实际效果:

android开发 RecyclerView 列表布局

相关标签: android