Recyclerview使用方法

package com.example.lianxi_recyclerview;

public class Bean {
    
    
    private String name;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
}

package com.example.lianxi_recyclerview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    
    
private List<Bean> data;
private Context context;

    public MyAdapter(List<Bean> data, Context context) {
    
    
        this.data = data;
        this.context = context;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
        View view =View.inflate(context,R.layout.recyclerview_item,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    
    
    holder.tv.setText(data.get(position).getName());
    }

    @Override
    public int getItemCount() {
    
    
        return data == null ? 0 : data.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
    
    
        private TextView tv;
        public MyViewHolder(@NonNull View itemView) {
    
    
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            itemView.setOnClickListener(new View.OnClickListener() {
    
    
                @Override
                public void onClick(View v) {
    
    
                    if (mOnItemClickListener != null) {
    
    
                        mOnItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }
    private OnRecyclerItemClickListener mOnItemClickListener;
    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
    
    
        mOnItemClickListener = listener;
    }
    public interface OnRecyclerItemClickListener {
    
    
        void onRecyclerItemClick(int posoition);
    }
}

package com.example.lianxi_recyclerview;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    
    
private List<Bean> data = new ArrayList<>();

@Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for (int i = 0; i  < 40; i++) {
    
    
            Bean bean = new Bean();
            bean.setName("享学" + i);
            data.add(bean);
        }
    RecyclerView recyclerView = findViewById(R.id.rv);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(linearLayoutManager);

    MyAdapter myAdapter = new MyAdapter(data, this);
    recyclerView.setAdapter(myAdapter);

    myAdapter.setRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
    
    
        @Override
        public void onRecyclerItemClick(int posoition) {
    
    
            Log.e("1","leo:"+posoition);
        }
    });
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    />
</LinearLayout>
<?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">
<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    />
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_44584479/article/details/115213791

相关文章