Restart the layout

Asked

Viewed 190 times

5

I made two buttons that set Adapter in recyclerview, each arrow button a different layout, but only doing so

findViewById(R.id.layout2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            recyclerView adapter = new recyclerView(R.layout.listlayout);
            RecyclerView rv = findViewById(R.id.rv);
            adapter.notifyDataSetChanged();
            rv.swapAdapter(adapter,true);
            rv.setLayoutManager(new LinearLayoutManager(v.getContext()));

        }
    });

didn’t work, he only arrow once, in case you want to change the layout it doesn’t change, I tried too

Intent main = new Intent(MainActivity.this,MainActivity.class);
finish();
startActivity(main);

But in doing so, he arrow the Adapter and reboot, thus losing the Adapter. I wonder if anyone has another way to do that.

2 answers

7


use instead of swapAdapter(adapter, true) the setAdapter(adapter)

4

In place of:

rv.swapAdapter(adapter,true);

use:

adapter.notifyDataSetChanged();
rv.setAdapter(adapter);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.