【问题标题】:Android touchlistener for customadapter listview用于自定义适配器列表视图的 Android 触摸监听器
【发布时间】:2015-05-04 03:56:09
【问题描述】:

我已经为带有点击监听器的列表视图实现了一个自定义适配器。现在我想扩展代码以包含一个触摸监听器来更改背景颜色(ACTION_DOWN 和 ACTION_UP)。

我遵循一个在线示例,这是我当前的代码

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======");
}

我如何以相同的方式实现触摸监听器(或者不能这样做?)到目前为止,我已经尝试在 getView 方法中注册触摸监听器,但它带走了点击监听器,我也有不知道为什么它不起作用。这是我正在尝试实现的触摸监听器:

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

这是我的列表视图 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>

这是我的适配器 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>

新增(据评论)

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

【问题讨论】:

  • 您需要 OnTouchListener 做什么?只是为了改变背景颜色?
  • 这也是我想知道的。
  • 是的,目前只是为了改变每个列表视图项的背景颜色
  • 我已经发布了一个答案,如果有帮助,请告诉我。

标签: java android listview android-listview custom-adapter


【解决方案1】:

如果您只是想做一些背景颜色更改,那么我建议您使用 XML 创建 listSelector。它非常简单,并且可以使用渐变使视图变得更加可爱。

看看它是如何工作的Custom selector for list background

【讨论】:

  • 它对我不起作用。可能是因为我使用的是自定义适配器吗?
  • 解决了。 listector 应该在 标记内。
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 2023-03-15
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多