Introduction and use of Recyclerview

Introduction:
1.RecylerView is a control in support-v7
2. This control is used to display a large number of datasets in a limited window, such as ListView and GridView.
RecyclerView architecture provides a plug-in experience, high degree of decoupling and exceptional flexibility. It provides different settings
LayoutManager, itemdecoration, itemanimator achieve amazing results.
1> If you want to control how it is displayed, please use the layout manager, LayoutManager
2> If you want to control the interval between items (paintable), please use ItemDecoration
3> If you want to control the animation of adding and deleting items, please use ItemAnimator
4> Events have to be written by yourself. Events that have no official response

It also uses the menu bar, just like wechat's menu bar for adding friends
First import dependency in build.gradle (second)
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
//If the dependent version is 26 and your version is not 26, try changing it to 26

Next is the layout

activity_main.xml

<LinearLayoutxmlns: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="com.example.ass.myapplication.MainActivity">

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

item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:scaleType="centerCrop"
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a18"/>

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="test"
        android:textSize="30dp"/>
</LinearLayout>

Layout of the menu bar in the upper right corner
In the menu bar, you can choose to display ListView,GridView, and waterfall flow
menu_title.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/one"
        android:title="ListView"
        app:showAsAction="never"></item>
    <item
        android:id="@+id/two"
        android:title="GridView"
        app:showAsAction="never"></item>
    <item
        android:id="@+id/three"
        android:title="Waterfall flow"
        app:showAsAction="never"></item>
</menu>

MainActivity

package com.example.ass.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
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 android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity {

    private int[] imgId=new int[]{
        R.drawable.a18,
            R.drawable.axly,
            R.drawable.cat,
            R.drawable.nhs,
            R.drawable.yyjdf
    };
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Get control
        rv=(RecyclerView) findViewById(R.id.recyclerView);
        ArrayList<User> list = new ArrayList<>();
        //Add data manually
        for (int i=0;i<100;i++){
            String conent = i + "Lin Zhiling";
            int img=imgId[i%imgId.length];
            User user = new User(img, conent);
            list.add(user);
        }

        //Set up layout manager
        rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        //Animating
        rv.setItemAnimator(new DefaultItemAnimator());
        //Set adapter
        final MyAdapter adapter = new MyAdapter(MainActivity.this, list);
        rv.setAdapter(adapter);
        //Click event for entry
        adapter.setOnMyItemClickListener(new MyAdapter.MyItemClick() {
            @Override
            public void itemClick(View view, int postion) {
            //id of the item toast clicked on
                Toast.makeText(MainActivity.this,"---"+postion,Toast.LENGTH_SHORT).show();
                User user = new User(R.mipmap.ic_launcher, "hello robot");
                //Add entry 
                //adapter.addUser(user,postion); 
                //Delete entry
                adapter.removeUser(postion);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.item,menu);
        return true;
    }
        //menu bar
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId=item.getItemId();
        switch (itemId) {
            case R.id.one:
                //listView layout
                rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                break;
            case R.id.two:
                //gridview layout
                rv.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
                break;
            case R.id.three:
                //Drain layout
                rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

MyAdapter

package com.example.ass.myapplication;
import android.content.Context;
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.TextView;
import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private Context context;
    private ArrayList<User> list;
    private MyItemClick myItemClick;//monitor

    public MyAdapter(Context context, ArrayList<User> list) {
        this.context=context;
        this.list=list;
    }

    //When creating ViewHolder
    //Instantiate ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Item layout
        View view = LayoutInflater.from(context).inflate(R.layout.item, null);
        //Instantiate ViewHolder and transfer the layout to
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    //When Binding ViewHolder
    //Set data for control
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.getTextView().setText(list.get(position).getContent());
        holder.getImageView().setImageResource(list.get(position).getImageId());
        //Item click event
        if(myItemClick!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i=holder.getLayoutPosition();
                    //Call the methods in the interface
                    myItemClick.itemClick(holder.itemView,i);
                }
            });
        }
    }

    //Number of entries
    @Override
    public int getItemCount() {
        return list.size();
    }

    //Define interface and implement item click event
    public interface MyItemClick {
        void itemClick(View view,int postion);
    }

    public void setOnMyItemClickListener(MyItemClick myItemClick){
        this.myItemClick = myItemClick;
    }

    //Add data
    public void addUser(User user,int position){
        list.add(position,user);
        //Update adapter
        notifyItemInserted(position);
    }
    //Delete data
    public void removeUser(int position){
        list.remove(position);
        //Update adapter
        notifyItemRemoved(position);
    }

    //ViewHolder
    class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.imageView=itemView.findViewById(R.id.image);
            this.textView=itemView.findViewById(R.id.content);
        }

        //getter setter method
        public ImageView getImageView(){
            return imageView;
        }

        public void setImageView(ImageView imageView) {
            this.imageView = imageView;
        }

        public TextView getTextView() {
            return textView;
        }

        public void setTextView(TextView textView) {
            this.textView = textView;
        }
    }
}

Bean class User

package com.example.ass.myapplication;

public class User {
    private int imageId;
    private String content;

    public User() {
    }

    public User(int imageId, String content) {
        this.imageId = imageId;
        this.content = content;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

Tags: Android xml Java Gradle

Posted on Sat, 02 May 2020 12:01:42 -0700 by TraceyK