Question

I am working on listview with selector property, when the user selects the index position that index position should be highlighted until i select the next index position of listview, selector property works with this one but when i scroll my list view the selector doesnot works as it moves on the top . please help me with this

<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);
            }
        }); 

Left side of image is the listview

Was it helpful?

Solution

You should track the lastSelected position, and in listView's onItemClickListener deselect the lastSelected postion and select the current one.. something like this

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*/);
    }
 ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top