线性布局内部的线性布局-根据对象递归添加线性布局



编辑跳转到下方

我正在尝试为android制作一个reddit应用程序,我现在遇到的唯一问题是评论部分。我从reddit获得JSON文件,然后将其转换为GSON对象,您开始遇到的问题是回复。所以一个评论可以有回复,这是一个更多评论的列表,这些评论可以有答复等等。当试图在android中显示这一点时,它会变得令人困惑。所以我所做的是创建一个xml对象,它里面有一个listview,还有author的textview和这个listview上面的注释本身。现在,在我的自定义适配器中,它可以判断回复是否有对象列表,并更新该列表视图,否则它会隐藏列表视图。我知道你不应该把列表视图放在任何类型的滚动视图中,但我该怎么处理呢。它几乎像我现在做的那样工作,但问题是列表视图有时可以在彼此内部滚动,所以我要问的是如何使内部列表视图包裹内容或至少像它们一样工作。

新问题

在做了一些研究后,我发现我能让我的应用程序以我想要的方式工作的唯一方法就是放弃列表视图,转而使用LinearLayouts。我正在尝试在线性布局中递归加载线性布局。我需要帮助的是编写递归方法,在父级的线性布局中创建新的线性布局。这次我会附上我目前为止的代码。

以下是我迄今为止为我的实践reddit应用程序创建递归线性布局注释部分的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>

好的,回到解决方案。这是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>

奖金

GSON类,用于从发布评论的json文件中检索评论

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

最新更新