1

I have a recyclerview inside a fragment which is inside viewpager. I have button outside viewpager. How to update the recyclerview upon clicking the button.

public class Level2MainActivity_grid1 extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.level2mainlayout_place1);



        // I am putting a viewpager with fragments into them and put tabs

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
        viewPager.setAdapter(pagerAdapter);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);



        Button buttonId = (Button) findViewById(R.id.planmytraveludupitown);
        buttonId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            //I want to save some things and do the changes in the recyclerview of tab 0;

            }
        });
    }
}

The framgment code in tab 0 of the viewpager, in which i am using recyclerview is as below:

public class Udupiwhattosee extends Fragment {

    private RecyclerView rv;
    private List<Udupiwhattoseearray> udupiwhattoseearray;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        RecyclerView v =(RecyclerView) inflater.inflate(R.layout.tab_1,container,false);
        v.setLayoutManager(new LinearLayoutManager(getActivity()));
        v.setHasFixedSize(true);
        initializeData();
        WhattoseeRVAdapter adapter = new WhattoseeRVAdapter(udupiwhattoseearray,container.getContext());
        v.setAdapter(adapter);
        return v;
    }


    private void initializeData(){
        udupiwhattoseearray = new ArrayList<>();
        udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Sri Krishna Temple", "Udupi, Karanata,INDIA", R.drawable.udupi));
        udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 2", "Udupi, Karanata,INDIA", R.drawable.udup_krishna_math_gopura_udupi));
        udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Place 3", "Udupi, Karanata,INDIA", R.drawable.udupi_rath));
        udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 4", "Udupi, Karanata,INDIA", R.drawable.udupi_window));
    }
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • You can use a flag, like `isRecyclerViewNeedUpdated` to indicate if your `RecyclerView` need to be updated. When button is clicked, set this flag to true. Use `ViewPage#addOnPageChangeListener(...)` so that when your target `Fragment` is selected, check flag and update `RecyclerView` if needed. – Lym Zoy Feb 15 '17 at 10:14

1 Answers1

0
  recycler_view.post(new Runnable() {
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });

HERE you will see the working of interface ,try this hope its gonna work fine

Vikas Rana
  • 26
  • 7