如何为每个选项卡刷新ViewPager?

时间:2014-01-06 17:14:48

标签: android tabs android-viewpager

我正在使用ViewPager作为主要布局,而对于单个寻呼机视图我使用Fragments。选项卡是Action Bar的一部分。 我想用我的操作栏中的刷新按钮刷新当前选项卡。

我的MainActivity.java

@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}


@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{     
    switch (menuItem.getItemId()) {


    case R.id.action_refresh:

         // code for the refresh             

        return true;

    default:
        this.finish();

        return super.onOptionsItemSelected(menuItem);
    }
    //startActivity(new Intent(this,PreviousActivity.class)); 
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_actions, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

这是我的标签之一 StartPage.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.startpage, container, false);
    return rootView;
}



@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

1 个答案:

答案 0 :(得分:2)

一种方法是在操作栏中找到所选选项卡,将其强制转换为类型,然后在其上调用方法。

这是您可能想要避免的依赖。

我更喜欢在这种情况下使用广播。在片段onResume方法中注册广播接收器,在onPause中注销它。然后让上下文广播一个刷新意图,片段就会这样做。

这将允许您从应用程序的任何位置刷新片段。即使刷新代码最终在后台运行的服务中也是如此。

更新

您可以在任何地方找到这样的代码(this是上下文):

LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
Intent i = new Intent(REFRESH_CONSTANT);
lbm.sendBroadcast(i);

然后在你的片段中你会收听这个广播:

public class MyFragment extends Fragment {

    MyReceiver r;

    public void refresh() {
        // Do the refresh
    }

    public void onPause() {
        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(r);
    }

    public void onResume() {
        r = new MyReceiver ();
        LocalBroadcastManager.getInstance(mContext).registerReceiver(r,
            new IntentFilter(REFRESH_CONSTANT));
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            MyFragment.this.refresh();
        }
    }
}