【问题标题】:Linear Layouts inside Linear Layout - recursively adding linear layouts depending on object线性布局内的线性布局 - 根据对象递归添加线性布局
【发布时间】:2015-08-19 04:35:13
【问题描述】:

编辑跳到下面

我正在尝试为 android 制作一个 reddit 应用程序,我现在遇到的唯一问题是 cmets 部分。我从 reddit 获取 JSON 文件,然后将其转换为 GSON 对象,而您开始遇到的问题是回复。所以评论可以有回复,这是一个更多 cmets 的列表,这些 cmets 可以有回复等等。当试图在 android 中显示它时,它会变得混乱。所以我所做的是创建一个 xml 对象,其中包含一个列表视图,以及作者的文本视图和此列表视图上方的评论本身。现在在我的自定义适配器中,它可以判断回复是否有对象列表并更新该列表视图,否则它会隐藏列表视图。我知道您不应该将列表视图放在任何类型的可滚动视图中,但我应该如何处理这个问题。它几乎按照我的方式工作,但问题是列表视图有时可以在彼此内部滚动,所以我要问的是如何让内部列表视图包装内容或至少像它们一样工作。

新问题

在做了一些研究之后,我发现让我的应用程序按照我想要的方式运行的唯一方法是放弃列表视图并转向线性布局。我正在尝试在线性布局中递归加载线性布局。我需要帮助的是编写递归方法,在父级的线性布局中创建新的线性布局。这次我会附上我目前掌握的代码。

这是迄今为止我为我的实践 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);
        }

    }
}

这是我的线性布局项目,用于将他们的孩子添加到线性布局中的 cmets

<?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>

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    好的,回到解决方案。这是 CommentsFragment 的代码,它处理递归地查看我的 GSON 对象列表以创建递归线性布局来处理 reddit cmets。

    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>
    

    奖金

    从帖子 cmets 的 json 文件中检索的 cmets 的 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;
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多