提问人:le Soup 提问时间:6/23/2023 更新时间:6/23/2023 访问量:14
为什么 RecyclerView 没有显示在实际应用程序中?
Why does the RecyclerView not show up in the actual app?
问:
我一直在开发一个 android studios 应用程序,其中我有一个片段,我想在其中显示一个 ImageView,并在其下方显示一个 RecyclerView。问题是 RecyclewView 没有显示在实际应用程序中。如果 XML 文件中只有 RecyclerView,而不在 ImageView 中,则可以看到 RecyclerView。
下面是 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/deliveryMapImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/baseline_map_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:name="com.example.hci_shop.DeliveryFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.deliveries.DeliveriesFragment"
tools:listitem="@layout/fragment_delivery"
app:layout_constraintTop_toBottomOf="@+id/deliveryMapImageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
```[enter image description here](https://i.stack.imgur.com/dsnYC.png)
this is what it looks like in the editor:
Here is the java file for the fragment:
软件包 com.example.hci_shop.ui.deliveries;
导入android.content.Context; 导入 android.os.Bundle; 导入 android.view.LayoutInflater; 导入android.view.View; 导入 android.view.ViewGroup;
导入 androidx.fragment.app.Fragment; 导入androidx.recyclerview.widget.GridLayoutManager; 导入androidx.recyclerview.widget.LinearLayoutManager; 导入androidx.recyclerview.widget.RecyclerView;
导入com.example.hci_shop。R; 导入 com.example.hci_shop.ui.placeholder.PlaceholderContent;
/**
表示项列表的片段。 */ public class DeliveriesFragment extends Fragment {
TODO:自定义参数参数名称 private static final String ARG_COLUMN_COUNT = “column-count”; TODO:自定义参数 私有 int mColumnCount = 1;
/**
- 片段管理器的必需空构造函数,用于实例化
- 片段(例如,在屏幕方向发生变化时)。 */
TODO:自定义参数初始化 @SuppressWarnings(“未使用”) public static DeliveriesFragment newInstance(int columnCount) { DeliveriesFragment fragment = new DeliveriesFragment(); Bundle args = new Bundle(); args.putInt(ARG_COLUMN_COUNT, columnCount); fragment.setArguments(参数); 返回片段; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
if (getArguments() != null) { mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); }
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑包 savedInstanceState) { 视图视图 = inflater.inflate(R.layout.fragment_delivery_list, container, false);
// Set the adapter if (view instanceof RecyclerView) { Context context = view.getContext(); RecyclerView recyclerView = (RecyclerView) view; if (mColumnCount <= 1) { recyclerView.setLayoutManager(new LinearLayoutManager(context)); } else { recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); } recyclerView.setAdapter(new MyDeliveriesRecyclerViewAdapter(PlaceholderContent.ITEMS)); } return view;
} }
Here is the RecyclerViewAdapter:
软件包 com.example.hci_shop.ui.deliveries;
导入 android.view.LayoutInflater; 导入 android.view.ViewGroup; 导入android.widget.TextView;
导入 androidx.annotation.NonNull; 导入androidx.recyclerview.widget.RecyclerView;
导入 com.example.hci_shop.databinding.FragmentDeliveryBinding; 导入 com.example.hci_shop.models.Order; 导入 com.example.hci_shop.ui.interfaces.OnItemClickListener; 导入 com.example.hci_shop.ui.placeholder.PlaceholderContent.PlaceholderItem;
导入 java.util.List;
/**
{@link RecyclerView.Adapter},可以显示 {@link PlaceholderItem}。
TODO:将实现替换为数据类型的代码。 */ 公共类 MyDeliveriesRecyclerViewAdapter 扩展 RecyclerView.Adapter<MyDeliveriesRecyclerViewAdapter.ViewHolder> {
private final list mValues;
public MyDeliveriesRecyclerViewAdapter(列表项) { this.mValues = 项目; }
@NonNull @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(FragmentDeliveryBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override public void onBindViewHolder(final ViewHolder holder, int position) { holder.mItem = mValues.get(位置); holder.textViewOrderID.setText(mValues.get(position).id); holder.textViewOrderAddress.setText(mValues.get(position).content); }
@Override 公共 int getItemCount() { 返回 mValues.size(); }
公共静态类 ViewHolder extends RecyclerView.ViewHolder { public final TextView textViewOrderID; public final TextView textViewOrderAddress; public final TextView textViewBoughtAt; public PlaceholderItem mItem;
public ViewHolder(FragmentDeliveryBinding binding) { super(binding.getRoot()); textViewOrderID = binding.textViewOrderID; textViewOrderAddress = binding.textViewOrderAddress; textViewBoughtAt = binding.textViewOrderBoughtAt; } @Override public String toString() { return super.toString() + " '" + textViewOrderAddress.getText() + "'"; }
} }
and here is the other xml file that is displayed above the fragment:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” android:id=“@+id/container” android:layout_width=“match_parent” android:layout_height=“match_parent”>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/profileBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/blueBlack"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" >
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profilePictureImageView"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:backgroundTint="@color/tanAccent"
android:tint="@color/tanAccent"
android:src="@drawable/profile_picture_placeholder"
app:civ_border_width="3dp"
app:civ_border_color="@color/tanAccent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<SearchView
android:id="@+id/searchbarShopActivity"
android:layout_width="match_parent"
android:layout_height="70dp"
android:queryHint="Suche ..."
android:iconifiedByDefault="false"
app:layout_constraintTop_toBottomOf="@id/profileBar"
app:layout_constraintStart_toStartOf="parent" />
<fragment
android:id="@+id/nav_host_fragment_activity_shop"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchbarShopActivity"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here is what the final result looks like:[enter image description here](https://i.stack.imgur.com/uCILd.png)
If somebody here could please tell me what my error is it would be very helpful. Thank you in advance
I have tried to see if I can find a solution in the documentation, on stackoverflow (if somebody my have had the same problem) and I have even stooped so low as to ask chatgpt. None of which could give me a solution that worked for me. I have come here in the hopes that maybe someone here could spot my error.
答: 暂无答案
评论