RecyclerView 不显示项目

Posted

技术标签:

【中文标题】RecyclerView 不显示项目【英文标题】:RecyclerView is not displaying items 【发布时间】:2019-05-25 06:27:45 【问题描述】:

我创建了一个应用程序,它在放置在片段中的 RecyclerView 中显示食品订单。使用 AsyncTask 检索食品订单详细信息。但不知何故,片段中没有显示任何内容。它保持空白。我一定是忘记了什么或忽略了什么。

这是订单片段:

public class OrdersFragment extends Fragment 
RecyclerView recyclerView;
private OrdersAdapter adapter;
RecyclerView.LayoutManager layoutManager;
private static List<Orders> orderslist;



 public static final MediaType JSON = MediaType.parse("application/json; 
charset=utf-8");

SharedPreferences sharedPreferences;
View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container,  
Bundle savedInstanceState) 

    sharedPreferences = getActivity().getSharedPreferences("UserInfo",0);
    view = inflater.inflate(R.layout.fragment_orders, container, false);

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    orderslist = new ArrayList<>();

    new load_orders().execute();


    return view;





   public class load_orders extends AsyncTask<String, Void, Void>  
        @Override
        protected Void doInBackground(String... strings) 
            try
                OkHttpClient client = new OkHttpClient();
                JSONObject json = new JSONObject();
                JSONObject datajson = new JSONObject();
                datajson.put("command","ecoexpress_orders");
                datajson.put("username", 
sharedPreferences.getString("Username",null));
                json.put("data",datajson);

                RequestBody body = RequestBody.create(JSON, 
json.toString());

                Request request = new Request.Builder()
                        .url("https://api.watext.com/hook/insert_pagetoken")
                        .post(body)
                        .addHeader("content-type", "application/json")
                        .addHeader("cache-control", "no-cache")
                        .addHeader("postman-token", "55747e8a-0a9a-1fd5-77b0-8af0027be1f1")
                        .build();
                Response response = client.newCall(request).execute();

                JSONObject responseJSON = new JSONObject(response.body().string());
                JSONArray messageArray = responseJSON.getJSONArray("message");



                for (int i = 0; i < messageArray.length(); i ++ )


                    JSONObject order_item = messageArray.getJSONObject(i);
                    Orders orders = new Orders(order_item.getString("order_id"),
                            order_item.getString("order_details"));

                    orderslist.add(orders);

                

            
            catch (Exception e)


                return null;
            
            return null;
        

        @Override
        protected void onPostExecute(Void aVoid) 

            layoutManager = new LinearLayoutManager(getActivity());

            adapter = new OrdersAdapter(getContext(),orderslist);

            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
        //adapter.notifyDataSetChanged();
        
    




这里是模型 Order.class:

public class Orders 


        private String order_id;
        private String order_details;

        public Orders(String order_id, String order_details) 
            this.order_id = order_id;
            this.order_details = order_details;
        

        public String getOrder_id() 
            return order_id;
        

        public void setOrder_id(String order_id) 
            this.order_id = order_id;
        

        public String getOrder_details() 
            return order_details;
        

        public void setOrder_details(String order_details) 
            this.order_details = order_details;
        


这是 OrdersAdapter:

public class OrdersAdapter extends RecyclerView.Adapter<OrdersAdapter.ViewHolder> 

    private Context ctx;
    private List<Orders> list;


    public OrdersAdapter(Context ctx, List<Orders> list) 
        this.ctx = ctx;
        this.list = list;
    


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) 
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);

        return new ViewHolder(v);
    

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) 

       viewHolder.id.setText(list.get(i).getOrder_id());
        viewHolder.details.setText(list.get(i).getOrder_details());

    

    @Override
    public int getItemCount() 

        return list.size();
    

    public class ViewHolder extends RecyclerView.ViewHolder

        public TextView id, details;

        public ViewHolder( View itemView) 
            super(itemView);

            id = (TextView)itemView.findViewById(R.id.orderID_textview);
            details = (TextView)itemView.findViewById(R.id.orderDetails_textview);

        
    

这是 OrdersFragment 的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_ android:layout_>


       <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_
            android:layout_
            android:scrollbars="vertical"

            />



</RelativeLayout>

还有 CardView 的布局:

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        card_view:cardBackgroundColor="@color/colorPrimary"
        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="horizontal"
            >



            <LinearLayout
                android:layout_
                android:layout_
                android:layout_marginTop="12dp"
                android:layout_weight="2"
                android:orientation="vertical"
                >

                <TextView
                    android:id="@+id/orderID_textview"
                    android:layout_
                    android:layout_
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10dp"
                    android:text="Order ID"
                   />

                <TextView
                    android:id="@+id/orderDetails_textview"
                    android:layout_
                    android:layout_
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10dp"

                    android:text="Order Details"
                   />

            </LinearLayout>
        </LinearLayout>

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

</LinearLayout>

提前致谢!

【问题讨论】:

首先检查list.size()是什么?还要将 match_parent 更改为 wrap_content 以获得卡片视图。 adapter.notifyDataSetChanged();被评论了,为什么? 【参考方案1】:

我注意到您有 RecyclerView.LayoutManager 作为布局管理器,将其更改为 LinearLayout.LayoutManager 应该可以正常工作。

【讨论】:

【参考方案2】:

尝试像这样在你的 recyclerview 适配器中初始化数组列表:-

List<Orders> list = new Arraylist<>();

【讨论】:

使用这个会有什么效果?【参考方案3】:

    RecyclerView.LayoutManager 更改为LinearLayoutManager

    以这种方式在onCreateView 中初始化您的LinearLayoutManager

    layoutManager = new LinearLayoutManager(getActivity());.

    layoutManager 分配给下一行的recyclerview,例如:recyclerView.setLayoutManager(layoutManager);

    不要将您的 list 设为静态。

还请检查adapter 中的列表大小,并尽量不要在onPostExecute() 中初始化和设置layoutManager

【讨论】:

【参考方案4】:

我同意答案@Piyush,你应该检查列表为空,如果你确定数据列表不为空,你必须将ma​​tch_parent更改为wrap_content 并将 最小高度 添加到根布局视图(添加到项目布局 xml 中 CardView 的 LinearLayoutView 父级

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:minHeight="70dp"
>
....

编辑

应@Raghavendra 的要求,我可以解释为什么我们必须为项目布局设置最小高度,如果你想感兴趣,请阅读这篇好文章:

RecyclerView 23.2.0 and item height

Android Support Library 23.2

【讨论】:

我只是好奇你能解释一下为什么吗?

以上是关于RecyclerView 不显示项目的主要内容,如果未能解决你的问题,请参考以下文章

如何在 JSON 的 Recycler 视图中显示特定类别

使用recyclerview不显示数据

Android RecyclerView 绘制流程及Recycler缓存

Android RecyclerView 绘制流程及Recycler缓存

Recycler View重复,如何查看一次

Recycler VIew 和 Adapter 打开内容不同的Activity