gpt4 book ai didi

java - 单击错误调用使用回收器 View 适配器的项目列表?

转载 作者:行者123 更新时间:2023-11-29 19:28:44 24 4
gpt4 key购买 nike

我有一个包含三个 fragment 的 Activity ,每个 fragment 都有一个单独的 RecyclerView 和一堆项目。在 fragment 之间滚动时,onClick() 会混淆,点击 fragment 1 中的某个项目会转到相同的位置,但点击 fragment 3 中的项目会进入错误的屏幕。我是否应该在我设置的自定义 onClick() 方法中区分页面?所有 fragment 都使用相同的适配器,所以我不确定这是否是罪魁祸首?不确定什么代码在这里有用,但这是我的适配器类:

public class ExploreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static OnEntryClickListener mOnEntryClickListener;
private List<Explore> exploreList;
private List<String> trackedProjects;
private Context context;
private Typeface typeFace, italicTypeface, boldTypeface;
private int typeOfExplore;
private HashMap<String, Boolean> uniqueExploreItems;
private HashMap<String, Long> offSet;
private int firstSet, secondSet;
private boolean apiProcessing;

private String stringValue;
//firebase
private DatabaseReference mDatabase;

public void changeTracked(List<String> tracks) {
trackedProjects = tracks;
notifyDataSetChanged();
}


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView projectTitle, projectCompany;
public ImageView projectPicture;
private ShineButton projectTrack;


public MyViewHolder(View view) {
super(view);
projectTitle = (TextView) view.findViewById(R.id.exploreProjectTitle);
projectCompany = (TextView) view.findViewById(R.id.exploreProjectCompany);
projectPicture = (ImageView) view.findViewById(R.id.exploreProjectPicture);
projectTrack = (ShineButton) view.findViewById(R.id.exploreProjectTrack);


view.setOnClickListener(this);
projectTrack.setOnClickListener(this);

}

@Override
public void onClick(View v) {
if (mOnEntryClickListener != null) {
mOnEntryClickListener.onEntryClick(v, getAdapterPosition());

}
}

}



public ExploreAdapter(Context mContext, List<Explore> explores, List<String> trackedProj, int typeOfE, String strV, HashMap<String, Boolean> uniqueE, HashMap<String, Long> offs, Typeface myTypeface, Typeface myTypefaceItalic, Typeface myTypefaceBold) {
context = mContext;
exploreList = explores;
typeFace = myTypeface;
italicTypeface = myTypefaceItalic;
boldTypeface = myTypefaceBold;
typeOfExplore = typeOfE;
uniqueExploreItems = uniqueE;
offSet = offs;
stringValue = strV;

apiProcessing = true;
mDatabase = FirebaseDatabase.getInstance().getReference();
trackedProjects = trackedProj;

}

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

return new MyViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.explore_item, parent, false));


}


public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
mOnEntryClickListener = onEntryClickListener;
}

public interface OnEntryClickListener {
void onEntryClick(View view, int position);
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder myViewHolder = (MyViewHolder) holder;
Explore proj = exploreList.get(position);
myViewHolder.projectTitle.setTypeface(boldTypeface);
myViewHolder.projectCompany.setTypeface(italicTypeface);
myViewHolder.projectTitle.setText(proj.getProjectTitle());
myViewHolder.projectCompany.setText(proj.getSubTitle());

//for updates
if(typeOfExplore == 1) {
String createImagePath = Constants.PHASES_IMAGE + proj.getProjectPicture();
Picasso.with(context).load(createImagePath).placeholder(R.drawable.default_liovinci_bg).resize(320, 240).into(myViewHolder.projectPicture);

} else {
String createImagePath = Constants.PROJECT_IMAGE + proj.getProjectPicture();
Picasso.with(context).load(createImagePath).placeholder(R.drawable.default_liovinci_bg).resize(320, 240).into(myViewHolder.projectPicture);

}

if(typeOfExplore == 1 || typeOfExplore == 2) {

myViewHolder.projectTrack.setVisibility(View.GONE);
} else {
if(trackedProjects != null && trackedProjects.contains(proj.getProjectId())) {

myViewHolder.projectTrack.setColorFilter(ContextCompat.getColor(context, R.color.actionBlue), PorterDuff.Mode.SRC_IN);
myViewHolder.projectTrack.setChecked(true);
} else {

myViewHolder.projectTrack.setColorFilter(ContextCompat.getColor(context, R.color.bottomBarGray), PorterDuff.Mode.SRC_IN);


}

}

最佳答案

如果您使用的是 View 寻呼机,那么它会初始化您绑定(bind)到该 View 寻呼机的所有 fragment 。所以最后它会初始化最后一个 fragment ,你的适配器类将包含来自你最后一个初始化 fragment 的上下文。

这就是原因。所以要克服你的问题,你应该为不同的 fragment 制作不同的适配器。

关于java - 单击错误调用使用回收器 View 适配器的项目列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689354/

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