android_recyclerView适配器写法

效果图

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.iamchan.recyc.MainActivity">
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="150dp" />
    <View
        android:background="#f5f5f5"
        android:layout_width="match_parent"
        android:layout_height="2dp"></View>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <Button
            android:id="@+id/btn1"
            android:text="竖直"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/btn2"
            android:text="grid"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/btn3"
            android:text="瀑布"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

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



</LinearLayout>

java

package com.example.iamchan.recyc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {



    private RecyclerView recyc;
    private List<String> stringList= Arrays.asList("a","b","c","d","e","f","g","h","i","j","k");
    private Button btn1,btn2,btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyc= (RecyclerView) findViewById(R.id.recyc);
        btn1= (Button) findViewById(R.id.btn1);
        btn2= (Button) findViewById(R.id.btn2);
        btn3= (Button) findViewById(R.id.btn3);

        RecycAdapter recycAdapter=new RecycAdapter(this,stringList);
        recyc.setAdapter(recycAdapter);
        /*
        *
        * recyclerView能实现很多效果就是因为有这个布局管理器 xxxManager  设置方向xxxManager.setOrientation()
        * 1.设置布局管理器
        * 2.方向
        * 3.recyclerView加载布局管理器
        *
        *
        * */
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyc.setLayoutManager(linearLayoutManager);

        /*
        *
        * 接口回调方式设置监听事件
        *
        * */
        recycAdapter.setOnViewClickListener(new RecycAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(int position) {
                Toast.makeText(MainActivity.this, "view"+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClickListener(int position) {
                Toast.makeText(MainActivity.this, "viewLong"+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTextClickListener(int position) {
                Toast.makeText(MainActivity.this, "text"+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTextLongClickListener(int position) {
                /*Toast.makeText(MainActivity.this, "textLong"+position, Toast.LENGTH_SHORT).show();*/
            }
        });



        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyc.setLayoutManager(linearLayoutManager);
                break;
            case R.id.btn2:
                 GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
                 gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                 recyc.setLayoutManager(gridLayoutManager);
                break;
            case R.id.btn3:
                /*
                *  瀑布流看不出效果
                *
                *  item不居中需要填wrap_content(也就是自适应)
                *
                * */
                StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                recyc.setLayoutManager(staggeredGridLayoutManager);
                break;
        }
    }
}

adapter

package com.example.iamchan.recyc;

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

import java.util.List;

/**
 * Created by iamchan on 2018/9/28.
 */

public class RecycAdapter extends RecyclerView.Adapter<RecycAdapter.MyViewHolder>{



    /*
    *
    * 另一种实现监听事件的实现方式采用接口回调的方式
    *
    * */
    public interface OnItemClickListener{
        public void onItemClickListener(int position);
        public void onItemLongClickListener(int position);
        public void onTextClickListener(int position);
        public void onTextLongClickListener(int position);
    }

    OnItemClickListener onItemClickListener;

    public void setOnViewClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
    /*
    *
    * 属性一般俩一个是数据源
    * 另一个是上下问对象
    *
    * */
    private Context context;
    private List<String> strings;
    /*
    *
    * 构造方法初始化数据
    *
    * */
    public RecycAdapter(Context context, List<String> strings) {
        this.context = context;
        this.strings = strings;
    }
    /*
    *
    *
    * 创建viewHolder
    * 这个主要作用就是找到item布局
    * 把布局里面有用的控件与Viewholder进行关联
    *
    *
    * */
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        return new MyViewHolder(view);
    }
    /*
    *
    *
    * 绑定viewholder
    * 主要是数据源与viewHolder的绑定
    * 这里可以实现监听事件
    *
    * */
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.textView.setText(strings.get(position));

        /*
        *
        * 监听事件
        *这是一种方式实现监听事件
        * */
       /* holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "view"+position, Toast.LENGTH_SHORT).show();
            }
        });
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "text"+position, Toast.LENGTH_SHORT).show();
            }
        });*/

        /*
        *
        * 接口回调方式实现监听事件
        *
        * */
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener!=null) {
                    onItemClickListener.onItemClickListener(position);
                }
            }
        });
        holder.view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onItemClickListener!=null) {
                    onItemClickListener.onItemLongClickListener(position);
                }
                return false;
            }
        });
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener!=null)
                onItemClickListener.onTextClickListener(position);
            }
        });

        holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onItemClickListener!=null)
                onItemClickListener.onTextLongClickListener(position);
                return false;
            }
        });






    }

    /*
    *
    * 获取数据源条数
    *
    * */
    @Override
    public int getItemCount() {
        return strings.size();
    }
    /*
    *
    * viewholder与布局中的控件关联
    * view代表一个item
    *
    * */
    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        View view;
        public MyViewHolder(View itemView) {
            super(itemView);
            view=itemView;
            textView=itemView.findViewById(R.id.text);

        }
    }
}

item

<?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="wrap_content">
    <TextView
        android:background="#000"
        android:textColor="#fff"
        android:layout_gravity="center"
        android:id="@+id/text"
        android:gravity="center"
        android:text="文字"
        android:textSize="18dp"
        android:layout_width="40dp"
        android:layout_height="80dp" />
    <View
        android:background="#e0e0e0"
        android:layout_width="match_parent"
        android:layout_height="2dp"></View>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/iamchan/article/details/82882334