簡體   English   中英

Android-如何在不知道內部有多少個對象的情況下創建ScrollView?

[英]Android - How do I create a ScrollView without knowing how many objects it will have inside?

我有一個與服務對話的應用程序,該服務返回帶有X個字符串的JSON。

我希望每個字符串在ScrollView中都具有自己的Edit Text或Text View,但是不知道如何執行此操作。

有人可以告訴我它的方式嗎? 我不希望代碼完成,而只是想如何實現。

而不是使用ScrollView,您應該將ListView與ArrayAdapter一起使用或更有效地使用RecyclerView。 一些教程如何使用此小部件:

希望這會有所幫助!

基本上,為了有一個動態的滾動視圖,建議利用像什么@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 ,該視圖在其中集成了快速滾動器。

但是,如果您對集成快速滾動條不感興趣,只需將小部件更改為:

<android.support.v7.widget.RecyclerView
        android:id="@+id/main_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

最后,這里有一些鏈接可以進一步加強使用RecyclerView的原因:

對於這種情況,我認為最好使用ListView,並根據需要創建一個自定義適配器,然后在其中填充和填充每個項目的自定義視圖。 例如,您需要一個由和組成的視圖,並為每個位置充氣。 使用文件中的數據填充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函數的類。 下面的例子

    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文件中的數據填充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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM