问题
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 not work with MaterialRippleLayout on row item
i have also return true in OnLongClicked but does not work..
also tried and added method in MaterialRippleLayout.java
@Override
public void setOnLongClickListener(OnLongClickListener l) {
super.setOnLongClickListener(l);
childView.setOnLongClickListener(l);
}
and some changes in gesturedetectore
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 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;
}
});
sory for bad english
回答1:
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()
.
So in your 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.
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.
回答2:
Actually there is a bug in material-ripple library , already reported there,https://github.com/balysv/material-ripple/issues/15
i have solved my issue,
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;
}
});
来源:https://stackoverflow.com/questions/27374594/prevent-shortclick-when-longclick-pressed