【问题标题】:CardView not showing up in fragmentCardView 未显示在片段中
【发布时间】:2018-07-05 05:29:15
【问题描述】:

我想创建cardview 列表,我已经为RecyclerView 创建了XML,并且该项目还带有适配器和模型。但是当我运行它时,它只显示空白屏幕。

这是我的片段:

public class OrderListFragment extends Fragment {

    private RecyclerView recyclerView;
    private OrderListAdapter adapter;
    private ArrayList<OrderListModel> OrderListArrayList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list, container, false);
        getActivity().setTitle(R.string.title_mn_order_list);

        recyclerView = (RecyclerView) view.findViewById(R.id.orderList_recycler_view);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);

        adapter = new OrderListAdapter(OrderListArrayList);
        recyclerView.setAdapter(adapter);
        return view;

    }

    void addData(){
        OrderListArrayList = new ArrayList<>();
        OrderListArrayList.add(new OrderListModel(
                "4 Juli 2018 12:20",
                "ODRNO1804120003",
                "Arief Wijaya Putra",
                "MOBIL BARU",
                "Mobil Baru HONDA BR-V-E CVT"));
        OrderListArrayList.add(new OrderListModel(
                "4 Juli 2018 10:54",
                "ODRNO1804100001",
                "Gusman Linandry",
                "MOTOR BEAS",
                "Motor Bekas Honda Supra X"));
        OrderListArrayList.add(new OrderListModel(
                "4 Juli 2018 11:54",
                "ODRNO1804120005",
                "Arie Fengki",
                "MOTOR BARU",
                "Motor Baru Yamaha N-Max 155"));
        OrderListArrayList.add(new OrderListModel(
                "3 Juli 2018 13:18",
                "ODRNO1804110018",
                "Bastian Anggara",
                "MOBIL BEKAS",
                "Mobil Bekas Toyota Avanza 1 3 G 2011"));
        OrderListArrayList.add(new OrderListModel(
                "11 Juni 2018 13:52",
                "ODRNO1804110005",
                "Tio",
                "MOBIL BARU",
                "Mobil Baru Mitsubishi Xpander Ultimate A/T"));
        OrderListArrayList.add(new OrderListModel(
                "11 Juni 2018 14:50",
                "ODRNO1804110006",
                "Mariska",
                "MOTOR BARU",
                "Motor Baru HONDA Verza 150"));
        OrderListArrayList.add(new OrderListModel(
                "11 Apr 2018 15:01",
                "ODRNO1804110010",
                "Dixie",
                "MOBIL BEKAS",
                "Mobil Bekas Xenia type X 1300cc 2016"));
    }
}

这是我的 OrderListFragment :

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



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <View
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        android:background="@drawable/actionbar_background"></View>


        <android.support.v7.widget.RecyclerView
            android:id="@+id/orderList_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </RelativeLayout>



</LinearLayout>

这是我的模特:

public class OrderListModel {

    private String date;
    private String orderNumber;
    private String customerName;
    private String productStatus;
    private String notes;

    public OrderListModel(String date, String orderNumber, String customerName, String productStatus, String notes) {
        this.date = date;
        this.orderNumber = orderNumber;
        this.customerName = customerName;
        this.productStatus = productStatus;
        this.notes = notes;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(String productStatus) {
        this.productStatus = productStatus;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }
}

这是我的 OrderListItem 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">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="6dp"
        card_view:cardElevation="3dp"
        card_view:cardUseCompatPadding="true">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView
                android:id="@+id/txt_order_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txt_order_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txt_customer_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txt_product_status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txt_notes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

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

这是我的适配器:

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {

    private ArrayList<OrderListModel> itemOrderList;

    public OrderListAdapter(ArrayList<OrderListModel> itemOrderList) {
        this.itemOrderList = itemOrderList;
    }

    @Override
    public OrderListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.order_list_item, parent, false);
        return new OrderListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
        orderListViewHolder.txtDate.setText(itemOrderList.get(position).getDate());
        orderListViewHolder.txtOrderNumber.setText(itemOrderList.get(position).getOrderNumber());
        orderListViewHolder.txtCustomerName.setText(itemOrderList.get(position).getCustomerName());
        orderListViewHolder.txtProductStatus.setText(itemOrderList.get(position).getProductStatus());
        orderListViewHolder.txtNotes.setText(itemOrderList.get(position).getNotes());
    }

    @Override
    public int getItemCount() {
        return (itemOrderList != null) ? itemOrderList.size() : 0;
    }

    public class OrderListViewHolder extends RecyclerView.ViewHolder{
        private TextView txtDate, txtOrderNumber, txtCustomerName, txtProductStatus,
                txtNotes;

        public OrderListViewHolder(View itemView) {
            super(itemView);
            txtDate = (TextView) itemView.findViewById(R.id.txt_order_date);
            txtOrderNumber = (TextView) itemView.findViewById(R.id.txt_order_number);
            txtCustomerName = (TextView) itemView.findViewById(R.id.txt_customer_name);
            txtProductStatus = (TextView) itemView.findViewById(R.id.txt_product_status);
            txtNotes = (TextView) itemView.findViewById(R.id.txt_notes);
        }
    }
}

【问题讨论】:

  • 上传你的截图??
  • 您忘记调用 addData() 方法。在初始化适配器之前调用该方法

标签: android android-fragments android-recyclerview android-cardview


【解决方案1】:

Isue1你的recyclerview高度是match_parent

Isue2你没有在你的conCrete方法中调用你的addData()方法。

将回收站项目的高度保持为match_parent,每个屏幕仅显示一个视图。将您的回收站项目高度从 match_parent 更改为 wrap_content

并在您的 onCreate 方法中调用您的 addData() 方法

【讨论】:

  • 非常感谢,我忘了添加 addData() 方法 :)
【解决方案2】:

你在哪里调用add() 方法?我没有在你的代码中看到它 同样在调用 add 之后,您应该将 OrderListArrayList 设置为您的适配器,然后调用 adapter.norifyDataSetChanged();

【讨论】:

    【解决方案3】:

    在您的 OrderListItem 主线性布局中将 android:layout_height="match_parent" 更改为 wrap_content

    【讨论】:

      【解决方案4】:
          recyclerView = (RecyclerView) view.findViewById(R.id.orderList_recycler_view);
          RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
          recyclerView.setLayoutManager(layoutManager);
      
          //call addData() method;
          addData();
          adapter = new OrderListAdapter(OrderListArrayList);
          recyclerView.setAdapter(adapter);
          return view;
      

      【讨论】:

        【解决方案5】:

        也许你应该重写 onViewCreate()

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            adapter = new OrderListAdapter(OrderListArrayList);
            recyclerView.setAdapter(adapter);
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-09
          • 2021-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多