0

我正在使用选择器属性处理列表视图,当用户选择索引位置时,该索引位置应突出显示,直到我选择列表视图的下一个索引位置,选择器属性适用于此,但是当我滚动列表视图时,选择器不起作用它在顶部移动。请在这件事上给予我帮助

<ListView
        android:id="@+id/lvMenuItem"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@color/selected_color"
        android:dividerHeight="3dp" >
    </ListView>

lvMenu.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                //view.setSelected(true);
                Log.d("das","dasdas");
                if(lastSelected != null) {
                    lastSelected.setBackgroundColor(Color.TRANSPARENT);//(dr/*id of your unselected drawable*/);
                }
                lastSelected = (ViewGroup)view;
                lastSelected.setBackgroundResource(R.color.selected_color/*id of your selected drawable*/);
               // selectedRow=position; 
                String photo =datasource.getImagePathFromSubMenuId(SubMnuIdlst.get(position));
                Bitmap theImage= BitmapFactory.decodeFile(ofFilePath+"/"+photo);
                Bitmap bitmapScaled = Bitmap.createScaledBitmap(theImage, 300,250, true);
                drawable = new BitmapDrawable(bitmapScaled);
                imgMainItem.setBackgroundDrawable(drawable); 
                Imagename=ServerPath+"images/"+photo;
                String Desc=datasource.getSubMenuDescriptionFromSubMenuId(SubMenuid12);
                tvDesc.setText(Desc);
                selectionPos=position;
                selectedimgpath=photo;
                menuAdapter.notifyDataSetChanged();
                Log.d("SelPos",""+selectionPos);
            }
        }); 

图像的左侧是列表视图

4

1 回答 1

2

您应该跟踪 lastSelected 位置,并在 listView 的 onItemClickListener 中取消选择 lastSelected 位置并选择当前位置.. 像这样

ListView myListView = (ListView)findViewById(/* id of listView */);
ViewGroup lastSelected = null;
myListView.setOnItemClickListener(new onItemClickListener(
@Override
onItemClick(AdapterView arg0, View childView, int pos) {
    if(lastSelected != null) {
        lastSelected.setBackgroundDrawable(/*id of your unselected drawable*/);
    }
    lastSelected = (ViewGroup)childView;
    lastSelected.setBackgroundDrawable(/*id of your selected drawable*/);
    }
 ));
于 2013-07-23T09:04:04.210 回答