我正在使用MaterialtTabHost和ViewPager,我希望每当我切换到新标签时,都应该自动更新新数据。
这是我的活动,其中我为每个ViewPager添加了标签和viewPager,我有一个不同的片段来显示。
public class FrontPage_Activity extends AppCompatActivity implements MaterialTabListener {
ViewPager mPager;
StorageUtil storageUtil;
User user;
MaterialTabHost tabHost;
Toolbar toolbar;
MyPageAdapter myPageAdapter;
@Override
protected void onResume() {
super.onResume();
for (int i = 0; i < myPageAdapter.getCount(); i++) {
tabHost.addTab(tabHost.newTab().setText(myPageAdapter.getPageTitle(i)).setTabListener(this));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_page_);
storageUtil = new StorageUtil(this);
storageUtil.setUserStatus(1);
user = storageUtil.getUser();
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (user != null)
actionBar.setTitle("Welcome " + user.getName());
mPager = (ViewPager) findViewById(R.id.pager);
tabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
tabHost.setSelectedNavigationItem(position);
}
});
myPageAdapter = new MyPageAdapter(getSupportFragmentManager());
mPager.setAdapter(myPageAdapter);
/*for (int i = 0; i < myPageAdapter.getCount(); i++) {
tabHost.addTab(tabHost.newTab().setText(myPageAdapter.getPageTitle(i)).setTabListener(this));
}*/
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
} else if (position == 1) {
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onTabSelected(MaterialTab tab) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(MaterialTab tab) {
}
@Override
public void onTabUnselected(MaterialTab tab) {
}
class MyPageAdapter extends FragmentPagerAdapter {
String tabs[] = getResources().getStringArray(R.array.tabs);
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public Fragment getItem(int position) {
return setFragment(position);
}
@Override
public int getCount() {
return 3;
}
}
private Fragment setFragment(int number) {
if (number == 0) {
Chats chats = new Chats();
return chats;
} else if (number == 1) {
Request frag = new Request();
return frag;
} else {
Invite frag = new Invite();
return frag;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.chatting_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.logout:
storageUtil.clear();
startActivity(new Intent(this, MainActivity.class));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
moveTaskToBack(true);
}
}
答案 0 :(得分:0)
首先,这可能有点困难,一种方法也有不同的结果,但最终有一些方法可以进行测试。
我通常使用的一种方法是覆盖 getItemPosition 。
在您的自定义适配器( MyPageAdapter
)中,覆盖 getItemPosition ,如下所示:
class MyPageAdapter extends FragmentPagerAdapter {
...
@Override
public int getItemPosition(Object object) {
if (object instanceof Chats ) {
((Chats )object).updateMyLogicOrView();
}
if (object instanceof Request) {
((Request)object).updateMyLogicOrView();
}
if (object instanceof Invite) {
((Invite)object).updateMyLogicOrView();
}
return super.getItemPosition(object);
}
..
}
然后,在属于其自己的标签的每个片段上,您创建 updateMyLogicOrView()
。在该方法中,您放下了一些您希望在视图上生成新数据/更新数据的操作。当您移动到选项卡时,将始终通过overriden方法调用此 updateMyLogicOrView()。
// put this method on each fragment class
public void updateMyLogicOrView(){
// do some logics here..
// like component.setText()
// or, adding some new elements to adapter..
}