CardView没有显示在片段中

时间:2018-07-05 05:29:15

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

我想列出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);
        }
    }
}

5 个答案:

答案 0 :(得分:2)

问题1 ,您的回收站视图高度为match_parent

问题2 ,您不是在addData()方法中调用conCrete方法。

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在您的OrderListItem主linearLayout中将android:layout_height="match_parent"更改为wrap_content

答案 3 :(得分:0)

    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;

答案 4 :(得分:0)

也许您应该重写onViewCreate()

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