2017-07-26 66 views
-1

已更新:回收視圖不會消失

我想顯示「列表中沒有項目」時,實際上沒有任何內容顯示在碎片的回收站中。這就是我想:

在我的片段

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#f0f0f0" 
android:animateLayoutChanges="true"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/assigned_recycler" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="50dp" 
    android:background="#f0f0f0" 
    android:animationCache="true" 
    android:animateLayoutChanges="false" /> 

<TextView 
    android:id="@+id/empty_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:visibility="gone" 
    android:text="@string/no_data_available" /> 

在我的片段的Java文件

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_assigned_orders, container, false); 


    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.assigned_recycler); 


    mRecyclerView.setHasFixedSize(true); 




    mLayoutManager = new LinearLayoutManager(this.getContext()); 
    mRecyclerView.setLayoutManager(mLayoutManager); 


    mAdapter = new AssignedOrdersListAdapter(orderService.getAssignedOrders()); 
    mAdapter.setManager(getFragmentManager()); 
    mRecyclerView.setAdapter(mAdapter); 
    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
    toolbar.setTitle("Assigned"); 

    emptyViewText = (TextView)rootView.findViewById(R.id.empty_view); 


    new LegDistanceTask().execute(""); 
    return rootView; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    getActivity().registerReceiver(receiver, new IntentFilter("OrderUpdate")); 

    int count = mAdapter.getItemCount(); 
    if(count==0){ 

     mRecyclerView.setVisibility(View.GONE); 
     emptyViewText.setVisibility(View.VISIBLE); 
    } 
} 

我失去了一些東西在這裏?

它也必須做我的佈局代碼和顏色?

+0

理想情況下,您不應該在onViewCreated中執行此操作,因爲您的recyclerview狀態可能會根據您如何獲取數據而經常更改。所以你應該在獲得數據後再做。如果它的服務調用然後在後臺執行異步任務。再加上您是否也爲回收視圖查找了viewviewid?它不在代碼 –

+0

嗨Kapsym,我應該把它放在哪裏?在調用onViewCreatred之前,我已驗證計數確實返回0,服務調用成功完成。我也試過把它放在片段的Resume上。 – Ackman

+0

檢查您是否正在更改代碼中其他位置的可見性。我只是試了一下,似乎工作正常 –

回答

2

請嘗試下面的代碼在onCreateView

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_assigned_orders, container, false); 

     mRecyclerView = (RecyclerView) rootView.findViewById(R.id.assigned_recycler); 

     mRecyclerView.setHasFixedSize(true); 

     mLayoutManager = new LinearLayoutManager(this.getContext()); 
     mRecyclerView.setLayoutManager(mLayoutManager); 


     Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
     toolbar.setTitle("Assigned"); 
     emptyViewText = (TextView)rootView.findViewById(R.id.empty_view); 

     if(orderService.getAssignedOrders().size() != 0) { 
      mRecyclerView.setVisibility(View.VISIBLE); 
      emptyViewText.setVisibility(View.GONE); 
      mAdapter = new AssignedOrdersListAdapter(orderService.getAssignedOrders()); 
      mAdapter.setManager(getFragmentManager()); 
      mRecyclerView.setAdapter(mAdapter); 
     } else { 
      mRecyclerView.setVisibility(View.GONE); 
      emptyViewText.setVisibility(View.VISIBLE); 
     } 

     new LegDistanceTask().execute(""); 
     return rootView; 
    } 

上面的代碼將檢查是否從服務接收到的列表爲空或不併相應地顯示組件。

您應該檢查列表大小來確定,而不是適配器計數。

+0

是的。這工作。一定是我使用getItemCount()邏輯的方式。或條件陳述。 – Ackman

+0

快樂編碼.. :) – codeWorm

+0

非常感謝您的幫助:) – Ackman