0

Ich habe ein Dialog Fragment, das ein RecycleView darin angezeigt wird. Dies macht mich verrückt, denn ich sehe fast alle SO Frage über RecycleView nicht Problem angezeigt, aber immer noch nicht mein Problem zu lösen. Bitte werfen Sie einen Blick in meinen CodeRecycleView zeigt nicht im Dialog Fragment

Hier ist meine 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 stellen Sie die recycleview t o das 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); 

Hier mache ich Anfrage an Server

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); 

} 

Hier habe ich die json Feed

ich hinzufügen, um alle Details zu den Item.java Hier
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"); 
    } 

} 

analysieren Modell

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); 
} 

Hier die 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> 

Schließlich hier ist der Adapter

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(); 
} 
} 

ich fast alles mit meinem anderen RecycleView überprüfen, hatte ich nicht anders sehen .Und sehen Sie auch die ganze Stackoverflow-Frage, ich kann immer noch nicht sehen, was passiert.Es zeigte nur kein Element in der Recyclingansicht e Hilfe

UPDATE ich das Layout in Fragment.xml von RelativeLayout zu Linearlayout, es funktioniert immer noch nicht ändern, nur.

Ich wechsle die layout_height zu match_parent, entfernen Sie diese Zeile als auch android:layout_weight="1", noch nicht die item.As zeigen unter

Versuchte Veränderung als Eltern Linear-Layout, android:layout_height="500dp" als auch, nicht auch

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

Antwort

0

Am wahrscheinlichsten Problem mit android:layout_weight="1" Sie haben Gewichte nirgendwo anders als die RecyclerView im übergeordneten Layout verwendet.

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

Hier ist die Höhe 0dp und Gewicht wird in einem RelativeLayout verwendet. Diese arbeiten nicht.

Zuerst werden Gewichte nicht mit RelativeLayouts arbeiten. Ihre Recycler-Ansicht ist Kind von RelativeLayout.Also, wenn Sie eine vordefinierte Größe haben dann das Gewicht entfernen und eine Höhe auf Ihre Recycler Ansicht verwenden

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

ODER Verwenden Sie geeignete Gewichte für die richtige Höhenverteilung mit einem Linearlayout als Elternteil. Das sollte es beheben.

+0

Du meinst das im linearen Layout von 'comment_item.xml'? Meinst du das ?? – ken

+0

können Sie mir sagen, wo ich diesen Code hinzufügen kann? – ken

+1

müssen Sie Ihre Fragment.xml-Datei aktualisieren. – Neji

0

Die Kombination android:layout_weight="1" und android:layout_height="0dp" funktioniert nur innerhalb eines linearen Layouts. Wenn Sie das Gewichtsattribut verwenden möchten, platzieren Sie Ihren Recycler in einem LinearLayout oder legen Sie stattdessen eine benutzerdefinierte Höhe fest. Grundsätzlich ist Ihr Problem, dass Ihre Recycler Höhe "0dp" ist

+0

Herr, ändere ich das Layout zu Linear Layout ady, aber immer noch nichts zeigen – ken

+0

Das ist, weil Sie alle Elemente neu anordnen sollten, wenn Sie die relativeLayout ändern, versuchen Sie, die Höhe des Recyclers auf 20dp oder etwas zu ändern, um zu sehen, ob es –

+0

Bro gerade nichts passiert..the 'comment_item.xml' wird nicht angezeigt. – ken

0

Wo immer Sie layout_weight Attribut setzen Breite oder Höhe auf 0dp. Weisen Sie auch anderen Elementen Gewicht zu, um nur einem Element Gewicht zu geben. Es ist keine gute Übung, dass sie das gesamte Layout abdecken.

Verwandte Themen