android_recyclerView适配器写法

效果图

android_recyclerView适配器写法_第1张图片

layout



    
    
    
        

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 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{



    /*
    *
    * 另一种实现监听事件的实现方式采用接口回调的方式
    *
    * */
    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 strings;
    /*
    *
    * 构造方法初始化数据
    *
    * */
    public RecycAdapter(Context context, List 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



    
    

你可能感兴趣的:(android_recyclerView适配器写法)