我正在使用标签式活动来显示三个标签 问题是,第一个选项卡总是空白,而第二个和第三个选项卡工作。如果我点击第三个,然后点击第一个,第一个加载数据
public class Main2Activity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public static class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a Movies_Fragment (defined as a static inner class below).
Movies_Fragment fragment = Movies_Fragment.newInstance(position );
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Most Popular";
case 1:
return "Top Rated";
case 2:
return "Top Rated 2";
}
return null;
}
}
}
public class Movies_Fragment extends Fragment {
public static ImageAdapter imageAdapter;
public static Movies_Fragment newInstance(int sectionNumber) {
Movies_Fragment fragment = new Movies_Fragment();
Bundle args = new Bundle();
args.putInt("index", sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onStart() {
super.onStart();
updateMovies();
//imageAdapter.notifyDataSetChanged();
}
public void updateMovies() {
FetchMovieTask movieTask = new FetchMovieTask();
Bundle bundle = getArguments();
int index = bundle.getInt("index");
movieTask.execute(index);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies_, container, false);
imageAdapter = new ImageAdapter(getActivity());
GridView gridView = (GridView)rootView.findViewById(R.id.gridView);
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MDMovie movie = (MDMovie) imageAdapter.getItem(i);
Intent intent = new Intent(getActivity(),movieDetailsActivity.class);
intent.putExtra("movie", movie);
startActivity(intent);
}
});
return rootView;
}
}
答案 0 :(得分:1)
这也发生在我的一个项目中,我也无法对其进行诊断。我最终使用了这个[脏]黑客。到目前为止它不是解决方案,但它对我们有用。我们基本上在开始时模拟页面更改:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(1);
mViewPager.setCurrentItem(0);
}