Android:如何在点击按钮时更新在viewpager内部的片段内的recyclerview

时间:2015-12-09 04:53:30

标签: android android-viewpager android-recyclerview

我在一个位于viewpager内部的片段中有一个recyclerview。我在viewpager外面有按钮。如何在单击按钮时更新recyclerview。

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;

            }
        });
    }
}

viewpager的tab 0中的framgment代码,我使用的是recyclerview,如下所示:

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

1 个答案:

答案 0 :(得分:0)

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

HERE你会看到界面的工作,试试这个希望它会正常工作