android recyclerview列表的例子

本文概述

RecyclerView类扩展了ViewGroup类并实现了ScrollingView接口。它是在棉花糖中引入的。它是ListView的高级版本,具有改进的性能和其他优点。 RecyclerView主要用于通过对Android应用程序的列表和网格进行细粒度控制来设计用户界面。

在本教程中,我们将使用RecyclerView使用ImageView(用于图标)和TextView(用于描述)创建项目列表,并对列表项执行点击侦听器。

带有列表示例的Android RecyclerView

创建一个Android项目,然后在build.gradle文件中添加RecyclerView支持库com.android.support:recyclerview-v7:23.1.0或更高版本。

在布局目录中的activity_main.xml文件中,添加RecyclerView小部件。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/recyclerView"
        tools:context="example.srcmini02.com.recyclerviewlist.MainActivity">

</android.support.v7.widget.RecyclerView>

在values目录中创建dimens.xml文件,并添加以下代码。

dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="ic_clear_margin">56dp</dimen>
</resources>

使用以下代码创建一个自定义布局list_item.xml文件。

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightLarge"
    android:background="@drawable/border">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:contentDescription="Icon" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toEndOf="@id/imageView"
        android:layout_toRightOf="@id/imageView"
        android:gravity="center_vertical"
        android:textSize="16sp"/>

</RelativeLayout>

在drawable目录中创建一个border.xml文件,该文件用于装饰RecyclerView项的边框。

border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="1dp"
        android:color="#CCCCCC" />
</shape>

使用以下代码创建MyListData.java类。此类用作(POJO)类,用于设置项目的属性。

MyListData.java

package example.srcmini02.com.recyclerviewlist;
public class MyListData{
    private String description;
    private int imgId;
    public MyListData(String description, int imgId) {
        this.description = description;
        this.imgId = imgId;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getImgId() {
        return imgId;
    }
    public void setImgId(int imgId) {
        this.imgId = imgId;
    }
}

创建一个MyListAdapter.java类,并添加以下代码。此类扩展了RecyclerView.Adapter类并覆盖了其未实现的方法。 onCreateViewHolder()方法使list_item.xml膨胀。在onBindViewHolder()方法中,每个数据项都设置为每一行。

MyListAdapter.java

package example.srcmini02.com.recyclerviewlist;

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.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{
    private MyListData[] listdata;

   // RecyclerView recyclerView;
    public MyListAdapter(MyListData[] listdata) {
        this.listdata = listdata;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final MyListData myListData = listdata[position];
        holder.textView.setText(listdata[position].getDescription());
        holder.imageView.setImageResource(listdata[position].getImgId());
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "click on item: "+myListData.getDescription(), Toast.LENGTH_LONG).show();
            }
        });
    }


    @Override
    public int getItemCount() {
        return listdata.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView textView;
        public RelativeLayout relativeLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
            this.textView = (TextView) itemView.findViewById(R.id.textView);
            relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);
        }
    }
}

最后,在MainActivity.java类中,添加以下代码。此类为MyListData类创建项目数组,并将适配器类设置为RecyclerView。

MainActivity.java

package example.srcmini02.com.recyclerviewlist;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyListData[] myListData = new MyListData[] {
                new MyListData("Email", android.R.drawable.ic_dialog_email), new MyListData("Info", android.R.drawable.ic_dialog_info), new MyListData("Delete", android.R.drawable.ic_delete), new MyListData("Dialer", android.R.drawable.ic_dialog_dialer), new MyListData("Alert", android.R.drawable.ic_dialog_alert), new MyListData("Map", android.R.drawable.ic_dialog_map), new MyListData("Email", android.R.drawable.ic_dialog_email), new MyListData("Info", android.R.drawable.ic_dialog_info), new MyListData("Delete", android.R.drawable.ic_delete), new MyListData("Dialer", android.R.drawable.ic_dialog_dialer), new MyListData("Alert", android.R.drawable.ic_dialog_alert), new MyListData("Map", android.R.drawable.ic_dialog_map), };

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        MyListAdapter adapter = new MyListAdapter(myListData);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }
}

输出:

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?