Android RecycleView未显示在对话片段中
我有一个对话片段,里面会显示一个RecycleView。这让我发疯,因为我几乎看到所有关于RecycleView的问题都没有出现,但仍然没有解决我的问题。请查看我的代码 这是我的片段.xmlAndroid RecycleView未显示在对话片段中,android,android-layout,android-fragments,android-recyclerview,android-dialogfragment,Android,Android Layout,Android Fragments,Android Recyclerview,Android Dialogfragment,我有一个对话片段,里面会显示一个RecycleView。这让我发疯,因为我几乎看到所有关于RecycleView的问题都没有出现,但仍然没有解决我的问题。请查看我的代码 这是我的片段.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
<?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>
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);
}
这里我向服务器发出请求
<?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>
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);
}
这里是注释\u item.xml
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);
}
<?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>
很可能是android:layout\u weight=“1” 除了父布局中的RecyclerView之外,您没有在任何地方使用权重
android:layout_height="0dp"
android:layout_weight="1"
此处高度为0dp,重量用于相对长度。这些行不通
首先,重量与相对长度无关。您的回收器视图是RelativeLayout的子视图。因此,如果你有一个预定义的尺寸,那么移除重量并为你的回收器视图设置一些高度
<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" />
或
使用适当的权重进行适当的高度分布,线性布局作为父级。这应该可以解决问题。android:layout\u weight=“1”和
android:layout\u height=“0dp”
的组合只能在线性布局中工作。如果要使用“重量”属性,请将回收器放在线性布局中或设置自定义高度。基本上,你的问题是你的回收器高度是“0dp”无论你在哪里添加布局重量属性,设置宽度或高度分别为0dp。另外,为其他元素指定权重仅为一个元素指定权重不是一种好的做法,因为它们将覆盖整个布局。请在onCreate()方法中尝试这一行:
setStyle(STYLE\u NORMAL,android.R.STYLE.ThemeOverlay)你在
comment\u item.xml
的线性布局中是这个意思吗?你是这个意思吗?我也将布局高度
更改为match\u parent
,仍然不工作,先生,我将布局更改为线性布局ady,但是仍然没有显示任何内容。因为如果更改相对长度,您应该重新排列所有项目,请尝试将回收器的高度更改为20dp或其他值,以查看它是否看起来像一个什么都没有发生的东西。comment\u item.xml
没有显示。
android:layout_height="0dp"
android:layout_weight="1"
<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" />