You use RecyclerView to create a list display in Android application development. When you set up a button on RecyclerView and transition from a list to another screen, you may want to know ** what number of buttons in the list was pressed **. On RecyclerView.Adapter, "what number button was pressed" can be obtained fairly quickly, but I was a little addicted to using it in MainActivity, so make a note.
For an overview, please refer to this article because it was easy to understand. Basics of RecyclerView
--I want to place a button on RecyclerView and change the screen when the button is pressed. --I want to change the behavior at the transition destination depending on the number of lines in the list where the button is pressed.
The image of the processing when the button at the time of screen transition is pressed is like this. I would like to tell you which list of buttons was pressed for the transition destination screen.
By the way, RecyclerView.Adapter does not inherit the Activty class, so screen transition is not possible. Therefore, it is necessary to know the number of the list pressed on MainActivity and change the screen.
MainActivity
@Override
public void onClick(View view) {
int line = getLine(); //I want to get the number of rows in the list I pressed somehow
Intent hoge = new Intent(MainActivity.this, HogeActivity.class);
hoge.putExtra("line", line);
startActivity(hoge);
}
I want to process on MainActivity when I press the button included in RecyclerView. Register OnClickListener of MainActivity in the listener of RecyclerView.Adapter. Now you can set the listener so that OnClick () on MainActivty is called when you press a button on the list.
RecyclerAdapter.java
public void setOnItemClickListener(View.OnClickListener listener){
m_listener = listener;
}
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final int pos = position;
//When the button on the list is pressed, the click listener registered above will be called.
holder.m_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m_listener.onClick(view);
}
});
}
MainActivity.java
RecyclerView.Adapter rAdapter = new RecycleAdapter(data);
rAdapter.setOnItemClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int line = getLine(); //I want to get the number of rows in the list I pressed somehow
Intent hoge = new Intent(MainActivity.this, HogeActivity.class);
hoge.putExtra("line", line);
startActivity(hoge);
}
});
In the current state, the same process will be performed no matter which list of buttons you press.
RecyclerView.Adapter has a method called ʻonBindViewHolder. This is the method that will be called each time a list item is created. And the argument
position` represents the exact number of the list created.
** Implement the value of this position to be registered in the member variable of RecyclerView in the click listener of the button on the list. ** ** ** By doing this, when the button is pressed, the value of the number of lines will be registered in the member variable (m_line) of RecycleViewAdapter. ** **
Then create a method that can refer to that m_line
from the outside, and implement it so that it is called in OnClick () of MainActivity.
After that, it is OK if you get the value with HogeActivity
of the transition destination and change the processing accordingly.
RecyclerAdapter.java
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final int pos = position;
holder.m_btn.setOnClickListener(new View.OnClickListener() { 
@Override
public void onClick(View view) {
m_line = pos; //Register the number of lines
m_listener.onClick(view); //Call Main OnClick immediately after registration
}
});
}
public int getLine(){
return m_line; //Get the number of lines
}
MainActivity.java
final RecyclerView.Adapter rAdapter = new RecycleAdapter(list);
recyclerView.setAdapter(rAdapter);
((RecycleAdapter) rAdapter).setOnItemClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int line = ((RecycleAdapter) rAdapter).getLine(); //Here m_Get the value of line
Intent edit = new Intent(MainActivity.this, HogeActivity.class);
edit.putExtra("line", line);
startActivity(edit);
}
});
It's kind of a very roundabout implementation, I could only find this method to tell the Main Activity the number of lines I pressed, so I ended up with this implementation. If there is something like "No, if you do hogehoge without doing that, you'll get one shot ...", please teach me.
Since the code is extracted from the application I made and modified a little, the variable names in the text and the source code may not match. Please convert it in the brain.
Recommended Posts