Change the processing when the button on RecyclerView is pressed for each list

Introduction

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.

About RecyclerView

For an overview, please refer to this article because it was easy to understand. Basics of RecyclerView

Thing you want to do

--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);                                                              
}    

First of all

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);  
    }
});    

Main subject

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);
    }
});

Finally

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.

At the very end

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

Change the processing when the button on RecyclerView is pressed for each list
[Swift] Setting the behavior when the Share button is pressed
How to change the process depending on the list pressed when there are multiple ListViews
[Android] Easily control the event when the Back button is pressed in Fragment
[swift] How to control the behavior when the back button of NavigationBar is pressed
Resize or hide Button and EditText placed on the screen for each page
How to perform a specific process when the back button is pressed in Android Fragment
Change the injection target for each environment with Spring Boot 2
[Rails] When the layout change of devise is not reflected
Change the setting value for each environment with Digdag (RubyOnRails)
Behavior when each is executed in the reverse order Range
[Android] Change the app name and app icon for each Flavor
What is the constructor for?
Countermeasures for forgetting to specify the font when titleTextAttributes is specified
When the processing after conditional branching is redundant-12 [C # refactoring sample]
[Rails] How to change the page title of the browser for each page
Change the conversation depending on which day of the week is today
Change the layout when rotating with onConfigurationChanged
[Swift] Setting the behavior when the Share button is pressed