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

Android Studio RecyclerView

程序员文章站 2024-03-21 13:36:16
...

RecyclerView三种布局
线性布局
网格布局
瀑布流

代码片段

主类

package com.example.day1.LinerLayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.example.day1.R;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {
    RecyclerView recycler;
    RVAdapter rvAdapter;

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

        recycler = findViewById(R.id.recycler);

        //线性布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        recycler.setLayoutManager(linearLayoutManager);
        //网格布局
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
//      gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
//        recycler.setLayoutManager(gridLayoutManager);
        //瀑布流
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recycler.setLayoutManager(manager);
        //垂直排列
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getDrawable(R.drawable.recyc_divider));
        recycler.addItemDecoration(dividerItemDecoration);

        DefaultItemAnimator animator = new DefaultItemAnimator();
        animator.setRemoveDuration(200);
        animator.setAddDuration(200);
        //设置动画
        recycler.setItemAnimator(animator);

        final ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            list.add(i + "");
        }

        rvAdapter = new RVAdapter(Main2Activity.this, list, new recycLisnter() {
            @Override
            public void itemClick(int i) {
                list.remove(i);
                rvAdapter.notifyItemRemoved(i);
                //删除后更新下表
                rvAdapter.notifyItemRangeChanged(0, list.size());
            }

            @Override
            public void longClick(int i) {
                ArrayList<String> newList = new ArrayList<>();
                newList.add("A");
                newList.add("B");
                newList.add("C");
                list.addAll(i, newList);
                rvAdapter.notifyDataSetChanged();
            }
        });
        recycler.setAdapter(rvAdapter);
    }
}

适配器

package com.example.month3;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.RVHolder> {

    Context context;
    ArrayList<String> datas;
    recycLisnter recycLisnter;

    public RVAdapter(Context context, ArrayList<String> datas, com.example.month3.recycLisnter recycLisnter) {
        this.context = context;
        this.datas = datas;
        this.recycLisnter = recycLisnter;
    }

    @NonNull
    @Override
    public RVHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_recyc, viewGroup, false);
        return new RVHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RVHolder rvHolder, final int i) {
        rvHolder.textView.setText(datas.get(i));
        int random = (int) (Math.random() * 101 + 100);
        ViewGroup.LayoutParams params = rvHolder.textView.getLayoutParams();
        params.height = random;
        rvHolder.textView.setLayoutParams(params);
        rvHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recycLisnter.itemClick(i);
            }
        });

        rvHolder.textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                recycLisnter.longClick(i);
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class RVHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }
}

定义点击长按接口

public interface recycLisnter {
    void itemClick(int i);

    void longClick(int i);
}