单击项目时保持列表视图状态

时间:2014-09-03 17:08:37

标签: android listview android-fragments android-listview

我有一个片段(父),其中包含一个带有两个tab的tabhost。每个tab都是一个包含listview的片段。当第一次访问父片段时,两个listview片段加载正常,如下所示。

enter image description here  enter image description here

问题是当点击listview项目的任何一个时。在被重定向到被点击的项目细节之后,按下后​​退按钮时,两个列表视图为空并且不再显示。这就是我的意思 enter image description here

我想在点击项目之前显示列表视图。我如何实现这一目标?建议将受到高度赞赏。 两个片段中每个片段的代码:

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);
}

}

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 这是因为生命周期。按下后退按钮后,它将转到 onResume()。 不要 onCreateView()。这是你的问题。 将一些代码移动到onResume(),例如 listview.setAdpater(adp)。 我希望这会对你有所帮助。

答案 1 :(得分:0)

创建一个类并在其中声明一个静态ArrayList。将所有数据存储在其中并通过它填充ListView。数据将持续到应用程序运行的时间。这种方法可以解决您的问题。