I'm having trouble with highlighted an item within a RecyclerView, similar to setting the selected item in a ListView.
At the moment, I've set up up the RecyclerView, have a default LayoutManager, and have an adapter which displays all the data. I've just recently got the onClickListener()
working (although I'm not sure if it should go in the onCreateViewHolder()
or onBindViewHolder*(
- not sure when onBindViewHolder()
is called).
I've tried searching around, and the closest I've gotten is the question here. However, I'm completely lost in that question. Where is that onClick()
method (inside the adapter, inside the containing Activity/fragment, etc?). What is that viewHolderListener
? What is getPosition()
from and what does it do exactly? Essentially, I've gotten nowhere from that question, and it was the best resource I could find so far.
Here is my current code for setting up the 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);
}
Here is my current code for the 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;
}
}
I would appreciate any help. Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…