我正在使用https://github.com/astuetz/PagerSlidingTabStrip/库。我的活动由3个“标签”组成,每个标签都有自己的片段。
问题在于,每当我加载标签3时,标签1中的所有数据都将丢失,并且不会刷新。
所有3个片段都填充了listview并具有类似的代码。
我无法弄清楚为什么会一直发生,但似乎每当我加载一个不是当前所选标签的直接“邻居”的标签时,当前选择的标签都会被卸载。
基本上,我有3个标签。如果我加载标签3,标签1卸载并且不重新加载。标签2始终保持填充(我假设标签2始终是标签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 [Tab 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 [Tab 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 [Tab 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;
}
}
感谢您的帮助。
答案 0 :(得分:0)
事实证明,在我实际收到结果之前,我正在通知我的列表视图它的数据集已经改变。
解决方案:将@AfterViews带注释的方法#init()中的“dataChanged()”行移动到@Background带注释的方法#getOrders()。