接口回调通过在adapter里面来实现;
下面是recyclerview的简单adapter:
package com.example.zhoukao_zidingyiyuan_moredownload.adapter;
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 com.example.zhoukao_zidingyiyuan_moredownload.R;
import com.example.zhoukao_zidingyiyuan_moredownload.bean.Bean;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
/**
* 作者:author
* 时间 :2017/11/18:10:32
* 说明:
*/
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.MyViewHolder> implements View.OnClickListener {
Context context;
List<Bean.DataBean> list;
private OnItemClickListener mOnItemClickListener=null;
public static interface OnItemClickListener{
void onItemClick(View view,int positon);
}
public RvAdapter(Context context, List<Bean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.items,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
view.setOnClickListener(this);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
DraweeController controller= Fresco.newDraweeControllerBuilder()
.setUri(list.get(position).getImage_url())
.setAutoPlayAnimations(true)
.build();
holder.sdv.setController(controller);
holder.tv.setText(list.get(position).getTitle());
holder.itemView.setTag(position);
}
public void onClick(View v){
if(mOnItemClickListener!=null){
mOnItemClickListener.onItemClick(v,(int)v.getTag());
}
}
public void setOnItemClickListener (OnItemClickListener listener) {
this.mOnItemClickListener=listener;
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
SimpleDraweeView sdv;
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
sdv=itemView.findViewById(R.id.sdv);
tv=itemView.findViewById(R.id.tv);
}
}
}
在activity里面可以实现点击条目的接口回调
rvAdapter.setOnItemClickListener(new RvAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int positon) {
String urlmovie = list.get(positon).getVedio_url();
Toast.makeText(MoreActivity.this,urlmovie,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MoreActivity.this,DownloadActivity.class);
intent.putExtra("urlmovie",urlmovie);
startActivity(intent);
}
});
网友评论