Recyclerview不显示或滚动片段

时间:2019-08-20 05:34:05

标签: android android-layout android-fragments android-recyclerview android-5.1.1-lollipop

我在这里找到了有关此问题的一些问题,但是没有一个问题可以解决我的问题。回收站视图工作正常,但棒棒糖版本中存在问题。它在活动中完美运行,但是在片段中使用时不会显示或滚动。我的片段屏幕显示为空白,但在回收者视图的适配器中的监听程序正在运行。当我将片段代码迁移到活动中时,它工作正常。我正在使用28.0.0,这是我的xml代码。

<?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>

我的商品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>

长片段代码

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

1 个答案:

答案 0 :(得分:0)

该死!在这个问题上呆了整整两天后,我终于找到了解决方案。这是因为我在活动中添加片段时使用了FragmentTransaction.TRANSIT_FRAGMENT_FADE。删除过渡解决了我的问题