我在片段中使用Viewpager,并且viewpager在其片段中有一个listview。因此,当单击列表项时,它将被重定向到同一活动中的另一个片段。所以当我回到我之前的片段(viewpager)时,viewpager是空白的!为什么会如此 我迫切需要解决方案。任何建议都将不胜感激。
//tabs inside the fragment
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
/* @Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PageFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return "TAB " + (position + 1);
}*/
@Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new Full_Roaster_Fragment();
break;
case 1:
frag = new By_Sport_Fragment();
break;
case 2:
frag = new By_Team_Fragment();
break;
case 3:
frag = new Video_Critique_Fragment();
break;
}
return frag;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
String title=" ";
switch (position){
case 0:
title="Full Roaster";
break;
case 1:
title="By Sport";
break;
case 2:
title="By Team";
break;
case 3:
title="For Video Critique";
break;
}
return title;
}
}
// Viewpager的子片段
public class Full_Roaster_Fragment extends Fragment implements AdapterView.OnItemClickListener {
private View rootView;
public static ListView Analystslist;
public static FullRoasterAdapter full_Roaster_adapter;
public static ArrayList<ics.com.ics_app.Beans.AnalystSearchBean.Teams> analysts_search_arraylist1 = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_full_roaster, container, false);
initialization();
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Full Roaster on Activity Created", Toast.LENGTH_SHORT).show();
// Populates list with our static array
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
Log.e("sizeee",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.size()));
}
private void initialization() {
Analystslist = (ListView)rootView.findViewById(R.id.Analysts_listview);
Analystslist.setOnItemClickListener(this);
}
@Override
public void onResume() {
super.onResume();
// if (ApplicationGlobalClass.flag) {
Toast.makeText(getActivity(), "full Roaster on Resume", Toast.LENGTH_SHORT).show();
full_Roaster_adapter = new
..
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ApplicationGlobalClass.user_id=ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId();
Log.e("userid",String.valueOf(ApplicationGlobalClass.user_id));
Log.e("userid2",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId()));
//here new fragment is calling from this fragment
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FlexibleSpaceWithImageWithViewPagerTab2Activity()).addToBackStack("profile1").commit();
}
答案 0 :(得分:0)
您需要覆盖托管片段的活动中的onBackPressed()
,如下所示:
public void onBackPressed() {
int = count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}