问题
I am using custom list view using base adapter
<ListView
android:id="@+id/lstHome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:drawSelectorOnTop="false"
android:cacheColorHint="#3d4241"
android:clickable="true"
android:listSelector="@drawable/listbackground">
</ListView>
and listbackground.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/list_background" android:state_pressed="true"/>
<item android:drawable="@drawable/list_background" android:state_pressed="false" android:state_selected="true"/>
</selector>
when i click on item the image background is just flashing but i want it should be activated
and show the background.
i used android:attr/activatedBackgroundIndicator in custom listview but it does not work for api below level 11.
回答1:
You will use this custom adapter in you listview
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public int[] i ;
public int start=0;
private LinearLayout layout;
private View view;
private View mLastView;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
start=0;
this.context = context;
i = new int[this.mlist.size()];
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
@Override
public Sample getItem(int position) {
return mlist.get(position);
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
view = inflater.inflate(R.layout.listitem, null);
layout = (LinearLayout)view.findViewById(R.id.linearlayoutSample);;
TextView text1 = (TextView) view.findViewById(R.id.item1);
TextView text2 = (TextView) view.findViewById(R.id.item2);
layout.setBackgroundColor(Color.BLACK);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(start==0)
{
mLastView = v;
start++;
}
if(start>0)
{
mLastView.setBackgroundColor(Color.BLACK);
mLastView = v;
}
v.setBackgroundColor(Color.GRAY);
}
});
return view;
}
}
回答2:
Instead of listselector, you can try this:
Create bg_key.xml:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
Then include this background to your listview:
android:background="@drawable/bg_key"
And then, in your activity, create an onclick listner for your listview:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
}
}
The color code in bg_key, is up to you..
回答3:
you could try this with the previous version of your code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_pressed="false"
android:state_selected="false"/>
<item android:drawable="@drawable/list_background" android:state_pressed="true"/>
<item android:drawable="@drawable/list_background" android:state_selected="true"/>
<item android:drawable="@drawable/list_background" android:state_active="true"/>
</selector>
Then in the onClick of the view, get the relativeLayout or the LinearLayout inflating the row, and set its activated and also selected property to true, and that way i think you will get a background that remains.
Hope that helps.
来源:https://stackoverflow.com/questions/22728444/set-background-image-if-the-custom-listview-item-is-pressed