如果按下自定义列表视图项,请设置背景图像

时间:2014-03-29 07:45:15

标签: android android-listview

我使用基本适配器

使用自定义列表视图
<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>

和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>

当我点击项目时,图像背景只是闪烁,但我希望它应该被激活 并显示背景。 我在自定义列表视图中使用了android:attr/activatedBackgroundIndicator,但它不适用于11级以下的api。

3 个答案:

答案 0 :(得分:1)

您可以尝试以下操作代替listselector:

创建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>

然后将此背景包含在列表视图中:

android:background="@drawable/bg_key"  

然后,在您的活动中,为列表视图创建一个onclick列表器:

  listView.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
      view.setSelected(true);

  }
}

bg_key中的颜色代码取决于您..

答案 1 :(得分:0)

您将在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 :(得分:0)

您可以使用以前版本的代码尝试此操作:

<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>

然后在视图的onClick中,获取relativeLayout或LinearLayout使行膨胀,并将其激活且也选择的属性设置为true,这样我认为您将获得剩余的背景。

希望有所帮助。

相关问题