我正在尝试在RecyclerView中突出显示一个项目,类似于在ListView中设置选定的项目。
目前,我已经设置了RecyclerView,有一个默认的LayoutManager,并且有一个显示所有数据的适配器。最近我刚刚让onClickListener()
起作用(虽然我不确定它应该放在onCreateViewHolder()
还是onBindViewHolder()
- 不确定onBindViewHolder()
何时被调用)。
我已经尝试搜索过,最接近的是这里的问题。然而,我完全迷失在那个问题中。那个onClick()
方法在哪里(在适配器内部、在包含活动/片段内、等等)?那个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;
}
}
我希望能得到任何帮助。谢谢。