[英]Android touchlistener for customadapter listview
I'm have implemented a custom adapter for a listview with click listener. 我已经使用click监听器为listview实现了一个自定义适配器。 Now I would like to extend the code to include a touch listener to change the background color (ACTION_DOWN & ACTION_UP). 现在我想扩展代码以包含一个触摸侦听器来更改背景颜色(ACTION_DOWN&ACTION_UP)。
I follow an online example and this is my current code 我按照在线示例,这是我目前的代码
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.sharer, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text1 = (TextView) vi.findViewById(R.id.text1);
holder.text2=(TextView)vi.findViewById(R.id.text2);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text1.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( Sharer ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.text1.setText( tempValues.getName() );
holder.text2.setText(String.valueOf(tempValues.getAmount()));
/******** Set Item Click Listener for LayoutInflater for each row *******/
vi.setOnClickListener(new OnItemClickListener( position ));
}
return vi;
}
private class OnItemClickListener implements OnClickListener
{
private int mPosition;
OnItemClickListener(int position)
{
mPosition = position;
}
@Override
public void onClick(View v) {
SharerActivity act = (SharerActivity) activity;
act.onItemClick(mPosition);
}
}
@Override
public void onClick(View v) {
Log.v("SharerAdapter", "====== Row Button Click======");
}
How do I implement the touch listener in the same way (or it cannot be done this way?) So far, I have tried to register the touch listener inside the getView method but it takes away the click listener and I too have no idea why it doesn't work. 我如何以相同的方式实现触摸侦听器(或者不能以这种方式实现?)到目前为止,我已经尝试在getView方法中注册触摸侦听器,但它会夺走点击侦听器,我也不知道为什么它不起作用。 This is the touch listener I am trying to implement: 这是我试图实现的触摸监听器:
private class OnTouchListener implements View.OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(Color.GREEN);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
v.setBackgroundColor(Color.TRANSPARENT);
return true;
}
return false;
}
}
This is my listview xml 这是我的listview xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/sharerlistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:listSelector="@drawable/item"/>
</RelativeLayout>
this is my adapter xml 这是我的适配器xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="0dip" android:layout_gravity="top"
>
<TableRow>
<TextView
android:id="@+id/text1"
android:layout_height="75px"
android:layout_width="wrap_content"
android:layout_weight="2" android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:layout_span="1"
/>
<TextView
android:text=""
android:id="@+id/text2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:gravity="left"/>
</TableRow>
</TableLayout>
newly added (accordingly to comment) 新增(相应评论)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/bg"/>
</selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_red_dark"/>
</shape>
If you just wanted to do some backgroundColor change, then i will suggest you to create listSelector using XML. 如果你只想做一些backgroundColor更改,那么我建议你使用XML创建listSelector。 It's so easy and can make the view much cute using gradients. 它非常简单,可以使用渐变使视图更加可爱。
See how it works Custom selector for list background 了解它的工作原理列表背景的自定义选择器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.