【问题标题】:prevent shortClick when longClick pressed按下长按时防止短按
【发布时间】:2014-12-09 08:43:06
【问题描述】:

我想在 LongClicked 一个项目时取消 shortClick, 我在列表视图中使用ripple effect material-ripple 库,但是当我长按列表项时,它也会调用 onClick-event

list.setOnItemClickListner 不适用于行项目上的 MaterialRippleLayout

我在 OnLongClicked 中也返回 true 但不起作用..

也在MaterialRippleLayout.java中尝试并添加了方法

   @Override
   public void setOnLongClickListener(OnLongClickListener l) {
    super.setOnLongClickListener(l);
     childView.setOnLongClickListener(l);
}

以及gesturedetectore的一些变化

这是我的代码列表项

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center" >

<com.balysv.materialripple.MaterialRippleLayout
    android:id="@+id/list_item_ripple"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    app:rippleAlpha="0.2"
    app:rippleColor="#585858"
    app:rippleDelayClick="true"
    app:rippleHover="true"
    app:rippleOverlay="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:gravity="left"
                android:text="Name/Number"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView2_dur"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:gravity="right"
                android:text="Duration"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp" >

            <TextView
                android:id="@+id/textView3_in_out"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:text="Incoming/Outgoing"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/textView4_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:gravity="right"
                android:text="Time"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </TableRow>
    </LinearLayout>
</com.balysv.materialripple.MaterialRippleLayout>

&java代码

      holder.riple.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(getActivity(), "item riple clicked "+position, 0).show();
                    Intent intent = new Intent(getActivity(), PlayerActivity.class);
                    intent.putExtra("songs", list_path.get(position).toString());
                    intent.putExtra("name", ""+parts[0]);
                    intent.putExtra("dur", ""+convertMillis(parts[2]));
                    intent.putExtra("time", getDate(parts[1].toString()));
                    startActivity(intent);
                }
            });
            holder.riple.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    list.showContextMenu();
                    //Toast.makeText(getActivity(), "LongClick", 0).show();
                    //v.setClickable(false);//v.isClickable();
                    return true;
                }
            });

对不起英语不好

【问题讨论】:

    标签: java android android-listview android-5.0-lollipop rippledrawable


    【解决方案1】:

    看起来您使用的是ListView,而list_item 是每一行的xml。如果我错了,请立即停止阅读!但如果我是对的,我们应该考虑使用方法setItemClickListener()setOnItemLongClickListener()

    所以在你的Activity:

        listView = (ListView) findViewById(R.id.listView);
    
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            }
        });
    
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                return true; // return true to prevent the `onClick` from being triggered as well.
            }
        });
    

    注意 position 参数是在这两个方法中传递的,所以只要你有对正在显示的数据集的引用,你就可以访问被点击或长按的确切项目。

    我很难说出你的代码为什么不起作用,所以有些人可能不认为这是一个答案,但我希望这能有所帮助。我所知道的是,当通过适配器在列表视图中的单个视图上设置点击侦听器时,事情会变得非常混乱。

    附带说明,如果我们想要在长按操作中执行的唯一操作是显示上下文菜单,那么我们应该考虑使用registerForContextMenu(listView),然后使用上下文菜单生命周期。 One can find more documentation on that practice here.

    【讨论】:

    • 对不起,我没有提到,但 listView.setOnItemClickListner 对我不起作用,如果我从 xml 中删除 MaterialRippleLayout 比 itemClick .. 工作正常...具有波纹效果 itemClick 不起作用
    【解决方案2】:

    其实有一个bug in material-ripple library,那里已经报过了,https://github.com/balysv/material-ripple/issues/15

    我的问题已经解决了,

    boolean isLongClick = false;

        holder.riple.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    Toast.makeText(getActivity(), "item riple clicked "+position, 0).show();
                    Intent intent = new Intent(getActivity(), PlayerActivity.class);
                    intent.putExtra("songs", list_path.get(position).toString());
                    intent.putExtra("name", ""+parts[0]);
                    intent.putExtra("dur", ""+convertMillis(parts[2]));
                    intent.putExtra("time", getDate(parts[1].toString()));
                    if(isLongClick== false)
                    {
                    startActivity(intent);
                    }
                     // for click agin on item
                    isLongClick = false;
                }
            });
            holder.riple.setOnLongClickListener(new OnLongClickListener() {
    
                @Override
                public boolean onLongClick(View v) {
                    isLongClick = true;
                    list.showContextMenu();
                    //Toast.makeText(getActivity(), "LongClick", 0).show();
                    //v.setClickable(false);//v.isClickable();
                    return true;
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 2012-08-31
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多