Unselect The Listview Item Onclick In Android
Solution 1:
I got solution for your problem. Do as following.
1) open your main layout file where ListView you have created.
Add android:choiceMode="singleChoice"
. This will look like below.
<ListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:choiceMode="singleChoice" ></ListView>
2) Open your list_item.xml layout file. In which, to your root view, add android:background="?android:attr/activatedBackgroundIndicator"
. In my sample project, its look like below.
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="?android:attr/activatedBackgroundIndicator"android:orientation="vertical" >
//your views
</LinearLayout>
3) Open your activity file. After setting adapter to your ListView
, add list.setSelector(R.drawable.selection_effect);
. This will look like below.
ListViewls= (ListView) findViewById(R.id.listView1);
ListAdapteradapter=newListAdapter(this, data);
ls.setAdapter(adapter);
ls.setSelector(R.drawable.selection_effect);
Here, selection_effect
is drawable file which you have created in drawable
directory.
I tested my code. Which is working fine.
4) To select first view by default, remove your code in BaseAdapter
and put following code after completing 3rd step.
ls.setItemChecked(0,true);
You need to put it after above code like below.
ListAdapteradapter=newListAdapter(data);
ls.setAdapter(adapter);
ls.setSelector(R.drawable.selection_effect);
ls.setItemChecked(0, true);
Explanation
ls.setSelector(R.drawable.selection_effect);
This will select row item based on selector
you have defined in drawable directory.
ls.setItemChecked(0, true);
This will select first item by default at first time run. After you can select other items by clicking on them.
Solution 2:
You could just declare an int for item clicked which defaults to the first item that starts clicked, then in onclick update accordingly.
int selected = <default>;
set in oncreate etc.
Then you can have onItemClicked listener and do this,
publicvoidonItemClick(AdapterView<?> adapterView, View view, int i, long l){
if (selected != i){
<listView>.getChildAt(selected).setBackgroundColor(getResources().getColor(android.R.color.background_dark));
<listView>.getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
selected = i;
}
}
This will change the background colour of the previously selected item back to (in my case) the default android dark theme colour, set the newly selected one to a nice highlighted light blue, and update the selected int to reflect the newly selected item.
Solution 3:
You can try like this:
after setting adapter for ListView set position to 0 for default selection i.e. listView.setSelection(0);
and in onItemClick
you get selected position (3rd parameter) of item, so inside onItemClick write listView.setSelection("that 3rd param");
Solution 4:
I have had similar problem with ListView all you do is when item is clicked call a method on adapter, adadpter.SetPosition(position) {mSelectedPsoiton = position; notifyDataSetChanged();}
, and in your getView function check if the position is equal to selectedPosition, then set background accordingly this never fails, focus does not work as ur in touch mode always
//adapter classpublicoverride View GetView(int position, View convertView, ViewGroup parent) { TextView view = null; int lookupPos = position; if (null == convertView) { view = new TextView(_context); view.SetTextSize(ComplexUnitType.Sp, 20); view.SetPadding(_pixels, _pixels, _pixels, _pixels); } else { view = convertView as TextView; }
if (position == mSelectedPos )
{
view.SetBackgroundResource(Android.Resource.Color.HoloBlueDark);
}
else
{
view.SetBackgroundResource(Resource.Drawable.listItemSelector);
}
return view;
}
publicvoidSetSelectedPosition(int position) { mSelectedPos = position;
}
privateint mSelectedPos = -1;
// ListView code, _adapter is adapter of listview
listView.SetOnItemClickListener (new OnItemClickListener() { @Override publicvoidonItemClick(AdapterView adapter, View view, int pos, long id) { _adapter.SetSelectionPostion(pos); }
}
Post a Comment for "Unselect The Listview Item Onclick In Android"