Recyclerview在恢复活动后消失

时间:2017-01-06 07:43:59

标签: android android-recyclerview

这是一项名为HistoryOrdersActivity的活动。它的布局就像你想象的那样简单,内部只有一个Recyclerview。这是布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/orders_swipe">
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/history_order_recyclerview"/>
</android.support.v4.widget.SwipeRefreshLayout>

完整代码:

public class HistoryOrdersActivity extends BaseActivity implements IHistoryOrdersView,SwipeRefreshLayout.OnRefreshListener {
    private ILocalDishRepository localDishRepository;
    private IGetHistoryOrdersPresenter presenter;
    private RecyclerView historyOrders;
    private SwipeRefreshLayout swipeRefreshLayout;
    private HistoryOrderAdapter adapter;
    private static final String TAG=HistoryOrdersActivity.class.getSimpleName();

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_history_orders;
    }

    @Override
    protected void setUpView() {
        swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.orders_swipe);
        historyOrders=$(R.id.history_order_recyclerview);
        historyOrders.setLayoutManager(new LinearLayoutManager(this));
    }
    @Override
    public void setUpData(){
        localDishRepository=new LocalDishRepositoryImpl();
        presenter=GetHistoryOrdersPresenterImpl.getSingletonInstance(this);
        presenter.getHistoryOrdersByUserId(12);
    }


    @Override
    public void setUpOrders(List<Order> orders) {
        if(adapter==null){
            adapter=new HistoryOrderAdapter(orders,localDishRepository);
            historyOrders.setAdapter(adapter);
        }
        else adapter.setOrderList(orders);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        if(swipeRefreshLayout.isRefreshing()){
            Log.i(TAG,"swipe is refreshing");
            swipeRefreshLayout.setRefreshing(false);
        }
        ToastUtils.getInstance().makeToast("Orders fetched!");

    }

    @Override
    public void showError(String errorMsg) {

    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        this.finish();
    }

    @Override
    public void onRefresh() {
        presenter.getHistoryOrdersByUserId(12);
        //swipeRefreshLayout.setRefreshing(false);
    }


}

这是演讲者:

public class GetHistoryOrdersPresenterImpl implements IGetHistoryOrdersPresenter {
    private IRemoteOrderRepository remoteOrderRepository;
    private IHistoryOrdersView historyOrdersView;
    private Handler handler;
    private static GetHistoryOrdersPresenterImpl INSTANCE;

    private GetHistoryOrdersPresenterImpl(IHistoryOrdersView v){
        this.historyOrdersView=v;
        handler= new Handler(GluttonApplication.getInstance().getMainLooper());
        remoteOrderRepository=RemoteOrderRepository.getSingletonInstance(this);
    }

    public  static IGetHistoryOrdersPresenter getSingletonInstance(IHistoryOrdersView v){
        if(INSTANCE==null){
            INSTANCE=new GetHistoryOrdersPresenterImpl(v);
        }
        return INSTANCE;
    }
    @Override
    public void getHistoryOrdersByUserId(long userId) {
        handler.post(()->{
          historyOrdersView.showLoading();  
        });
        remoteOrderRepository.getHistoryOrdersByUserId(userId);
    }

    @Override
    public void getHistoryOrdersByUserIdSuccess(List<Order> ordersList) {
        handler.post(()->{
            historyOrdersView.hideLoading();
            historyOrdersView.setUpOrders(ordersList);
        });
    }

    @Override
    public void getHistoryOrdersByUserIdFail(String msg) {
        handler.post(()->{
            historyOrdersView.hideLoading();
            historyOrdersView.showError(msg);
        });
    }
}

这是适配器:

 public class HistoryOrderAdapter extends RecyclerView.Adapter<HistoryOrderAdapter.ViewHolder> {
        private List<Order> orderList;
        private ILocalDishRepository repository;
        public HistoryOrderAdapter(List<Order> orders, ILocalDishRepository repository){
            this.orderList=orders;
            this.repository=repository;
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v= LayoutInflater.from(GluttonApplication.getInstance())
                    .inflate(R.layout.history_order_item,parent,false);
            return new ViewHolder(v);
        }

        public void setOrderList(List<Order> orderList){
            this.orderList=orderList;
            this.notifyDataSetChanged();
        }


        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Order order=orderList.get(position);
            Map<Long,Integer> dishes=order.getDishes();
            List<String>names=dishes.keySet().stream()
                    .map((id)->repository.getName(id))
                    .collect(Collectors.toList());

            List<Integer> nums=dishes.values().stream().
                    collect(Collectors.toList());
            String time=order.getTime();
            long id=order.getId();
            StringBuilder builder=new StringBuilder();
            for(int i=0;i<names.size();i++){
                builder.append(names.get(i)+"*"+Long.toString(nums.get(i))+"\n");
            }
            holder.dishes.setText(builder.toString());
            holder.id.setText(Long.toString(id));
            holder.time.setText(time);

        }

        @Override
        public int getItemCount() {
            return orderList.size();
        }
        public class ViewHolder extends RecyclerView.ViewHolder{
            private TextView time,id,dishes;
            public ViewHolder(View itemView) {
                super(itemView);
                time=(TextView)itemView.findViewById(R.id.history_order_item_time);
                id=(TextView)itemView.findViewById(R.id.history_order_item_id);
                dishes=(TextView)itemView.findViewById(R.id.history_order_item_dishs);
            }
        }
    }

setOrderList,我打电话给notifyDataSetChanged,是的,它有效:

截图:

It works

然后我按回去跳转到另一个活动。当我回到HistoryOrdersActivity时。所有视图都消失了。但我仍然可以获取数据(我在数据到来时记录所有数据,并在活动获取数据时显示吐司)。 但除了吐司之外什么都没有显示出来。

截图:

Nothing shows

有人可以帮助我吗?我已经为此工作了将近一个小时。

1 个答案:

答案 0 :(得分:0)

您需要在adapter.notifyDataSetChanged();中致电OnResume

在包含“回收者”视图的活动中粘贴以下代码

  @Override
  public void onResume(){
    super.onResume();
    adapter.notifyDataSetChanged();
}