滚动ListView时项目的可见性会发生变化

时间:2012-06-28 13:30:25

标签: android

我正在使用列表视图和适配器来加载列表,每个列表项都有一个TextView,EditText和Image ..我根据列表行的位置设置箭头的可见性和编辑文本,一切我第一次加载列表时工作正常... 但是当我滚动列表时,项目的可见性不断变化......请在这个问题上帮助我...相关的代码已被附加...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="horizontal"  android:background="#FFFFFF">
    <TextView android:layout_height="wrap_content"  android:layout_width="0dip"
            android:textSize="20dip" android:layout_weight="1"
            android:id="@+id/textview_add_lot_list" android:textColor="@android:color/black"
            android:paddingTop="10dip" android:paddingBottom="10dip"
            android:paddingLeft="10dip"/>
    <EditText android:layout_height="fill_parent" android:layout_width="0dip"
            android:layout_weight="1" android:id="@+id/et_add_lot_list"
            android:layout_gravity="center_vertical"/>
    <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content"
            android:id="@+id/imageview_arrow_add_lot_list" android:layout_gravity="center_vertical"
            android:visibility="invisible" android:src="@drawable/more_reviews_arrow"
            android:paddingRight="10dip"/>
</LinearLayout>

Java代码活动......

final ArrayList<String> listItems       =   new ArrayList<String>();
        listItems.add("Parking name");
        listItems.add("Address");
        listItems.add("City");
        listItems.add("State");
        listItems.add("Zip");
        listItems.add("Phone");
        listItems.add("Web Address");
        listItems.add(" ");
        listItems.add("Parking Image");
        listItems.add(" ");
        listItems.add("Open Hours");
        listItems.add(" ");
        listItems.add("Web Reviews");

        final AddParkingLotAdapter adapter  =   new AddParkingLotAdapter(mAppContext,0,listItems);
        lv.setAdapter(adapter);

Java代码...适配器

public class AddParkingLotAdapter extends ArrayAdapter<String> {
    private ArrayList<String> mStrings;
    private LayoutInflater mInflater;
    private AppContext mContext;

    private static int NON_EMPTY_ROW    =   1;                                                                                                          
    private static int EMPTY_ROW       =    0;

    public AddParkingLotAdapter(Context context, int resId, List<String> strings) {
        super(context, resId,strings);
        mStrings        =   (ArrayList<String>) strings;
        mContext        =   (AppContext) context;
        mInflater       =   LayoutInflater.from(context);
    }
    @Override
    public int getViewTypeCount() {
        return 2;                               
    }
    @Override
    public int getCount() {
        return mStrings.size();     
    }
    @Override
    public String getItem(int position) {
        return mStrings.get(position);
    }
    @Override
    public int getItemViewType(int position) {
        if(position==7||position==9||position==11){
            return EMPTY_ROW;
        }else{
            return NON_EMPTY_ROW;
        }
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView itemTextView   =   null;

        //different inflations for different type rows..
        if(getItemViewType(position) == EMPTY_ROW){
            if (convertView == null) {
                convertView         =   mInflater.inflate(R.layout.review_empty_row, null);
            }
        }else if(getItemViewType(position) == NON_EMPTY_ROW){
            if (convertView == null) {
                convertView     =   mInflater.inflate(R.layout.add_parkinglist_item, null);
            }
            itemTextView        =   (TextView) convertView.findViewById(R.id.textview_add_lot_list);
            itemTextView.setText(mStrings.get(position));
            if (position==3||position==8||position==10||position==12){
                ImageView itemImageView       =   (ImageView)convertView.findViewById(R.id.imageview_arrow_add_lot_list);
                itemImageView.setVisibility(View.VISIBLE);
                EditText editText             =   (EditText)convertView.findViewById(R.id.et_add_lot_list);
                editText.setVisibility(View.INVISIBLE);
            }
        }
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:5)

在此代码中:

if (position==3||position==8||position==10||position==12){
    ImageView itemImageView       =   (ImageView)convertView.findViewById(R.id.imageview_arrow_add_lot_list);
    itemImageView.setVisibility(View.VISIBLE);
    EditText editText             =   (EditText)convertView.findViewById(R.id.et_add_lot_list);
    editText.setVisibility(View.INVISIBLE);
}

你没有 else 条款。这意味着如果position为0,1,2,4,5或6,则不会明确设置视图的可见性,因此可见性将是视图被回收时设置的任何内容。如果convertView非空,则您始终需要重置其可见性可能已在之前修改过的任何项目的可见性。