我正在使用选项卡式活动,其中有3个选项卡,其中一个选项卡包含列表的RecylerView,如果我点击列表,则应在选项卡式活动中打开新选项卡或片段
这是我的适配器类
public class tabAdapter extends RecyclerView.Adapter<tabAdapter.MyViewHolder> {
private LayoutInflater lf;
List<information> data = Collections.emptyList();
private Context context;
public tabAdapter(Context context,List<information> data){
this.context=context;
lf=LayoutInflater.from(context);
this.data=data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = lf.from(parent.getContext()).inflate(R.layout.res,parent,false);
MyViewHolder ms = new MyViewHolder(view);
return ms;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
information info=data.get(position);
holder.tx.setText(info.str);
holder.tm.setImageResource(info.img);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tx;
ImageView tm;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
tx = (TextView) itemView.findViewById(R.id.ids);
tm = (ImageView) itemView.findViewById(R.id.imag);
}
@Override
public void onClick(View view) {
context.startActivity(new Intent(context,tab3.class));
}
}
}
答案 0 :(得分:0)
在RecyclerView适配器中定义一个侦听器
并覆盖您的活动中的更改标签
public class tabAdapter extends RecyclerView.Adapter<tabAdapter.MyViewHolder> {
private LayoutInflater lf;
interface MyListener {
void changeTabs();
}
MyListener listener;
public void setListener(MyListener listener) {
this.listener = listener;
}
List<information> data = Collections.emptyList();
private Context context;
public tabAdapter(Context context, List<information> data) {
this.context = context;
lf = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = lf.from(parent.getContext()).inflate(R.layout.res, parent, false);
MyViewHolder ms = new MyViewHolder(view);
return ms;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
information info = data.get(position);
holder.tx.setText(info.str);
holder.tm.setImageResource(info.img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.changeTabs();
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tx;
ImageView tm;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
tx = (TextView) itemView.findViewById(R.id.ids);
tm = (ImageView) itemView.findViewById(R.id.imag);
}
}
}
在您的活动中覆盖,如下所示:
List<information> data = new ArrayList<>();
....
tabAdapter tabAdapter = new tabAdapter(data);
tabAdapter.setListener(new MyListener() {
@Override
public void changeTabs() {
// change tabs
}
});