android recyclerview

Posted ForMeDream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android recyclerview相关的知识,希望对你有一定的参考价值。

package com.example.administrator.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.List;

public class myadapt extends RecyclerView.Adapter {
    private List<String> list;
    private Context mcontext;
    private String Tag="Tag";

    public myadapt(List<String> list, Context context) {
        this.list = list;
        this.mcontext = context;
    }

    public  interface OnItemClickListener{
        void onItemClick(View v,int pos);
    }

//设置监听器接口,mainactvity 实现接口
private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener listener){ onItemClickListener=listener; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { LayoutInflater inflater = LayoutInflater.from(mcontext); return new myholder(inflater.inflate(R.layout.item,null),i); } @Override public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) { if(viewHolder instanceof myholder){ final myholder holder=(myholder)viewHolder; holder.getTv().setText(list.get(i)); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onItemClickListener.onItemClick(viewHolder.itemView,i); } }); } } @Override public int getItemCount() { return list.size(); } class myholder extends RecyclerView.ViewHolder { private TextView tv; private int pos; public myholder(View itemView,int pos) { super(itemView); this.pos=pos; Log.i("Tag","myholder"); tv = (TextView)itemView.findViewById(R.id.tv); } public int getPos(){return pos;} public TextView getTv(){ return tv; } } }

 

以上是关于android recyclerview的主要内容,如果未能解决你的问题,请参考以下文章

RecyclerView holder中的Android Google Maps动态片段

Android:以编程方式在片段中添加多个 RecyclerView

RecyclerView 内容未使用片段父级的全宽

我们如何在 android 的 RecyclerView 片段中使用 bottomSheet?

多个 RecyclerView 不在同一个片段中工作

android studio中片段内的RecyclerView使我的应用程序崩溃