1. 程式人生 > >RecyclerView +介面回撥

RecyclerView +介面回撥

在這裡插入圖片描述

需要的依賴

com.android.support:recyclerview-v7:28.0.0

MainActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.example.day12_recyclerview.adapter.MyAdapter;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView Recycler_View;
    private ArrayList<String> lists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        MyAdapter myAdapter = new MyAdapter(lists, MainActivity.this);
        Recycler_View.setAdapter(myAdapter);
        myAdapter.setOnItemClick(new MyAdapter.OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, lists.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void initData() {
        for (int i = 0; i < 20; i++) {
            lists.add("這是第" + i + "條資料");
        }
    }
    private void initView() {
        Recycler_View = (RecyclerView) findViewById(R.id.Recycler_View);
         //列表     表格GridLayoutManager   瀑布流StaggeredGridLayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this); 
        Recycler_View.setLayoutManager(linearLayoutManager);
    }
}

MyAdapter

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.day12_recyclerview.R;
import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener {
    private ArrayList<String> lists;
    private Context mContext;

    public MyAdapter(ArrayList<String> lists, Context mContext) {
        this.lists = lists;
        this.mContext = mContext;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mContext, R.layout.item_view, null);
        view.setOnClickListener(this);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.mGetContent.setText(lists.get(i));
        viewHolder.itemView.setTag(i);
    }
    @Override
    public int getItemCount() {
        return lists.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mGetContent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mGetContent = itemView.findViewById(R.id.Get_Content);
        }
    }
    //介面
    //通過觀察ListView條目點選事件
    public interface OnItemClick {
        void onItemClick(View view, int position);
    }
    private OnItemClick onItemClick;
    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
    @Override
    public void onClick(View v) {
        if (onItemClick != null) {
            onItemClick.onItemClick(v, (int) v.getTag());
        }
    }
}

主頁面佈局

<?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=".MainActivity">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Recycler_View"
    ></android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

條目佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Get_Content"
    android:textSize="30sp"
    android:gravity="center_horizontal"/>
</LinearLayout>