设置背景图片,如果自定义列表视图产品pressed [英] Set Background image, if the Custom Listview item is pressed

查看:116
本文介绍了设置背景图片,如果自定义列表视图产品pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的使用自定义列表视图基础适配器

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>

和listbackground.xml

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>

当我点击商品图片背景只是闪烁,但我想它应该被激活
和显示背景。
我用的android:ATTR / activatedBackgroundIndicator 自定义列表视图,但它并不适用于API低于11级的工作。

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.

推荐答案

在你的ListView您将使用该自定义适配器

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

}

这篇关于设置背景图片,如果自定义列表视图产品pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆