androidandroid-layoutandroid-fragmentsandroid-recyclerviewandroid-5.1.1-lollipop

Recyclerview doesn't show or scroll in fragment


I have found some questions regarding this issue here but none of them helped to solve my problem. Recycler view works fine but there is issue in lollipop version. It works perfectly in an activity but doesn't show or scroll when used inside fragment. My fragment screen appears blank but the tap listeners in recycler view's adapter is working. When I migrated my fragment code to an activity, it worked fine. I am using 28.0.0 Here is my xml code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/toolbar" />

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeToRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/initial_layout_empty_view" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_order"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:nestedScrollingEnabled="false" />

    </FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>

My item xml

  <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/wholeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtVendorName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/colorTextListTitle"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    tools:text="Redsun Family Restaurant" />

                <TextView
                    android:id="@+id/txtVendorAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/colorTextNormal"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    tools:text="KumariPati,Lalitpur,Nepal" />

            </LinearLayout>

            <ImageView
                android:id="@+id/imageCompleted"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_check_green"
                android:visibility="gone" />

            <Button
                android:id="@+id/btnDeliveryStatus"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:background="@drawable/circular_btn_background_dark_green"
                android:backgroundTint="@color/colorPrimary"
                android:text="Accept"
                android:textColor="@color/white"
                android:textSize="12sp"
                android:visibility="gone" />
        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp">

            <include layout="@layout/layout_seperator" />
        </FrameLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:orientation="horizontal">

            <include
                android:id="@+id/included"
                layout="@layout/layout_vendor_logo" />

            <View
                android:id="@+id/catLogoDivider"
                android:layout_width="0.7dp"
                android:layout_height="match_parent"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:background="#e8e8e8" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:orientation="vertical">

                <RelativeLayout
                    android:id="@+id/relOrderNo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp">

                    <TextView
                        android:id="@+id/txtLblOrderNo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:singleLine="true"
                        android:text="@string/txtOrderNo"
                        android:textColor="@color/colorTextNormal" />

                    <TextView
                        android:id="@+id/txtOrderNo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_centerVertical="true"
                        android:layout_toEndOf="@+id/txtLblOrderNo"
                        android:gravity="end"
                        android:maxLines="2"
                        android:textColor="@color/black"
                        tools:text="#16-0131-102716" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/relDeliveryTime"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp">

                    <TextView
                        android:id="@+id/txtLblDeliveryTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:singleLine="true"
                        android:text="@string/txtDeliveryTime"
                        android:textColor="@color/colorTextNormal" />

                    <TextView
                        android:id="@+id/txtDeliveryTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_centerVertical="true"
                        android:layout_toEndOf="@+id/txtLblDeliveryTime"
                        android:gravity="end"
                        android:maxLines="2"
                        android:textColor="@color/black"
                        tools:text="Jul 5, 2017, 7:15pm" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="32dp"
                    android:layout_marginTop="10dp">

                    <TextView
                        android:id="@+id/txtLblStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_centerVertical="true"
                        android:gravity="center"
                        android:text="@string/txtStatus"
                        android:textColor="@color/colorTextNormal" />

                    <TextView
                        android:id="@+id/txtStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_centerVertical="true"
                        android:gravity="end"
                        android:maxLines="2"
                        android:textSize="14sp"
                        android:textColor="@color/black"
                        tools:text="Processing" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp">

                    <TextView
                        android:id="@+id/txtTotalAmount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_centerInParent="true"
                        android:text="@string/txtTotalAmount"
                        android:textColor="@color/colorTextNormal" />

                    <TextView
                        android:id="@+id/txtTotalAmountValue"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_centerVertical="true"
                        android:textColor="@color/black"
                        tools:text="Rs. 197.50" />
                </RelativeLayout>


            </LinearLayout>
        </LinearLayout>

    </LinearLayout>


</android.support.v7.widget.CardView>

And the long fragment code

public class OrderFragment extends MvpFragment<OrderView, OrderListPresenter> implements OrderView, OnItemClickListener {
private View view;
private Toolbar mToolbar;
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private List<Order> orderList = new ArrayList<>();
private OrderAdaptor adaptor;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_order, container, false);
    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    findViews();
    setActionBar();
    initRecyclerView();
    initSwipeToRefresh();
    if (getActivity() != null)
        getActivity().setTitle(getString(R.string.titleOrders));
    presenter.getOrderData();
}

@Override
public OrderListPresenter createPresenter() {
    return new OrderListPresenter(getContext());
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home)
        if (getActivity() != null) {
            ((MainActivity) getActivity()).openDrawerLayout();
        }
    return super.onOptionsItemSelected(item);
}

@Override
public void setAdaptor(List<Order> orderList) {
    hideLoading();
    this.orderList.clear();
    this.orderList.addAll(orderList);
    adaptor.notifyDataSetChanged();
}

@Override
public void showLoading() {
    swipeRefreshLayout.setRefreshing(false);
    if (getActivity() != null)
        ProgressDialogFactory.getInstance(getActivity()).show();
}

@Override
public void initSwipeToRefresh() {
    swipeRefreshLayout.setOnRefreshListener(() -> {
        showLoading();
        presenter.getOrderData();

    });
}

@Override
public void onItemClicked(int position) {
    // Don't navigate if order is completed
    if (!orderList.get(position).getStatusId().equals(Constants.ORDER_STATUS_DELIVERED))
        GlobalUtils.navigateActivityWithMultipleData(getContext(), false, OrderDetailActivity.class, R.anim.slide_in_left, R.anim.no_change, orderList.get(position).getOrderId(), orderList.get(position).getStatusDisplay());
}

private void findViews() {
    mToolbar = view.findViewById(R.id.mToolbar);
    recyclerView = view.findViewById(R.id.recyclerView_order);
    swipeRefreshLayout = view.findViewById(R.id.swipeToRefresh);
}

private void initRecyclerView() {
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setHasFixedSize(true);
    recyclerView.setVisibility(View.VISIBLE);
    adaptor = new OrderAdaptor(orderList, this);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setAdapter(adaptor);
    recyclerView.setNestedScrollingEnabled(false);
}

private void setActionBar() {
    if (getActivity() == null)
        return;
    setHasOptionsMenu(true);
    ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
    ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
    }
}
}

Solution

  • God Damn! After being stuck in this issue for 2 whole days, I finally found the solution. It was because I was using FragmentTransaction.TRANSIT_FRAGMENT_FADE when adding fragment inside activity. Removing the transition solved my problem