android:按钮点击后如何刷新标签

时间:2015-12-08 05:02:41

标签: android android-viewpager

我的页面上有一个按钮和下面的标签。我想要单击按钮后打开的选项卡反映可变数据的变化。我无法理解它。

以下是我的代码: -

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 also refresh the current tab

            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

下面的行也可以,但是如果它不起作用,那么你也可以尝试另一个选项

  pagerAdapter.notifydatasetChanged();

尝试以下代码: -

     buttonId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


 pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
        viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);

            }
        });

注意: - 当您获得新数据时,重新定义您的标题和Numoftabs

答案 1 :(得分:0)

您无法通知适配器中的数据集吗?

pagerAdapter.notifyDatasetChanged()