我有android选项卡的问题,当我第一次加载时它完全加载,但是当我转到第三个选项卡并再次返回第一个选项卡时,所有数据都消失了?
这是我的标签片段。
package com.autoxtion.autoconnect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by NKCSL0015 on 9/23/2016.
*/
public class HomeActivity extends Fragment {
View root;
FrameLayout simpleFrameLayout;
private View categoryView;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.heart,
R.drawable.user,
R.drawable.heart
};
public HomeActivity() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = inflater.inflate(R.layout.home_content, null);
// toolbar = (Toolbar) root.findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) root.findViewById(R.id.viewpager);
setupViewPager(viewPager);
//viewPager.setCurrentItem(2, false);
tabLayout = (TabLayout) root.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
return root;
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabOne.setText("Vehicle");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.car, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabTwo.setText("Promotion");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.promotiontab1, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabThree.setText("Request");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.request_tab, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
TextView tabFour = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabFour.setText("Appointment");
tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.appointment, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFour);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(this.getChildFragmentManager());
adapter.addFrag(new VehicleFragmentTab(), "Vehicle");
adapter.addFrag(new PromotionFragment(), "Promotion");
adapter.addFrag(new RequestFragment(), "Request");
adapter.addFrag(new AppointmentFragment(), "Appointment");
viewPager.setAdapter(adapter);
if(RequestEditActivity.requestEditFlag==1){
viewPager.setCurrentItem(2);
}
if(AvailActivity.addpromotionrequest==1){
viewPager.setCurrentItem(2);
}
if(RequestAdapter.deleteflag==1){
RequestAdapter.deleteflag = 0;
viewPager.setCurrentItem(2);
}
if(VehcleAdapter.deleteVehicle == 1){
VehcleAdapter.deleteVehicle = 0;
viewPager.setCurrentItem(0);
}
/* if(CreateRequest.addrequestflag==1){
viewPager.setCurrentItem(2);
}*/
if(CreateRequest.successfullyadd==1){
viewPager.setCurrentItem(2);
}
if(VehicleDetails.updatesuccessfully==1){
viewPager.setCurrentItem(0);
}
if(AddVehicle.successfullyaddvehicle==1){
viewPager.setCurrentItem(0);
}
}
@Override
public void onResume() {
super.onResume();
for (Fragment fragment : getFragmentManager().getFragments()) {
if (fragment instanceof VehicleFragmentTab || fragment instanceof PromotionFragment || fragment instanceof RequestFragment || fragment instanceof AppointmentFragment) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
}
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
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);
}
}
}