0

我有一個對話框片段,裏面會顯示一個RecycleView。這讓我瘋狂,因爲我幾乎看到所有關於RecycleView的問題都沒有出現問題,但仍然沒有解決我的問題。請看看我的代碼RecycleView不會顯示在對話框中

這裏是我的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ŧ ○片段

@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提要

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型號

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中的任何項目。有人請求e幫助

UPDATE 我只是將Fragment.xml中的佈局從RelativeLayout更改爲LinearLayout,它仍然不起作用。

我改變layout_heightmatch_parent,刪除此行以及android:layout_weight="1",仍不會出現以下item.As

試圖改變線性佈局作爲母體,android:layout_height="500dp"爲好,未示出了也

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

回答

0

最有可能問題android:layout_weight="1" 您還沒有使用除父級佈局中的RecyclerView之外的任何位置的權重。

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

這裏高度是0dp,重量用在RelativeLayout中。這些不會工作。

首先,權重不會與RelativeLayouts一起使用。您的Recycler視圖是RelativeLayout的子視圖。所以,如果你有一個預定義的大小,然後取出重量和一些高度與LinearLayout中適當的高度分佈的家長設置爲您的回收視圖

<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 使用合適的權重。這應該解決它。

+0

你的意思是在'comment_item.xml'的線性佈局??你的意思是? – ken

+0

你能告訴我在哪裏可以添加此代碼? – ken

+1

你將不得不更新你的Fragment.xml文件。 – Neji

0

組合android:layout_weight="1"android:layout_height="0dp"只能在線性佈局內工作。如果您想使用weight屬性,請將您的回收站放在LinearLayout內,或者設置自定義高度。基本上你的問題是你的回收站高度是「0dp」

+0

先生,我更改了佈局線性佈局ady,但仍然沒有顯示任何東西 – ken

+0

那是因爲你應該重新排列所有項目,如果你改變relativeLayout,試圖改變回收站的高度爲20dp或其他東西,看看它是否出現 –

+0

兄弟什麼都沒有發生.. 'comment_item.xml'沒有顯示出來。 – ken

0

無論你將layout_weight屬性設置的寬度或高度分別添加到0dp。另外將權重分配給其他元素只給予一個元素的權重不是一個好的做法,他們將覆蓋整個佈局。