gpt4 book ai didi

android - 突出显示 RecyclerView 中的选定项目

转载 作者:可可西里 更新时间:2023-11-01 19:12:36 24 4
gpt4 key购买 nike

我在 RecyclerView 中突出显示项目时遇到问题,类似于在 ListView 中设置选定项目。

目前,我已经设置了 RecyclerView,有一个默认的 LayoutManager,还有一个显示所有数据的适配器。我最近刚刚让 onClickListener() 工作(虽然我不确定它是否应该进入 onCreateViewHolder()onBindViewHolder*(- 不确定何时调用 onBindViewHolder()

我尝试四处搜索,我得到的最接近的是 question here .但是,我完全迷失了那个问题。 onClick() 方法在哪里(在适配器内部、包含的 Activity/fragment 等内部?)。 viewHolderListener 是什么? getPosition() 的来源是什么?它的具体作用是什么?从本质上讲,我对这个问题一无所知,这是迄今为止我能找到的最好的资源。

这是我当前设置 RecyclerView 的代码:

// Sets up the main navigation
private void setupMainNav() {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mMainRecyclerNav.setHasFixedSize(true);

// use a linear layout manager
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mMainRecyclerNav.setLayoutManager(layoutManager);

// Create and set the adapter for this recyclerView
MainNavAdapter adapter = new MainNavAdapter(getActivity());
mMainRecyclerNav.setAdapter(adapter);
}

这是我当前的适配器代码:

class MainNavAdapter extends RecyclerView.Adapter<MainNavAdapter.ViewHolder> {
Context mContext;

// Holds the titles of every row
String[] rowTitles;

// Default constructor
MainNavAdapter(Context context) {
this.mContext = context;

// Get the rowTitles - the necessary data for now - from resources
rowTitles = context.getResources().getStringArray(R.array.nav_items);
}

// Simple class that holds all the views that need to be reused
class ViewHolder extends RecyclerView.ViewHolder{
View parentView; // The view which holds all the other views
TextView rowTitle; // The title of this item

// Default constructor, itemView holds all the views that need to be saved
public ViewHolder(View itemView) {
super(itemView);

// Save the entire itemView, for setting listeners and usch later
this.parentView = itemView;

// Save the TextView- all that's supported at the moment
this.rowTitle = (TextView) itemView.findViewById(R.id.row_title);
}
}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// Create the view for this row
View row = LayoutInflater.from(mContext)
.inflate(R.layout.list_navmain_row, viewGroup, false);

// Create a new viewHolder which caches all the views that needs to be saved
ViewHolder viewHolder = new ViewHolder(row);

return viewHolder;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
viewHolder.rowTitle.setText(rowTitles[i]);

// TODO: Make a better workaround for passing in the position to the listener
final int position = i;



// Set a listener for this entire view
viewHolder.parentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked on row: " + position, Toast.LENGTH_SHORT).show();
}
});
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return rowTitles.length;
}
}

如果有任何帮助,我将不胜感激。谢谢。

最佳答案

RecyclerView 不像 ListView 那样处理项目选择或状态。相反,您必须在 View 持有者中手动处理它。

您可以做的第一件事是在 `ViewHolder 构造函数中将项目 View 声明为可点击:

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

// Make this view clickable
itemView.setClickable(true);

// ...
}

然后如果你想突出显示选择,你必须在你的适配器(例如使用索引列表)和你的 onBindViewHolder 方法中跟踪选定的行:

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int i) {
// mark the view as selected:
viewHolder.parentview.setSelected(mSelectedRows.contains(i));
}

作为旁注,您应该在 onCreateViewHolder 而不是 onBindViewHolder 的父 View 上设置 onClickListener。 onBindViewHolder 方法将针对同一 View 持有者多次调用,您将执行不必要的操作

关于android - 突出显示 RecyclerView 中的选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390682/

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