CardView没有显示在片段中

乔什·帕里努萨(Josh Parinussa)

我要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);
        }
    }
}
维卡斯·辛格(Vikas Singh)

Isue1,您的recyclerview高度为match_parent

Isue2您未在addData()方法中调用您conCrete方法。

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

然后addData()在你的onCreate方法中调用你方法

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

recyclerview 没有在片段中显示数据

FloatingActionButton没有显示在片段中

我的工具栏没有显示在 CardView 中

如何在带有片段的活动中显示RecyclerView和CardView布局?

片段中的Recyclerview没有显示任何内容

为什么我的RecyclerView没有显示在片段中?

为什么我的 recyclerview 没有在片段中显示数据?

CardView 不显示披萨片段中的图片

片段中的RecycleView + CardView

显示Google Maps片段,但没有地图

没有片段显示在ViewPager上

没有片段显示的标签

Logcat没有显示任何片段

带有CardView的RecyclerView:什么都没有显示

为什么我的片段没有显示在导航控制器中?

片段中的Recycler View没有显示任何数据,尽管PagingSource loggs正确的数据

Textviews 没有从数组和片段中显示出来?

试图在odoo表单小部件中显示棋盘js,没有错误,没有片段

Cardview圆角没有显示在屏幕截图中?

Cardview圆角没有显示在屏幕截图中?

将cardview放入片段中

没有片段视图的片段中的列表视图

Viewpager2打开所选页面(从cardview)没有片段

有什么特殊的原因为什么在下面显示的代码片段中没有省略move构造函数?

机器人 - ViewPager没有显示片段

的Android RecyclerView没有显示在片段图像或文本

android -fragmenttabhost没有显示在我的片段内

带有 RecycleView + CardView 的片段(致命例外)

将Google地图实现为片段后,我在运行块中没有任何错误,但该片段未在我的设备上显示地图