2016-12-02 32 views
0

我有一個應用程序與一個返回一個帶有X個字符串的JSON的服務通信。Android - 如何創建一個ScrollView而不知道里面有多少物體?

我希望這些字符串中的每一個在ScrollView中都有自己的編輯文本或文本視圖,但不知道如何執行此操作。

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

+0

這聽起來像是一個RecyclerView可能更合適 –

回答

0

對於這種情況,在我看來,使用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函數的類對我的需要。下面

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); 
+0

非常感謝你,正是用這裏所說的創建我自己的CustomAdapter並使用ListView。 – Guigs

+0

歡迎,快樂編碼。 –

0

基本上爲了有一個動態的滾動視圖,建議像@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。

但是,如果你不感興趣的整合fastscroller,簡單的改變widget中這樣的:

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

最後,這裏有幾個環節,進一步強化的理由使用RecyclerView:

+0

非常感謝!現在我不明白爲什麼要使用RecyclerView。決定創建我自己的CustomAdapter並使用ListView – Guigs

相關問題