RecycleView未显示在对话片段中 [英] RecycleView not showing in Dialogue Fragment

本文介绍了RecycleView未显示在对话片段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话片段,里面会显示一个RecycleView.这让我发疯,因为我几乎看到所有关于RecycleView的问题都没有出现,但是仍然不能解决我的问题.请看看我的问题.代码

I have a Dialogue Fragment which a RecycleView will shown inside it.This is driving me crazy,cause I almost see all the SO question about RecycleView not show up problem,but still not solving my problem.Please take a look in my code

这是我的 Fragment.xml

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


<LinearLayout
    android:id="@+id/titlebar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Be the first to like this"
        android:layout_marginLeft="@dimen/feed_item_margin"
        android:layout_marginRight="@dimen/feed_item_margin"
        android:layout_marginTop="@dimen/feed_item_margin"/>
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="@color/dialog"
    android:layout_marginTop="10dp"
    android:paddingRight="@dimen/comment_item_status_pad_left_right"
    android:paddingLeft="@dimen/comment_item_status_pad_left_right"
    android:layout_below="@+id/titlebar"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/comment_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:animateLayoutChanges="false"
    android:scrollbars="vertical" />

<LinearLayout
    android:id="@+id/commentInsert"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:background="@android:color/white"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/commentField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Add a comment"
        android:background="@null"/>

    <Button
        android:id="@+id/sendButton"
        android:layout_width="77dp"
        android:layout_height="wrap_content"
        android:text="Send" />
</LinearLayout>

Fragment.java 将recycleview设置为Fragment

Fragment.java set the recycleview to the Fragment

  @Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
    // Remove TITLE
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    View dialogView = inflater.inflate(R.layout.fragment_comment, container,false);
    commentRecyclerView =(RecyclerView)dialogView.findViewById(R.id.comment_recycler_view);
    commentRecyclerView.setNestedScrollingEnabled(false);

    //bind the recycler view with the adapter
    commentAdapter = new CommentAdapter(this.getActivity(),commentItems);
    final LinearLayoutManager mLayoutManager = new LinearLayoutManager(this.getActivity());
    commentRecyclerView.setLayoutManager(mLayoutManager);
    commentRecyclerView.setAdapter(commentAdapter);

在这里我向服务器发出请求

  private void fetchItem(int item_id){


    // making fresh volley request and getting json
    JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
            URL_GET_ITEM + item_id, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            VolleyLog.d(AppController.TAG, "VolleyResponse: " + response.toString());
            Log.d("responseGet",response.toString());
            parseJsonFeed(response);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(AppController.TAG, "Error: " + error.getMessage());

        }
    }) {
        //adding header to authenticate
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {

            Map<String,String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            return  headers;
        }
    };

    // Adding request to volley request queue
    AppController.getInstance().addToRequestQueue(jsonReq);

}

在这里我解析json feed

private void parseJsonFeed(JSONObject response) {
    try {

        JSONArray itemArray = response.getJSONArray("item");
        //get all the item in Json
        for (int i = 0; i < itemArray.length(); i++) {
            JSONObject itemObj = (JSONObject) itemArray.get(i);

            itemId = itemObj.getInt("item_id");
            itemUsername= itemObj.getString("item_username");
            itemBody =  itemObj.getString("item_body");
            itemProfileImage =  itemObj.getString("item_profile_image");
            itemCreatedAt = itemtObj.getString("item_created_at");

            //set all item to the Array list
            setItemToCommentArrayList(itemId,itemUsername,itemProfileImage,itemBody,itemCreatedAt);

        }

        // notify data changes to list adapter
        itemAdapter.notifyDataSetChanged();

    }catch (JSONException e){
        System.out.println("end of content");
    }

}

在这里,我将所有详细信息添加到Item.java模型

Here I add all the details to the Item.java model

private void setItemToCommentArrayList(int itemId, String itemUsername, String itemrofileImage, String itemBody, String itemCreatedAt) {
    CommentItem item = new CommentItem();
    item.setCommentId(itemId);
    item.setUsername(itemUsername);
    item.setCommentProfilePic(itemProfileImage);
    item.setCommentBody(itemBody);
    item.setCommentTimeStamp(itemCreatedAt);


    //save it to the comment array list
    items.add(item);
}

这是comment_item.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="wrap_content">

    <ImageView
        android:id="@+id/commentProfilePic"
        android:layout_width="@dimen/comment_item_profile_pic"
        android:layout_height="@dimen/comment_item_profile_pic"
        android:layout_marginLeft="@dimen/feed_item_margin"
        android:layout_marginRight="@dimen/feed_item_margin"
        android:layout_marginTop="@dimen/feed_item_margin"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/feed_item_margin"
        android:layout_marginRight="@dimen/feed_item_margin"
        android:layout_marginTop="@dimen/feed_item_margin"
        android:orientation="vertical"

        android:paddingLeft="@dimen/comment_item_profile_info_padd">

        <TextView
            android:id="@+id/commentUsername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="@dimen/comment_item_status_pad_left_right"
            android:paddingRight="@dimen/comment_item_status_pad_left_right"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/commentBody"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="@dimen/comment_item_status_pad_left_right"
            android:paddingRight="@dimen/comment_item_status_pad_left_right" />

        <TextView
            android:id="@+id/commentTimestamp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="@dimen/comment_item_status_pad_left_right"
            android:paddingRight="@dimen/comment_item_status_pad_left_right"
            android:paddingTop="@dimen/comment_item_timestamp_pad_top" />
    </LinearLayout>

</LinearLayout>

最后,这是适配器

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyViewHolder>{
private Context mContext;
private List<CommentItem> commentItems;

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView commentBody,commentUsername,commentTimeStamp;
    ImageView commentProfilePic;

    //find all the view here
    MyViewHolder(final View view) {
       super(view);

        commentProfilePic = (ImageView)view.findViewById(R.id.commentProfilePic);
        commentUsername = (TextView)view.findViewById(R.id.commentUsername);
        commentBody = (TextView)view.findViewById(R.id.commentBody);
        commentTimeStamp = (TextView)view.findViewById(R.id.commentTimestamp);

    }
}

public CommentAdapter(Context mContext, List<CommentItem> commentItems) {
    this.mContext = mContext;
    this.commentItems = commentItems;
}


@Override
public long getItemId(int position) {
    return position;
}
//this one for make the adview inside this
@Override
public int getItemViewType(int position) {
    return position;
}


//bind the comment item here
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View commentItemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.comment_item, parent, false);
    return new MyViewHolder(commentItemView);
}

//do all the action here
@Override
public void onBindViewHolder(CommentAdapter.MyViewHolder holder, int position) {

    final CommentItem commentItem = commentItems.get(position);

    //commenter username
    holder.commentUsername.setText(commentItem.getUsername());

    //commenter profile image
    Glide
            .with(mContext)
            .load(commentItem.getCommentProfilePic())
            .fitCenter()
            .into(holder.commentProfilePic);


    //comment body
    holder.commentBody.setText(commentItem.getCommentBody());

    //comment timestamp
    holder.commentTimeStamp.setText(commentItem.getCommentTimeStamp());
}

@Override
public int getItemCount() {
    return commentItems.size();
}
}

我几乎用另一个RecycleView检查了所有内容,没有发现任何不同.也看到了所有Stackoverflow问题,我仍然看不到发生了什么.它只是没有在recycleview中显示任何项目.有人请帮忙

I almost check everything with my another RecycleView,I didnt see any different.And also see all the Stackoverflow question too,I still cant see what happen.It just didnt show any item in the recycleview.Somebody please help

更新 我只是将Fragment.xml中的布局从RelativeLayout更改为LinearLayout,它仍然无法正常工作.

UPDATE I just change the layout in Fragment.xml from RelativeLayout to LinearLayout,it still not working.

我将layout_height更改为match_parent,也删除了该行android:layout_weight="1",仍然没有显示该项目.如下

I change the layout_height to match_parent ,remove this line as well android:layout_weight="1" ,still not show up the item.As below

尝试将线性布局更改为父级,android:layout_height="500dp"也未显示

Tried change to Linear Layout as parent, android:layout_height="500dp"as well,not shown up also

<android.support.v7.widget.RecyclerView
    android:id="@+id/comment_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="false"
    android:scrollbars="vertical" />

推荐答案

android:layout_weight="1"最有可能出现问题 您没有在父版式中的RecyclerView之外的任何地方使用权重.

Most probably issue with android:layout_weight="1" You havent used weights anywhere other than the RecyclerView in parent Layout.

android:layout_height="0dp"
android:layout_weight="1"

此处的高度为0dp,而重量在RelativeLayout中使用.这些都行不通.

Here height is 0dp and weight is used in a RelativeLayout. These wont work.

首先,权重无法与RelativeLayouts配合使用.您的Recycler视图是RelativeLayout的子视图.因此,如果您有预定义的尺寸,那么请去掉重量并为回收者视图设置一些高度

First of all, weights wont work with RelativeLayouts. Your Recycler view is child of RelativeLayout. So, if you have a predefined size then remove the weight and set some height to your recycler view

<android.support.v7.widget.RecyclerView
        android:id="@+id/comment_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:animateLayoutChanges="false"
        android:scrollbars="vertical" />

OR 使用适当的权重以将LinearLayout作为父级进行适当的高度分布.那应该解决它.

OR Use appropriate weights for proper height distribution with a LinearLayout as parent. That should fix it.

这篇关于RecycleView未显示在对话片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆