我使用bindView()
方法是自定義CursorAdapter
實現動態添加文本視圖到列表。如何在自定義`CursorAdapter`中重新使用動態添加的視圖
每個列表項被list_item
佈局其含有來自Android Flowlayout
<!--List Item Layout-->
<?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="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF">
<!--Flow Layout-->
<org.apmem.tools.layouts.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/view_padding"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:id="@+id/hash_tag_layout"
>
</org.apmem.tools.layouts.FlowLayout>
</LinearLayout>
flow_layout
佈局的加入flow_layout
每list item
每個實例文本視圖的數量反映了光標返回的行值的數量來表示。
public void bindView(View view, Context context, Cursor cursor) {
// Flow layout wraps new views around the screen.
FlowLayout flowLayout = (FlowLayout) view.findViewById(R.id.flow_Layout);
// getRowValues() puts row values from cursor into an array list.
ArrayList<> rowValues = getRowValues(cursor);
// A new text view is created and inserted into Flow layout
// for each value in rowValues
TextView tv;
for value in rowValues {
tv = = new TextView(ctx);
tv.setText(value);
flowLayout.addView(tv);
}
}
再次重申,我希望每個flow_layout
每list_item
每個實例內的文本視圖的數量,以反映光標返回行值的數量。然而,每當我重新滾動列表項目時,該特定項目中的文本視圖的數量加倍,並且另外,綁定數據有時會反映在光標的位置和列表的位置之間對稱地反映項目。我認爲這個問題與回收舊文本視圖有關。如何防止堆疊到舊文本視圖中的新文本視圖?
這是自定義光標適配器
public class DatabaseAdapter extends CursorAdapter {
Context ctx;
public DatabaseAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
ctx = context;
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
return v;
}
public void bindView(View view, Context context, Cursor cursor) {
// Flow layout wraps new views around the screen.
FlowLayout flowLayout = (FlowLayout) view.findViewById(R.id.flow_Layout);
// getRowValues() puts row values from cursor into an array list.
ArrayList<> rowValues = getRowValues(cursor);
// A new text view is created and inserted into Flow layout
// for each value in rowValues array list
TextView tv;
for value in rowValues {
tv = = new TextView(ctx);
tv.setText(value);
flowLayout.addView(tv);
}
}
}
設置ID您'tv1',然後你可以調用'findViewById'檢查,如果該視圖已經添加或不添加,但實際上爲什麼不在'R.layout.list_item'中添加'TextView'? – pskink
@pskink謝謝。這工作。實際上,我嘗試設置ID並使用'findViewById',但我沒有考慮在'createView'方法中使用'findViewById',而是在'bindView'中使用它'...有什麼區別? –
爲什麼不在'R.layout.list_item'中添加'TextView'? – pskink