0
我使用bindView()
方法是定製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
每個實例內的文本視圖的數量,以反映光標返回行值的數量。然而,每當我重新滾動列表項目時,該特定項目中的文本視圖的數量加倍,並且另外,綁定數據有時會反映在光標的位置和列表的位置之間對稱地反映項目。我認爲這個問題與回收舊文本視圖有關。
如何防止堆疊到舊文本視圖中的新文本視圖?是否有可能在特定子視圖的自定義遊標適配器中覆蓋視圖回收,並在屏幕關閉時強制垃圾回收?
這是自定義光標適配器的全implemtation
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);
}
}
}