Ackman Ackman - 1 year ago 63
Android Question

recycle view is not going away

UPDATED:

I want to display "no items in the list" when there is actually nothing to display in a recyclerview in a fragment. This is what I am trying:

in my fragment:

<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" />




in my fragment's java file:

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


Am I missing something here?

Also does it have to do something with my layout code and color?

Answer Source

Please try the below code in 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;
    }

The above code will check whether the list received from service is empty or not and display the components accordingly.

You should be checking the list size to determine, not the adapter count.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download