物品未显示在RecyclerView内

时间:2017-03-22 06:06:23

标签: android xml android-layout android-fragments android-recyclerview

我检查了我的代码中的所有内容,我提出了一个问题并进行了一些修改,并且还阅读了有关"项目未在RecyclerView"中显示的所有问题,但布局仍未显示在我的recyclerView中

This is my previous question,我在这里附上我的所有代码,有3个回答告诉我,我应该使用LinearLayout代替RelativeLayout作为RecyclerView的父代,所以我修改了代码如下

fragment.xml之

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/commentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <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_marginLeft="@dimen/feed_item_margin"
            android:layout_marginRight="@dimen/feed_item_margin"
            android:layout_marginTop="@dimen/feed_item_margin"
            android:layout_weight="1"
            android:text="Be the first to like this" />

    </LinearLayout>

    <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">

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

    <LinearLayout
        android:id="@+id/commentInsert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:background="@null"
            android:ems="10"
            android:hint="Add a comment" />

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

    </LinearLayout>

</LinearLayout>

以下是应该出现在RecyclerView中的项目。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"
android:layout_weight="1">

<ImageView
    android:id="@+id/commentProfilePic"
    android:layout_weight="0.05"
    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:layout_weight="1"
    android:paddingLeft="@dimen/comment_item_profile_info_padd">

    <TextView
        android:id="@+id/commentUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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: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: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>

我读了这个issue告诉我需要添加这行代码adapter.notifyDataSetChanged();,我这样做了,这就是我解析JSON时的notifyDataSetChanged()

 private void parseJsonFeed(JSONObject response) {
    try {

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

            commentId = commentObj.getInt("comment_id");
            commenterUsername= commentObj.getString("commenter_username");
            commentBody =  commentObj.getString("comment_body");
            commenterProfileImage =  commentObj.getString("commenter_profile_image");
            commentCreatedAt = commentObj.getString("comment_created_at");

            //set all item to the Array list
            setItemToCommentArrayList(commentId,commenterUsername,commenterProfileImage,commentBody,commentCreatedAt);

        }

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

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

}

我检查过,当我将适配器设置为RecycleView时是否有问题。所以我读了这个answer,这就是我如何设置我的适配器

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

    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(getActivity(),commentItems);
    final LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
    commentRecyclerView.setLayoutManager(myLayoutManager);
    commentRecyclerView.setAdapter(commentAdapter);

这是我的CommentAdpater ,我仍然没有看到与我的另一个recycleview适配器有任何不同。

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyViewHolder>{
    private Context cContext;
    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 cContext, List<CommentItem> commentItems)       {
        this.cContext = cContext;
        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(cContext)
                .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();
    }
}

我确保在我的排球请求中收到我的JSON。我将其注销,在Volley onResponse中收到。我的JSON看起来像这样

  "comment":[{"comment_created_at":"2017-03-21 13:03:40","comment_id":8,"comment_body":"abc","commenter_username":"abc","profile_image_path":"http:\/\/abc\/def\/v1\/ef\/defaultmale.jpg"

这个question我想告诉我没有必要担心因为我使用Glide加载我的图片,Glide会处理

这是尝试的所有解决方案,但仍然没有完成,所以我仍然错过了这里,以comment_xml recyclerView Fragment.xml内的setItemToCommentArrayList()显示?任何引导我走向正确方向的指南都非常受欢迎。谢谢

更新

这是我的private void setItemToCommentArrayList(int commentId, String commenterUsername, String commenterProfileImage, String commentBody, String commentCreatedAt) { CommentItem item = new CommentItem(); item.setCommentId(commentId); item.setUsername(commenterUsername); item.setCommentProfilePic(commenterProfileImage); item.setCommentBody(commentBody); item.setCommentTimeStamp(commentCreatedAt); //save it to the comment array list commentItems.add(item); }

public class CommentItem {
private String username,commentBody,commentTimeStamp,commentProfilePic;
private int commentId;

public CommentItem(){
}

public CommentItem(int commentId, String username,String commentBody,String commentTimeStamp,String commentProfilePic){
    super();
    this.commentId = commentId;
    this.username = username;
    this.commentBody = commentBody;
    this.commentProfilePic= commentTimeStamp;
    this.commentTimeStamp= commentProfilePic;
}

public int getCommentId() {
    return commentId;
}

public void setCommentId(int commentId) {
    this.commentId = commentId;
}


public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getCommentBody() {
    return commentBody;
}

public void setCommentBody(String commentBody) {
    this.commentBody = commentBody;
}

public String getCommentTimeStamp() {
    return commentTimeStamp;
}

public void setCommentTimeStamp(String commentTimeStamp) {
    this.commentTimeStamp = commentTimeStamp;
}

public String getCommentProfilePic() {
    return commentProfilePic;
}

public void setCommentProfilePic(String commentProfilePic) {
    this.commentProfilePic = commentProfilePic;
}
}

以下是评论项目的数据模型

<link rel='stylesheet' href='push_ucid_bar_style.css' \>

2 个答案:

答案 0 :(得分:1)

首先尝试将dataSet尺寸打印成getItemCount recyclerview方法 -

@Override
public int getItemCount() {
    Log.e("Size of data:", "" +commentItems.size())
    return commentItems.size();
}

如果返回大于零 - 您的row_item recyclerview可能存在问题。仔细检查您的comment_item.xml文件。可能会尝试删除weights

答案 1 :(得分:1)

卡片中是否显示物品或者回收者视图本身未显示? 如果回收器视图本身未显示,则问题可能在getItemCount内,发送的大小为0。 检查那里,如果大小为零,则返回1.

@Override
public int getItemCount() {
 if(commentItems.size() == 0) 
    return 1; 
 else
    return commentItems.size();
}