我已经实现了自定义SimpleCursorAdapter
,以便将图片和文字插入我的ListView
,但是当用户滚动时图片会移动。我怎样才能让他们留在原地?这是我的代码(为了简单起见,我将其剥离了):
final class mySimpleCursorAdapter extends SimpleCursorAdapter {
public mySimpleCursorAdapter(Context context, int layout, Cursor cur,
String[] from, int[] to) {
super(context, layout, cur, from, to);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.row, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
ImageView bar11 = (ImageView) v.findViewById(R.id.bar);
ImageView bar12 = (ImageView) v.findViewById(R.id.bar2);
ImageView bar13 = (ImageView) v.findViewById(R.id.bar3);
TextView sign = (TextView) v.findViewById(R.id.text3);
sign.setText(currency);
if (someValue == 0){
bar11.setVisibility(View.GONE);
}
if (someValue == 1){
bar12.setVisibility(View.VISIBLE);
}
if (someValue == 2){
bar13.setVisibility(View.VISIBLE);
}
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/bar"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:background="@drawable/greenbar"/>
<ImageView
android:id="@+id/bar2"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:background="@drawable/two"/>
<ImageView
android:id="@+id/bar3"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:background="@drawable/two"/>
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
List.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/expense"
android:background="@drawable/expensebackgroundblue">
<ListView
android:id="@+id/contentlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
android:cacheColorHint="#00000000" >
</ListView>
</RelativeLayout>
答案 0 :(得分:1)
问题正在发生,因为查看回收您需要做的事情,将视图可见性设置为可见或像这样
@Override
public void bindView(View v, Context context, Cursor c) {
ImageView bar11 = (ImageView) v.findViewById(R.id.bar);
ImageView bar12 = (ImageView) v.findViewById(R.id.bar2);
ImageView bar13 = (ImageView) v.findViewById(R.id.bar3);
TextView sign = (TextView) v.findViewById(R.id.text3);
sign.setText(currency);
if (someValue == 0){
bar11.setVisibility(View.GONE);
}else
{
bar11.setVisibility(View.VISIBLE);
}
if (someValue == 1){
bar12.setVisibility(View.VISIBLE);
}else
{
bar12.setVisibility(View.GONE);
}
if (someValue == 2){
bar13.setVisibility(View.VISIBLE);
}else
{
bar13.setVisibility(View.GONE);
}
}