编辑跳过
我正在尝试为android创建一个reddit应用程序,我遇到的唯一问题是现在的评论部分。我从reddit获取JSON文件然后将其转换为GSON对象,您开始遇到的问题是回复。所以评论可以有回复,这是一个更多评论的列表,这些评论可以有回复等等。当试图在Android中显示它时,它变得令人困惑。所以我所做的是创建一个xml对象,其中包含listview,以及作者的textview和此listview上方的注释本身。现在在我的自定义适配器中,它可以判断回复是否有对象列表并更新该列表视图,否则它会隐藏列表视图。我知道你不应该把listview放在一个可滚动视图的内部,但是我应该怎么处理它。它几乎按照我的方式工作,但问题是列表视图有时可以在彼此内部滚动,所以我要问的是如何使内部Listviews包装内容或至少像他们一样。
新问题
在做了一些研究之后,我发现我能够以我想要的方式工作的唯一方法是抛弃列表视图并转移到LinearLayouts。我试图在线性布局中递归加载线性布局。我需要帮助的是编写递归方法,在父级线性布局中创建新的线性布局。这次我将附上我迄今为止的代码。
这是我到目前为止为我的练习reddit app创建这个递归线性布局评论部分的java代码。
public class CommentsFragment extends Fragment {
public CommentsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_comments, container, false);
List<Comments.ChildData> list = MainActivity.comments.getData().getChildren();
LinearLayout layout = (LinearLayout) view.findViewById(R.id.mainLinearLayoutComments);
recursiveLinearLayoutCreator(list, layout);
return view;
}
public void recursiveLinearLayoutCreator(List<Comments.ChildData> list, LinearLayout layout){
for ( Comments.ChildData c : list) {
boolean temp = false;
try {
temp = ( c.getData().getReplies().getData() != null );
} catch ( Exception e) { temp = false; }
if ( temp )
{
// need to add children to layout here after creating new layout I think`enter code here`
recursiveLinearLayoutCreator( c.getData().getReplies().getData().getChildren(), // need to put current layout in);
}
}
}
这是我的线性布局项,用于将其子项添加到其线性布局的注释
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/commentTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="comment" />
<Space
android:layout_width="fill_parent"
android:layout_height="2dp" />
<TextView
android:id="@+id/authorTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="author" />
<Space
android:layout_width="fill_parent"
android:layout_height="2dp" />
<LinearLayout
android:id="@+id/CommentLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
这是我的顶级线性布局,我开始使用的还是递归添加
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.ejf011.scroller.CommentsFragment"
android:id="@+id/mainLinearLayoutComments">
</LinearLayout>
答案 0 :(得分:0)
好的,回到解决方案。下面是CommentsFragment的代码,它处理递归查看我的GSON对象列表以创建递归线性布局来处理reddit注释。
public class CommentsFragment extends Fragment {
public LayoutInflater mInflater;
public ViewGroup mContainer;
public CommentsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_comments, container, false);
mInflater = inflater;
mContainer = container;
List<Comments.ChildData> list = MainActivity.comments.getData().getChildren();
LinearLayout layout = (LinearLayout) view.findViewById(R.id.mainLinearLayoutComments);
recursiveLinearLayoutCreator(list, layout);
return view;
}
public void recursiveLinearLayoutCreator(List<Comments.ChildData> list, LinearLayout layout){
for ( Comments.ChildData c : list) {
boolean temp;
try {
temp = ( c.getData().getReplies().getData() != null );
c.getData().getBody();
c.getData().getAuthor();
} catch ( Exception e) { temp = false; }
if ( temp )
{
LinearLayout newLayout = (LinearLayout) mInflater.inflate(R.layout.comment_item, layout, false);
Log.d("child being made", c.getData().getBody());
ViewHolder holder = new ViewHolder(newLayout);
holder.comment.setText(c.getData().getBody());
holder.author.setText(c.getData().getAuthor());
layout.addView(newLayout);
recursiveLinearLayoutCreator(c.getData().getReplies().getData().getChildren(), holder.ll);
}
}
}
static class ViewHolder {
TextView comment;
TextView author;
LinearLayout ll;
ViewHolder(View view){
comment = (TextView) view.findViewById(R.id.commentTextView);
author = (TextView) view.findViewById(R.id.authorTextView);
ll = (LinearLayout) view.findViewById(R.id.CommentLinearLayout);
}
}
}
这是我的片段,它充当根片段。它是一个线性布局,带有一个滚动视图,里面有一个线性布局。将在以后清理。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.ejf011.scroller.CommentsFragment"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainScrollCommentsVertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/mainLinearLayoutComments"></LinearLayout>
</ScrollView>
</LinearLayout>
最后我的post_item.xml是我用
填充线性布局的项目<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:weightSum="100">
<Space
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="@+id/PostThumbnailImageView"
android:layout_width="0px"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_weight="40"
android:src="@android:drawable/ic_menu_save" />
<Space
android:layout_width="0px"
android:layout_height="1dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="56"
android:orientation="vertical">
<TextView
android:id="@+id/TitleOfThePost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title of the Post" />
<Space
android:layout_width="5dp"
android:layout_height="5dp" />
<TextView
android:id="@+id/AuthorOfThePost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Author of the Post" />
<Space
android:layout_width="5dp"
android:layout_height="5dp" />
<TextView
android:id="@+id/PostSubReddit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="r/random" />
</LinearLayout>
</LinearLayout>
<强>加成强>
从json文件中检索的评论的GSON类,用于帖子评论
public class Comments {
private String kind;
private CommentChildData data;
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public CommentChildData getData() {
return data;
}
public void setData(CommentChildData data) {
this.data = data;
}
static public class CommentChildData {
private String modhash;
private List<ChildData> children;
private String before;
private String after;
public String getModhash() {
return modhash;
}
public void setModhash(String modhash) {
this.modhash = modhash;
}
public List<ChildData> getChildren() {
return children;
}
public void setChildren(List<ChildData> children) {
this.children = children;
}
public String getBefore() {
return before;
}
public void setBefore(String before) {
this.before = before;
}
public String getAfter() {
return after;
}
public void setAfter(String after) {
this.after = after;
}
}
static public class ChildData {
private String kind;
private CommentData data;
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public CommentData getData() {
return data;
}
public void setData(CommentData data) {
this.data = data;
}
}
static public class CommentData {
private String subredd_id;
private String banned_by;
private String removal_reason;
private String link_id;
private String likes;
private Comments replies;
private Object user_reports;
private Boolean saved;
private boolean id;
private int gilded;
private String report_reason;
private String author;
private String parent_id;
private int score;
private String approved_by;
private int controversiality;
private String body;
private String edited;
private String author_flair_css_class;
private int downs;
private String body_html;
private String subreddit;
private boolean score_hidden;
private String name;
private int created;
private String author_flair_text;
private int created_utc;
private String distinguished;
private Object mod_reports;
private String num_reports;
private int ups;
public String getSubredd_id() {
return subredd_id;
}
public void setSubredd_id(String subredd_id) {
this.subredd_id = subredd_id;
}
public String getBanned_by() {
return banned_by;
}
public void setBanned_by(String banned_by) {
this.banned_by = banned_by;
}
public String getRemoval_reason() {
return removal_reason;
}
public void setRemoval_reason(String removal_reason) {
this.removal_reason = removal_reason;
}
public String getLink_id() {
return link_id;
}
public void setLink_id(String link_id) {
this.link_id = link_id;
}
public String getLikes() {
return likes;
}
public void setLikes(String likes) {
this.likes = likes;
}
public Comments getReplies() {
return replies;
}
public void setReplies(Comments replies) {
this.replies = replies;
}
public Object getUser_reports() {
return user_reports;
}
public void setUser_reports(Object user_reports) {
this.user_reports = user_reports;
}
public Boolean getSaved() {
return saved;
}
public void setSaved(Boolean saved) {
this.saved = saved;
}
public boolean isId() {
return id;
}
public void setId(boolean id) {
this.id = id;
}
public int getGilded() {
return gilded;
}
public void setGilded(int gilded) {
this.gilded = gilded;
}
public String getReport_reason() {
return report_reason;
}
public void setReport_reason(String report_reason) {
this.report_reason = report_reason;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getParent_id() {
return parent_id;
}
public void setParent_id(String parent_id) {
this.parent_id = parent_id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getApproved_by() {
return approved_by;
}
public void setApproved_by(String approved_by) {
this.approved_by = approved_by;
}
public int getControversiality() {
return controversiality;
}
public void setControversiality(int controversiality) {
this.controversiality = controversiality;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getEdited() {
return edited;
}
public void setEdited(String edited) {
this.edited = edited;
}
public String getAuthor_flair_css_class() {
return author_flair_css_class;
}
public void setAuthor_flair_css_class(String author_flair_css_class) {
this.author_flair_css_class = author_flair_css_class;
}
public int getDowns() {
return downs;
}
public void setDowns(int downs) {
this.downs = downs;
}
public String getBody_html() {
return body_html;
}
public void setBody_html(String body_html) {
this.body_html = body_html;
}
public String getSubreddit() {
return subreddit;
}
public void setSubreddit(String subreddit) {
this.subreddit = subreddit;
}
public boolean isScore_hidden() {
return score_hidden;
}
public void setScore_hidden(boolean score_hidden) {
this.score_hidden = score_hidden;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCreated() {
return created;
}
public void setCreated(int created) {
this.created = created;
}
public String getAuthor_flair_text() {
return author_flair_text;
}
public void setAuthor_flair_text(String author_flair_text) {
this.author_flair_text = author_flair_text;
}
public int getCreated_utc() {
return created_utc;
}
public void setCreated_utc(int created_utc) {
this.created_utc = created_utc;
}
public String getDistinguished() {
return distinguished;
}
public void setDistinguished(String distinguished) {
this.distinguished = distinguished;
}
public Object getMod_reports() {
return mod_reports;
}
public void setMod_reports(Object mod_reports) {
this.mod_reports = mod_reports;
}
public String getNum_reports() {
return num_reports;
}
public void setNum_reports(String num_reports) {
this.num_reports = num_reports;
}
public int getUps() {
return ups;
}
public void setUps(int ups) {
this.ups = ups;
}
}
}