RecyclerView 未显示在对话片段中
RecycleView not showing in Dialogue Fragment
我有一个对话片段,里面会显示一个 RecycleView it.This 快把我逼疯了,因为我几乎看到了所有关于 RecycleView 不显示问题的问题,但仍然没有解决我的问题 problem.Please 看看我的代码
这是我的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 将回收视图设置为片段
@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
模型
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 检查了所有内容,我没有看到任何 different.And 也看到了所有 Whosebug 问题,我仍然看不到什么 happen.It 只是没有显示 recycleview.Somebody 请帮忙
更新
我只是将 Fragment.xml 中的布局从 RelativeLayout 更改为 LinearLayout,它仍然无法正常工作。
我把layout_height
改成了match_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" />
很可能是 android:layout_weight="1"
的问题
除了父布局中的 RecyclerView,您还没有在任何地方使用权重。
android:layout_height="0dp"
android:layout_weight="1"
此处高度为0dp,重量用在RelativeLayout中。这些都行不通。
首先,权重不适用于 RelativeLayouts。您的 Recycler 视图是 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" />
或
以 LinearLayout 作为父级,使用适当的权重进行适当的高度分布。这应该可以解决它。
android:layout_weight="1"
和 android:layout_height="0dp"
的组合仅适用于线性布局。如果你想使用 weight 属性,请将你的回收器放在 LinearLayout 中或设置自定义高度。基本上你的问题是你的回收站高度是“0dp”
无论您在何处添加 layout_weight 属性,都将宽度或高度分别设置为 0dp。还为其他元素分配权重,只给一个元素分配权重不是一个好的做法,它们将覆盖整个布局。
在 onCreate() 方法中尝试这一行:
setStyle(STYLE_NORMAL, android.R.style.ThemeOverlay)
我有一个对话片段,里面会显示一个 RecycleView it.This 快把我逼疯了,因为我几乎看到了所有关于 RecycleView 不显示问题的问题,但仍然没有解决我的问题 problem.Please 看看我的代码
这是我的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 将回收视图设置为片段
@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
模型
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 检查了所有内容,我没有看到任何 different.And 也看到了所有 Whosebug 问题,我仍然看不到什么 happen.It 只是没有显示 recycleview.Somebody 请帮忙
更新 我只是将 Fragment.xml 中的布局从 RelativeLayout 更改为 LinearLayout,它仍然无法正常工作。
我把layout_height
改成了match_parent
,把这行也去掉了android:layout_weight="1"
,还是没有出现
尝试将线性布局更改为父级,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" />
很可能是 android:layout_weight="1"
的问题
除了父布局中的 RecyclerView,您还没有在任何地方使用权重。
android:layout_height="0dp"
android:layout_weight="1"
此处高度为0dp,重量用在RelativeLayout中。这些都行不通。
首先,权重不适用于 RelativeLayouts。您的 Recycler 视图是 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" />
或 以 LinearLayout 作为父级,使用适当的权重进行适当的高度分布。这应该可以解决它。
android:layout_weight="1"
和 android:layout_height="0dp"
的组合仅适用于线性布局。如果你想使用 weight 属性,请将你的回收器放在 LinearLayout 中或设置自定义高度。基本上你的问题是你的回收站高度是“0dp”
无论您在何处添加 layout_weight 属性,都将宽度或高度分别设置为 0dp。还为其他元素分配权重,只给一个元素分配权重不是一个好的做法,它们将覆盖整个布局。
在 onCreate() 方法中尝试这一行: setStyle(STYLE_NORMAL, android.R.style.ThemeOverlay)