Recyclerview 不显示或滚动片段
Recyclerview doesn't show or scroll in fragment
我在这里发现了一些关于此问题的问题,但其中 none 帮助解决了我的问题。 Recycler 视图工作正常,但棒棒糖版本存在问题。它在 activity 中完美运行,但在片段内部使用时不显示或滚动。我的片段屏幕显示为空白,但回收器视图适配器中的点击侦听器正在工作。当我将片段代码迁移到 activity 时,它运行良好。我正在使用 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);
}
}
}
该死的!在这个问题上卡了整整两天后,我终于找到了解决方案。这是因为我在 activity 中添加片段时使用了 FragmentTransaction.TRANSIT_FRAGMENT_FADE
。删除过渡解决了我的问题
我在这里发现了一些关于此问题的问题,但其中 none 帮助解决了我的问题。 Recycler 视图工作正常,但棒棒糖版本存在问题。它在 activity 中完美运行,但在片段内部使用时不显示或滚动。我的片段屏幕显示为空白,但回收器视图适配器中的点击侦听器正在工作。当我将片段代码迁移到 activity 时,它运行良好。我正在使用 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);
}
}
}
该死的!在这个问题上卡了整整两天后,我终于找到了解决方案。这是因为我在 activity 中添加片段时使用了 FragmentTransaction.TRANSIT_FRAGMENT_FADE
。删除过渡解决了我的问题