gpt4 book ai didi

java - 如何在 recyclerView 的适配器中调用 scrollToPosition(position)?

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:15 24 4
gpt4 key购买 nike

我的 onBindViewHolder 中有一个 setOnClick 监听器

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
((ViewHolder) holder).txtType.setText(object.text);
((ViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
//???.scrolltoPosition(position+1);
}
});

单击时,我会将视口(viewport)滚动到下一个回收器 View 项目。我通过 recyclerView.scrollToPosition(position); 在我的 MainActivity java 中完成了这个。但是在 onBindViewHolder 上的适配器类中,我不知道如何在主 Java 类上调用我的 recyclerView。

我应该怎么做?

编辑:出于简化目的,我修改了之前的代码,因为我为我的回收器使用了多 View 适配器。这是多 View 适配器:

public class MultiViewTypeAdapter extends RecyclerView.Adapter {

private ArrayList<InstructionModel>dataSet;
Context mContext;
int total_types;
private LinearLayoutManager manager;

public MultiViewTypeAdapter(LinearLayoutManager manager)
{
this.manager=manager;
}


public static class SimpleTextViewHolder extends RecyclerView.ViewHolder {

TextView txtType;
CardView cardView;
CheckBox checkBox;

public SimpleTextViewHolder(View itemView) {
super(itemView);

this.txtType = (TextView) itemView.findViewById(R.id.type);
this.cardView = (CardView) itemView.findViewById(R.id.card_view);
this.checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);

}
}
public static class TimeTextViewHolder extends RecyclerView.ViewHolder {

TextView txtType;
CardView cardView;
CheckBox checkBox;

public TimeTextViewHolder(View itemView) {
super(itemView);

this.txtType = (TextView) itemView.findViewById(R.id.type);
this.cardView = (CardView) itemView.findViewById(R.id.card_view);
this.checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
}
}




public MultiViewTypeAdapter(ArrayList<InstructionModel> data, Context context) {
this.dataSet = data;
this.mContext = context;
total_types = dataSet.size();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view;
switch (viewType) {
case InstructionModel.SIMPLE_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple, parent, false);
return new SimpleTextViewHolder(view);
case InstructionModel.TIME_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.time, parent, false);
return new TimeTextViewHolder(view);
}
return null;
}

@Override
public int getItemViewType(int position) {

switch (dataSet.get(position).type) {
case 0:
return InstructionModel.SIMPLE_TYPE;
case 1:
return InstructionModel.TIME_TYPE;
default: return -1;
}
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

InstructionModel object = dataSet.get(listPosition);
if (object != null) {
switch (object.type) {

case InstructionModel.SIMPLE_TYPE:

((SimpleTextViewHolder) holder).txtType.setText(object.text);
break;
case InstructionModel.TIME_TYPE:
((TimeTextViewHolder) holder).txtType.setText(object.text);

break;
}
}



}

@Override
public int getItemCount() {
return dataSet.size();
}

附言如果你看到一些随机无用的东西,我正在尝试一些东西。

最佳答案

使用构造函数传递 LayoutManager

 class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private LinearLayoutManager manager;

public Adapter(LinearLayoutManager manager)
{
this.manager=manager;
}


@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
((ViewHolder) holder).txtType.setText(object.text);
((ViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
manager.scrollToPosition(position+1);
}
});
}

关于java - 如何在 recyclerView 的适配器中调用 scrollToPosition(position)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48376471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com