Android - 如何在不知道内部有多少对象的情况下创建 ScrollView?
Android - How do I create a ScrollView without knowing how many objects it will have inside?
我有一个与 returns 具有 X 个字符串的 JSON 服务通信的应用程序。
我希望每个字符串在 ScrollView 中都有自己的编辑文本或文本视图,但不知道如何做到这一点。
有人能告诉我方法吗?不求代码完成,只求如何实现。
您应该使用带有 ArrayAdapter 或更高效的 RecyclerView 的 ListView,而不是使用 ScrollView。
一些教程如何使用这个小部件:
- https://developer.android.com/training/material/lists-cards.html
- http://www.vogella.com/tutorials/AndroidListView/article.html
希望这对您有所帮助!
基本上,为了拥有动态滚动视图,建议像@Macimi 所说的那样使用 RecyclerView。为了进一步为您提供全面的演练,这里是要使用的代码:
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
android:id="@+id/main_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:fastScrollAutoHide="true"
app:fastScrollPopupBgColor="@color/colorAccent"
app:fastScrollPopupTextSize="56sp"
app:fastScrollPopupBackgroundSize="88dp"
app:fastScrollPopupTextColor="@android:color/primary_text_dark"
app:fastScrollThumbColor="@color/colorAccent" />
</RelativeLayout>
这段代码使用了 timusus' recyclerview,其中集成了一个快速滚动器。
但是,如果您对集成 fastscroller 不感兴趣,只需将小部件更改为:
<android.support.v7.widget.RecyclerView
android:id="@+id/main_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
最后,这里有几个链接进一步加强了使用 RecyclerView 的理由:
- Should we use RecyclerView to replace ListView?
- RecyclerView vs. ListView
在我看来,对于这种情况,使用 ListView 会更好,根据需要创建一个自定义适配器,在其中为每个项目填充和扩充自定义视图。例如,您需要一个由 textview 组成的视图,并为每个位置膨胀它。使用文件中的数据填充 ArrayList 并将其用于适配器。下面的例子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textColor="@color/black"/>
</LinearLayout>
我正在为我的所有适配器使用 ArrayList,并根据我的需要创建一个带有 getter 和 setter 函数的 class。下面的例子
public class DataSet {
private String data;
public DataSet(String data){
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
现在您需要为您的 ListView 创建一个适配器。下面的示例。
public class LetsMakeAnAdapter extends ArrayAdapter<DataSet> {
private ArrayList<DataSet> strings;
private Activity activity;
private static LayoutInflater inflater = null;
public LetsMakeAnAdapter(Context context, ArrayList<DataSet> item) {
super(context,R.layout.list_element, item);
this.strings = item;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View customViewToInflate = convertView;
if(customViewToInflate == null){
inflater = LayoutInflater.from(getContext());
customViewToInflate = inflater.inflate(R.layout.list_element, null);
}
TextView textViewExample = (TextView) customViewToInflate.findViewById(R.id.textView);
textViewExample.setText(strings.get(position).getData());
return customViewToInflate;
}
}
现在只需将 json 文件中的数据填充到 activity ArrayList 中,并将适配器设置为 ListView。示例如下。
DataSet objectOne = new DataSet("Value1");
DataSet objectTwo = new DataSet("Value2");
ArrayList<DataSet> dataSetValues = new ArrayList<>();
dataSetValues.add(objectOne);
dataSetValues.add(objectTwo);
LetsMakeAnAdapter adapter = new LetsMakeAnAdapter(getActivity(),dataSetValues);
listView.setAdapter(adapter);
我有一个与 returns 具有 X 个字符串的 JSON 服务通信的应用程序。
我希望每个字符串在 ScrollView 中都有自己的编辑文本或文本视图,但不知道如何做到这一点。
有人能告诉我方法吗?不求代码完成,只求如何实现。
您应该使用带有 ArrayAdapter 或更高效的 RecyclerView 的 ListView,而不是使用 ScrollView。 一些教程如何使用这个小部件:
- https://developer.android.com/training/material/lists-cards.html
- http://www.vogella.com/tutorials/AndroidListView/article.html
希望这对您有所帮助!
基本上,为了拥有动态滚动视图,建议像@Macimi 所说的那样使用 RecyclerView。为了进一步为您提供全面的演练,这里是要使用的代码:
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
android:id="@+id/main_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:fastScrollAutoHide="true"
app:fastScrollPopupBgColor="@color/colorAccent"
app:fastScrollPopupTextSize="56sp"
app:fastScrollPopupBackgroundSize="88dp"
app:fastScrollPopupTextColor="@android:color/primary_text_dark"
app:fastScrollThumbColor="@color/colorAccent" />
</RelativeLayout>
这段代码使用了 timusus' recyclerview,其中集成了一个快速滚动器。
但是,如果您对集成 fastscroller 不感兴趣,只需将小部件更改为:
<android.support.v7.widget.RecyclerView
android:id="@+id/main_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
最后,这里有几个链接进一步加强了使用 RecyclerView 的理由:
- Should we use RecyclerView to replace ListView?
- RecyclerView vs. ListView
在我看来,对于这种情况,使用 ListView 会更好,根据需要创建一个自定义适配器,在其中为每个项目填充和扩充自定义视图。例如,您需要一个由 textview 组成的视图,并为每个位置膨胀它。使用文件中的数据填充 ArrayList 并将其用于适配器。下面的例子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textColor="@color/black"/>
</LinearLayout>
我正在为我的所有适配器使用 ArrayList,并根据我的需要创建一个带有 getter 和 setter 函数的 class。下面的例子
public class DataSet {
private String data;
public DataSet(String data){
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
现在您需要为您的 ListView 创建一个适配器。下面的示例。
public class LetsMakeAnAdapter extends ArrayAdapter<DataSet> {
private ArrayList<DataSet> strings;
private Activity activity;
private static LayoutInflater inflater = null;
public LetsMakeAnAdapter(Context context, ArrayList<DataSet> item) {
super(context,R.layout.list_element, item);
this.strings = item;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View customViewToInflate = convertView;
if(customViewToInflate == null){
inflater = LayoutInflater.from(getContext());
customViewToInflate = inflater.inflate(R.layout.list_element, null);
}
TextView textViewExample = (TextView) customViewToInflate.findViewById(R.id.textView);
textViewExample.setText(strings.get(position).getData());
return customViewToInflate;
}
}
现在只需将 json 文件中的数据填充到 activity ArrayList 中,并将适配器设置为 ListView。示例如下。
DataSet objectOne = new DataSet("Value1");
DataSet objectTwo = new DataSet("Value2");
ArrayList<DataSet> dataSetValues = new ArrayList<>();
dataSetValues.add(objectOne);
dataSetValues.add(objectTwo);
LetsMakeAnAdapter adapter = new LetsMakeAnAdapter(getActivity(),dataSetValues);
listView.setAdapter(adapter);