android中RecyclerView的简单使用(三),android系统开发视频教程

import java.util.ArrayList;

import java.util.List;

import android.content.Context;

import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.RecyclerView.ViewHolder;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnLongClickListener;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Viewholder>{

private Listlist;

private Context ctx;

private ListmHeight;

private onItemClickListerner listener;//自己写的回调接口

public MyAdapter(List list, Context ctx) {

this.list=list;

this.ctx=ctx;

mHeight=new ArrayList();

for (int i = 0; i < list.size(); i++) {

mHeight.add((int) (150+Math.random()*300));

}

}

class Viewholder extends ViewHolder{

TextView mTv;

public Viewholder(View itemView) {

super(itemView);

mTv=(TextView) itemView.findViewById(R.id.mTv);

}

}

@Override

public int getItemCount() {

return list.size();

}

@Override

public void onBindViewHolder(final Viewholder arg0, final int arg1) {

LayoutParams lp = arg0.mTv.getLayoutParams();

lp.height=mHeight.get(arg1);

arg0.mTv.setLayoutParams(lp);

arg0.mTv.setText(list.get(arg1));

if(listener!=null){

arg0.itemView.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

int position = arg0.getAdapterPosition();

// int pos=arg0.getAdapterPosition();

listener.onItemClick(arg0.itemView, position);

}

});

arg0.itemView.setOnLongClickListener(new OnLongClickListener() {

@Override

public boolean onLongClick(View v) {

int pos=arg0.getAdapterPosition();

listener.onLongItemClick(arg0.itemView, pos);

return false;

}

});

}

}

@Override

public Viewholder onCreateViewHolder(ViewGroup arg0, int arg1) {

View v = View.inflate(ctx, R.layout.item, null);

return new Viewholder(v);

}

public interface onItemClickListerner{

void onItemClick(View view,int position);

void onLongIandroid中RecyclerView的简单使用(三),android系统开发视频教程
temClick(View view,int position);

}

public void setOnItemClickListener(onItemClickListerner listener){

this.listener=listener;

}

public void RemoveView(int position){

list.remove(position);

notifyItemRemoved(position);

}

}

这个是适配器的代码。关键的代码如下:

mHeight=new ArrayList();

for (int i = 0; i < list.size(); i++) {

mHeight.add((int) (150+Math.random()*300));

适配器里面有一个布局文件,比较简单,只有一个textview,代码就不贴了。

实现随机高度,下面给出mainActivity中的代码:

package cn.lzl.pubuliu;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.os.Bundle;
有一个布局文件,比较简单,只有一个textview,代码就不贴了。

实现随机高度,下面给出mainActivity中的代码:

package cn.lzl.pubuliu;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.os.Bundle;

上一篇:进程 7.多进程实现文件夹copy


下一篇:Frida Hook Java