RecyclerView实现列表

简单介绍RecyclerView:
接触过Listview的就知道,相对RecyclerView来说,ListView存在很多不足之处,比如实现点击Item里某个文本或者图片效果,虽然也能实现,不够很复杂,而RecyclerView很好解决了这个问题。并且还能实现瀑布布局,横向布局等。而ListView却无法实现。并且谷歌更加推荐使用RecyclerView。

项目目录如下
这里写图片描述
第一步:引入官方的依赖库
我这引入的是
compile ‘com.android.support:recyclerview-v7:25.3.1’
注意要与你的项目
compile ‘com.android.support:appcompat-v7:25.3.1’
版本保持一致

下面是我的布局文件
activity_mian.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.recyelerview.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

以及item.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="60dp"
    android:id="@+id/layout"
    android:descendantFocusability="blocksDescendants">
    <ImageView
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center_vertical"/>
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是内容"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="100dp"
        android:textColor="#000000"/>
</LinearLayout>

重点代码
第二步定义一个实体类ItemBean

public class ItemBean {
    private int IamgeId;
    private String Title;

    public ItemBean(int IamgeId, String Title) {
        this.IamgeId=IamgeId;
        this.Title=Title;
    }

    public int getIamgeId() {
        return IamgeId;
    }

    public void setIamgeId(int iamgeId) {
        IamgeId = iamgeId;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }
}

第二步适配器RecyclerViewAdapter

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

import java.util.List;


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private List<ItemBean> mList;

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView titleTextView;
        LinearLayout linearLayout;

        public ViewHolder(View view){
            super(view);
            imageView=(ImageView)view.findViewById(R.id.image);
            titleTextView=(TextView)view.findViewById(R.id.title);
            linearLayout=(LinearLayout)view.findViewById(R.id.layout);

        }
    }

    //传入展示数据,并且赋值给全局变量
    public RecyclerViewAdapter(List<ItemBean> List){
        mList=List;
    }

    //创建ViewHolder实例,将item布局加载出来
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    //对RecyclerView的子项数据进行赋值
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        ItemBean itemBean = mList.get(position);
        holder.imageView.setImageResource(itemBean.getIamgeId());
        holder.titleTextView.setText(itemBean.getTitle());
        //点击事件
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                ItemBean itemBean=mList.get(position);
                Toast.makeText(v.getContext(),itemBean.getTitle() + "当前位置" + position,Toast.LENGTH_SHORT).show();

            }
        });
    }
    //获取RecyclerView有多少子项
    @Override
    public int getItemCount() {
        return mList.size();
    }
}

MianActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<ItemBean> mList=new ArrayList<>();
    private RecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
    }

    private void initRecyclerView() {
        initItemBean();//初始化item数据
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter=new RecyclerViewAdapter(mList);
        recyclerView.setAdapter(adapter);
    }
    private void initItemBean() {
        for(int i=0;i<10;i++){
            ItemBean one= new ItemBean(R.mipmap.ic_launcher, "标题" + i);
            mList.add(one);
        }
    }
}

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页