【发布时间】: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