我使用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