在选项卡之间切换时不显示数据

Data not being displayed when switching between tabs

我正在使用 https://github.com/astuetz/PagerSlidingTabStrip/ 库。我的 activity 由 3 个 "tabs" 组成,每个选项卡都有自己的片段。

问题是每当我加载选项卡 3 时,选项卡 1 中的所有数据都会丢失并且不会被刷新。

所有 3 个片段都填充了一个列表视图并且具有相似的代码。

我不明白为什么它一直发生,但似乎每当我加载一个不是当前选定选项卡的直接 "neighbour" 的选项卡时,当前选定的选项卡就会被卸载。

基本上,我有 3 个选项卡。如果我加载选项卡 3,选项卡 1 将卸载并且不会重新加载。选项卡 2 始终保持填充状态(我假设是因为选项卡 2 始终是 a] 选项卡 1 的邻居或 b] 选项卡 3 的邻居)。

OrdersActivity.java

package nl.*.android.order;


    import android.content.Intent;
    import android.support.v4.app.Fragment;
    import android.support.v4.view.ViewPager;

    import com.astuetz.PagerSlidingTabStrip;

    import org.androidannotations.annotations.AfterViews;
    import org.androidannotations.annotations.EActivity;
    import org.androidannotations.annotations.ViewById;

    import nl.*.android.DrawerActivity;
    import nl.*.android.home.HomeActivity_;
    import nl.*.android.R;
    import nl.*.android.utility.IFragmentSetter;
    import nl.*.android.utility.TabsPagerAdapter;

    @EActivity(R.layout.activity_orders)
    public class OrdersActivity extends DrawerActivity implements IFragmentSetter {

    @ViewById
    public ViewPager viewpager;
    @ViewById
    public PagerSlidingTabStrip strip;

    private TabsPagerAdapter adapter;

    @AfterViews
    public void init() {
        buildDrawer();
        adapter = new TabsPagerAdapter(getSupportFragmentManager(), this);
        addFragments();
        viewpager.setAdapter(adapter);
        strip.setViewPager(viewpager);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Bestellingen");
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white);
        }
    }

    private void addFragments() {
        adapter.addFragment(new OrdersNewFragment_(), "Nieuw");
        adapter.addFragment(new OrdersDeliveryFragment_(), "Afleveren");
        adapter.addFragment(new OrdersPickupFragment_(), "Ophalen");
    }

    @Override
    public void setFragment(Fragment fragment) {
        //geen implementatie nodig
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        final Intent i = HomeActivity_.intent(getApplicationContext()).get();
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
        finish();
    }
}

OrdersNewFragment.java [选项卡 1]

package nl.*.android.order;

    import android.support.v4.app.Fragment;
    import android.widget.ListView;

    import org.androidannotations.annotations.AfterViews;
    import org.androidannotations.annotations.Background;
    import org.androidannotations.annotations.Bean;
    import org.androidannotations.annotations.EFragment;
    import org.androidannotations.annotations.UiThread;
    import org.androidannotations.annotations.ViewById;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    import nl.*.android.R;
    import nl.*.android.backend.API;
    import nl.*.android.backend.APIFactory;
    import nl.*.android.model.MOrderItem;
    import nl.*.android.model.MOrders;

    @EFragment(R.layout.fragment_orders_overview)
    public class OrdersNewFragment extends Fragment {

        @Bean
        public APIFactory apiFactory;

        private API api;

        private List<MOrderItem> items = new ArrayList<>();
        private OrderListAdapter adapter;

        @ViewById
        public ListView ordersList;

        @AfterViews
        public void init() {
            api = apiFactory.get();
            getNewOrders();
            adapter = new OrderListAdapter(getActivity(), R.id.ordersList, items);
            ordersList.setAdapter(adapter);
            dataChanged();
        }

        @UiThread
        public void dataChanged() {
            adapter.notifyDataSetChanged();
        }

        @Background
        public void getNewOrders() {
            items.clear();
            final MOrders result = api.getOrdersForNew();
            if (result != null) {
                Collections.addAll(items, result.result.orderList);
            }
        }

    }

OrdersDeliveryFragment.java [选项卡 2]

package nl.*.android.order;

import android.support.v4.app.Fragment;
import android.widget.ListView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import nl.*.android.R;
import nl.*.android.backend.API;
import nl.*.android.backend.APIFactory;
import nl.*.android.model.MOrderItem;
import nl.*.android.model.MOrders;

@EFragment(R.layout.fragment_orders_delivery)
public class OrdersDeliveryFragment extends Fragment {

    @Bean
    public APIFactory apiFactory;

    private API api;

    private List<MOrderItem> items = new ArrayList<>();
    private OrderListAdapter adapter;

    @ViewById
    public ListView ordersList;

    @AfterViews
    public void init() {
        api = apiFactory.get();
        getDeliveryOrders();
        adapter = new OrderListAdapter(getActivity(), R.id.ordersList, items);
        ordersList.setAdapter(adapter);
        dataChanged();
    }

    @UiThread
    public void dataChanged() {
        adapter.notifyDataSetChanged();
    }

    @Background
    public void getDeliveryOrders() {
        items.clear();
        final MOrders result = api.getOrdersForDelivery();
        if (result != null) {
            Collections.addAll(items, result.result.orderList);
        }
    }

}

OrdersPickupFragment.java [选项卡 3]

package nl.*.android.order;

import android.support.v4.app.Fragment;
import android.widget.ListView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import nl.*.android.R;
import nl.*.android.backend.API;
import nl.*.android.backend.APIFactory;
import nl.*.android.model.MOrderItem;
import nl.*.android.model.MOrders;

@EFragment(R.layout.fragment_orders_pickup)
public class OrdersPickupFragment extends Fragment {

    @Bean
    public APIFactory apiFactory;

    private API api;

    private List<MOrderItem> items = new ArrayList<>();
    private OrderListAdapter adapter;

    @ViewById
    public ListView ordersList;

    @AfterViews
    public void init() {
        api = apiFactory.get();
        getPickupOrders();
        adapter = new OrderListAdapter(getActivity(), R.id.ordersList, items);
        ordersList.setAdapter(adapter);
        dataChanged();
    }

    @UiThread
    public void dataChanged() {
        adapter.notifyDataSetChanged();
    }

    @Background
    public void getPickupOrders() {
        items.clear();
        final MOrders result = api.getOrdersForPickup();
        if (result != null) {
            Collections.addAll(items, result.result.orderList);
        }
    }

}

OrderListAdapter.java

package nl.****.android.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import nl.*.android.R;
import nl.*.android.model.MOrderItem;

/**
 * @author Niels
 * @version 1.0
 * @since 4-9-2015
 */
public class OrderListAdapter extends ArrayAdapter<MOrderItem> {

    private List<MOrderItem> itemList = new ArrayList<>();
    private Context context;
    private OrdersActivity activity;
    private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.FRENCH);

    public OrderListAdapter(Context context, int resource, List<MOrderItem> orderList) {
        super(context, resource, orderList);
        this.itemList = orderList;
        this.context = context;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.list_single, parent, false);
        MOrderItem item = itemList.get(position);
        TextView label = (TextView) view.findViewById(R.id.txtKlant);
        TextView txt = (TextView) view.findViewById(R.id.txtDate);
        label.setText(item.name);
        txt.setText(FORMATTER.format(item.placeDate));
        return view;
    }

}

感谢所有帮助。

原来我是在通知我的列表视图它的数据集在我实际完成获取结果之前发生了变化。

解决方法:将@AfterViews注解方法#init()中的"dataChanged()"行移动到@Background注解方法#getOrders()中。