按下longClick时防止shortClick [英] prevent shortClick when longClick pressed
问题描述
我想在LongClicked一个项目时取消shortClick, 我在列表视图中使用涟漪效果Material-ripple 库,但是当我长按列表项时也叫onClick-event
I want to cancel shortClick when LongClicked an item, I'm using ripple effect material-ripple library in list view but when i long press list item it also call onClick-event
list.setOnItemClickListner不适用于行项目上的MaterialRippleLayout
list.setOnItemClickListner not work with MaterialRippleLayout on row item
我在OnLongClicked中也返回true,但不起作用.
i have also return true in OnLongClicked but does not work..
这是我的代码list_item
here is my code list_item
<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代码
& java code
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;
}
});
英语不好对不起
推荐答案
您似乎正在使用ListView
,而list_item
是每一行的xml.如果我错了,请立即停止阅读!但是,如果我是对的,我们应该考虑使用方法setItemClickListener()
和setOnItemLongClickListener()
.
It looks like you are using a ListView
and list_item
is the xml for each row. If I'm wrong, stop reading now! But if I'm right, we should consider using the methods setItemClickListener()
and 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.
}
});
请注意,位置参数是通过这两种方法传递的,因此只要您对要显示的数据集有引用,就可以访问单击或长按的确切项目.
Notice the position parameter gets passed in these two methods, so as long as you have a reference to the data set that is being displayed, you can access the exact item that was clicked or long clicked.
我很难说出您的代码为什么不起作用,因此有些人可能不会认为这是一个答案,但是我希望这会有所帮助.我所知道的是,通过适配器在列表视图内的各个视图上设置单击侦听器时,事情可能会变得非常混乱.
It's hard for me to tell why your code wasn't working, so some might not consider this an answer, but I hope this can help. All I know is that things can get really messy when setting click listeners on individual views inside a list view through the adapter.
作为旁注,如果我们要在长按单击操作中执行的唯一操作是显示上下文菜单,那么我们应该考虑使用registerForContextMenu(listView)
,然后使用上下文菜单的生命周期. 可以在此处找到有关该做法的更多文档.
As a side note, if the only action we want to execute in the on long click action is showing the context menu, then we should consider using registerForContextMenu(listView)
and then using the context menu life cycle. One can find more documentation on that practice here.
这篇关于按下longClick时防止shortClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!