我有一个仪表板,其中包含四个标签,可以说A,B,C和D.在每个片段中,我有另外两个子片段,比方说A - (a,b),B - (a,b), C - (a,b)和D - (a,b)。当我启动应用程序时,它会加载所有碎片。现在,如果我将数据从片段A传递到片段B,如果我单击片段B则不加载。只有当我点击标签A并点击标签B时,才会加载页面。
public class Dashboard extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout,tablayout_top;
private ViewPager viewPager;
public List<Task_data> tasklist = new ArrayList<>();
private TaskFragmentAdapter tadapter;
private final Task_main_fragment mfragment0;
public Dashboard() {
mfragment0 = null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
toolbar = (Toolbar) findViewById(R.id.toolbar_home_dashboard);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager_home_dashboard);
setupViewPager(viewPager);
final int PAGE = 0;
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (viewPager.getCurrentItem() == PAGE) {
viewPager.setCurrentItem(PAGE - 1, false);
viewPager.setCurrentItem(PAGE, false);
return true;
}
return false;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 2) {
//do your Staff
Toast.makeText(Dashboard.this, "You are currently working on snags page", Toast.LENGTH_SHORT).show();
//now I need to refresh the fragments in onpage selected page.
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
/* viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(final int i, final float v, final int i2) {
}
@Override
public void onPageSelected(final int i) {
YourFragmentInterface fragment = (YourFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, i);
if (fragment != null) {Toast.makeText(Dashboard.this, "you are on task page", Toast.LENGTH_SHORT).show();
fragment.fragmentBecameVisible();
}
}
@Override
public void onPageScrollStateChanged(final int i) {
}
});*/
tabLayout = (TabLayout) findViewById(R.id.tabs_home_dashboard);
tabLayout.setupWithViewPager(viewPager);
/* for(int i=0;i<tabLayout.getTabCount();i++){
TabLayout.Tab tab =tabLayout.getTabAt(i);
RelativeLayout relativeLayout=(RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_layout,tabLayout,false);
tab.setCustomView(relativeLayout);
tab.select();
}*/
setupTabIcons();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_dashboard,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Refresh_dashboard:
Toast.makeText(getApplicationContext(), "The page is about to get refresh", Toast.LENGTH_SHORT).show();
//getRefresh(); Iam calling a m
break;
case R.id.Account:
startActivity(new Intent(getApplicationContext(), Profile.class));
return true;
case R.id.Homepage_dashboard:
startActivity(new Intent(getApplicationContext(),homepage.class));
return true;
case R.id.sync_dashboard:
startActivity(new Intent(getApplicationContext(),sync.class));
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("Home");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.schld, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("Schedule");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.schld, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("Tasks");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.taskimg, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
TextView tabfour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabfour.setText("Drafts");
tabfour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.cloud, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabfour);
/* TextView tabfive = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabfive.setText("Sync");
tabfive.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.cloud, 0, 0);
tabLayout.getTabAt(4).setCustomView(tabfive);*/
/* TextView tabfive = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabfive.setText("More");
tabfive.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_action_help, 0, 0);
tabLayout.getTabAt(4).setCustomView(tabfive);*/
}
/**
* Adding fragments to ViewPager
* @param viewPager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new A(),"ONE");
adapter.addFrag(new B(),"TWO");
adapter.addFrag(new C(), "THREE");
adapter.addFrag(new D(), "FOUR");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
这是我的片段A代码,其中包含另外两个片段a,b
public class B extends Fragment {
private TabLayout tabLayout,tablayout_top;
private ViewPager viewPager;
View view;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
/* @Bind(R.id.title) TextView name;
@Bind(R.id.job_title) TextView jobTitle;*/
public Schedule_Fragment(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_schedule__fragment,container, false);
super.onCreate(savedInstanceState);
ButterKnife.inject(this, view);
viewPager =ButterKnife.findById(view,R.id.viewPager_top_schedule);
setupViewPager(viewPager);
TabLayout tabLayout = ButterKnife.findById(view,R.id.tabs_top_home_schedule);
tabLayout.setupWithViewPager(viewPager);
/* setupTabicons();*/
return view;
}
private void setupTabicons() {
TextView tabOne = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabOne.setText("Snags");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabTwo.setText("Desnags");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_call, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapterTop adaptertop = new ViewPagerAdapterTop(getChildFragmentManager());
adaptertop.addFragment(new a(),"Snags");
adaptertop.addFragment(new b(),"Desnags");
viewPager.setAdapter(adaptertop);
}
class ViewPagerAdapterTop extends FragmentPagerAdapter{
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public ViewPagerAdapterTop(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
现在,当我点击片段B时,这两个片段a,b没有加载。当我们点击片段A然后回到片段B时,只加载片段B.请帮助我,因为我是Android的新手。