我有一個片段(父),它包含一個帶有兩個選項卡的tabhost。每個選項卡是一個包含listview的片段。當第一次訪問父片段時,兩個listview片段加載好,如下所示。當項目被點擊時維護列表視圖狀態
問題是,當eaither的ListView項是clicked.After被重定向到被點擊的項目的詳細信息,在按下後退按鈕兩個列表視圖是空的,不再displayed.Here是我意思是
我想顯示列表視圖就像他們之前我點擊一個item.How我可以實現這一點?建議將高度讚賞。 爲每兩個片段的代碼:
public class AvailableDriversFragment extends Fragment implements
OnItemClickListener, OnScrollListener {
ProgressDialog progressDialog;
ArrayList<Driver> adverts;
TextView loadingmore;
// products JSONArray
JSONArray products = null;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_COUNT = "count";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_FNAME = "first_name";
private static final String TAG_ONAME = "other_names";
private static final String TAG_BRIEF = "description";
private static final String TAG_ID = "driver_id";
private static final String TAG_LOCATION = "location";
private static final String TAG_PHONE = "phone";
private static final String TAG_CLASSA = "classA";
private static final String TAG_CLASSB = "classB";
private static final String TAG_CLASSC = "classC";
private static final String TAG_CLASSD = "classD";
private static final String TAG_CLASSE = "classE";
// Bundle key names
private static final String KEY_ID = "advert_id";
private static final String KEY_LOCATION = "advert_location";
private static final String KEY_NAME = "advert_name";
private static final String KEY_BRIEF = "advert_brief";
private static final String KEY_PHONE = "advert_phone";
ListView driverslist;
Driver advert;
boolean loadingMore;
int currentPage;
int itemCount;
public AvailableDriversFragment() {
advert = new Driver();
loadingMore = false;
currentPage = 0;
itemCount = 0;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View rootView = inflater.inflate(R.layout.fragment_availabledrivers,
container, false);
driverslist = (ListView) rootView
.findViewById(R.id.listView_availabledrivers);
loadingmore = (TextView) rootView
.findViewById(R.id.availabledrivers_textView_more);
if (savedInstanceState != null) {
currentPage = savedInstanceState.getInt("currentPage");
itemCount = savedInstanceState.getInt("itemCount");
Log.i("currentPage", "" + currentPage);
driverslist.setAdapter(new DriversListAdapter(getActivity(),
adverts));
} else {
new GetDrivers().execute();
}
driverslist.setOnItemClickListener(this);
driverslist.setOnScrollListener(this);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Fragment detailsFragment = new DriverDetailsFragment();
advert = (Driver) parent.getItemAtPosition(position);
int advert_id = advert.getId();
String advert_location = advert.getLocation();
String advert_brief = advert.getBrief();
String advert_name = advert.getName();
Bundle bundle = new Bundle();
bundle.putInt(KEY_ID, advert_id);
bundle.putString(KEY_LOCATION, advert_location);
bundle.putString(KEY_NAME, advert_name);
bundle.putString(KEY_BRIEF, advert_brief);
detailsFragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity()
.getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detailsFragment)
.addToBackStack("latest").commit();
}
}
第二個片段:
public class DriverJobsFragment extends Fragment implements
OnItemClickListener, OnScrollListener {
ProgressDialog progressDialog;
ArrayList<CarItem> adverts;
TextView loadingmore;
// products JSONArray
JSONArray products = null;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_COUNT = "count";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_HEADING = "tag";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_PRICE = "price";
private static final String TAG_ID = "item_id";
private static final String TAG_LOCATION = "location";
private static final String TAG_PHOTO = "photo";
private static final String TAG_MODEL = "model";
private static final String TAG_MAKE = "make";
private static final String TAG_REGISTRATION = "registration";
private static final String TAG_CATEGORY = "category";
private static final String TAG_TRANSMISSION = "transmission";
private static final String TAG_FUEL = "fuel";
private static final String TAG_ENGINE = "engine_size";
private static final String TAG_PHONE = "phone";
// Bundle key names
private static final String KEY_DESCRIPTION = "advert_description";
private static final String KEY_ID = "advert_id";
private static final String KEY_RATE = "advert_rate";
private static final String KEY_LOCATION = "advert_location";
private static final String KEY_FUEL = "advert_fuel";
private static final String KEY_ENGINE = "advert_engine";
private static final String KEY_MODEL = "advert_model";
private static final String KEY_MAKE = "advert_make";
private static final String KEY_TRANSMISSION = "advert_transmission";
private static final String KEY_REGISTRATION = "advert_registration";
private static final String KEY_CATEGORY = "advert_category";
private static final String KEY_PHONE = "advert_phone";
ListView jobslist;
CarItem advert;
boolean loadingMore;
int currentPage;
int itemCount;
public DriverJobsFragment() {
advert = new CarItem();
loadingMore = false;
currentPage = 0;
itemCount = 0;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View rootView = inflater.inflate(R.layout.fragment_driverjobs,
container, false);
jobslist = (ListView) rootView.findViewById(R.id.listView_driverjobs);
loadingmore = (TextView) rootView
.findViewById(R.id.driverjobs_textView_more);
if (savedInstanceState != null) {
currentPage = savedInstanceState.getInt("currentPage");
itemCount = savedInstanceState.getInt("itemCount");
Log.i("currentPage", "" + currentPage);
jobslist.setAdapter(new CarsListAdapter(getActivity(), adverts));
} else {
new GetDriverJobs().execute();
}
jobslist.setOnItemClickListener(this);
jobslist.setOnScrollListener(this);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Fragment detailsFragment = new DriverJobDetailsFragment();
advert = (CarItem) parent.getItemAtPosition(position);
int advert_id = advert.getId();
String advert_description = advert.getDescription();
String advert_location = advert.getLocation();
String advert_price = advert.getPrice();
String advert_engine = advert.getEngineSize();
String advert_model = advert.getModel();
String advert_make = advert.getMake();
String advert_transmission = advert.getTransmission();
String advert_fuel = advert.getFuel();
String advert_registration = advert.getRegistration();
String advert_category = advert.getCategeory();
String advert_phone = advert.getPhone();
Bundle bundle = new Bundle();
bundle.putString(KEY_DESCRIPTION, advert_description);
bundle.putInt(KEY_ID, advert_id);
bundle.putString(KEY_RATE, advert_price);
bundle.putString(KEY_LOCATION, advert_location);
bundle.putString(KEY_ENGINE, advert_engine);
bundle.putString(KEY_MODEL, advert_model);
bundle.putString(KEY_MAKE, advert_make);
bundle.putString(KEY_TRANSMISSION, advert_transmission);
bundle.putString(KEY_FUEL, advert_fuel);
bundle.putString(KEY_REGISTRATION, advert_registration);
bundle.putString(KEY_CATEGORY, advert_category);
bundle.putString(KEY_PHONE, advert_phone);
detailsFragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity()
.getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detailsFragment)
.addToBackStack("jobs_details").commit();
}
}
和主機片段:
public class DriversFragment extends Fragment implements OnTabChangeListener,
OnPageChangeListener {
private ViewPager mViewPager;
private DriverTabsAdapter mAdapter;
private TabHost mTabHost;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_drivers, container,
false);
mViewPager = (ViewPager) view.findViewById(R.id.drivers_pager);
// Tab Initialization
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup();
DriversFragment.AddTab(getActivity(), this.mTabHost, this.mTabHost
.newTabSpec("jobs").setIndicator("Driver Jobs"));
DriversFragment.AddTab(getActivity(), this.mTabHost, this.mTabHost
.newTabSpec("drivers").setIndicator("Drivers"));
mAdapter = new DriverTabsAdapter(getActivity()
.getSupportFragmentManager());
// Fragments and ViewPager Initialization
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(this);
return view;
}
// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
}
@Override
public void onPageSelected(int arg0) {
}
// Method to add a TabHost
private static void AddTab(Activity activity, TabHost tabHost,
TabHost.TabSpec tabSpec) {
tabSpec.setContent(new MyTabFactory(activity));
tabHost.addTab(tabSpec);
}
}
顯示你所做的一些代碼 – tyczj 2014-09-03 17:11:29