我已经阅读了所有与ListView的长按和点击监听器相关的SO帖子,但是无法找到解决此问题的方法。根据我所阅读的帖子,我确信我的操作是正确的。
问题:
我的点击手势以前可以被注册,但是在将LongClickListener附加到我的ListView之后,只有长按手势可以在ListView中被注册。你对我做的事情有什么想法吗?
代码:
TheStreamActivity.java(ListView)
问题:
我的点击手势以前可以被注册,但是在将LongClickListener附加到我的ListView之后,只有长按手势可以在ListView中被注册。你对我做的事情有什么想法吗?
代码:
TheStreamActivity.java(ListView)
public class TheStreamActivity extends AppCompatActivity{
private ListView listView;
/** UI Actions and Set up */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_stream);
// Attach the adapter to a ListView
listView = (ListView) findViewById(R.id.stream_feed);
if (savedInstanceState!=null && !savedInstanceState.isEmpty())
{
lvContent = savedInstanceState.getParcelableArrayList(RESTORED_USER_FLOWS);
manager = savedInstanceState.getParcelable(RESTORED_MANAGER_UTIL);
} else {
lvContent = new ArrayList<>();
manager = new DataManagerUtil(this);
}
}
/** Sets up each of the individual list view items to be clicked and launch an
* new activity based on selected Flow Object.
*
*/
private void setItemOnClicks() {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* Passes Flow but passes the memory address of the childFlowElements
instead of the actual object containing the
*/
Flow selectedFlow = (Flow) listView.getItemAtPosition(position);
Intent i = new Intent(TheStreamActivity.this, FlowSandBoxActivity.class);
i.putExtra("selectedFlow", selectedFlow);
// Parcels the Flow Object to@ be passed to new activity
startActivity(i);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
showPopUpMenu(TheStreamActivity.this, view);
return true;
}
});
}
public void showPopUpMenu(Context ctx, View v) {
PopupMenu popup = new PopupMenu(ctx, v);
// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId())
{
case R.id.menu_delete:
return true;
default:
return false;
}
}
});
popup.inflate(R.menu.menu_flow_popup);
popup.show();
}
@Override
protected void onResume() {
super.onResume();
setItemOnClicks();
}
List Item
flow_item_in_stream.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="75dp"\
android:longClickable="true"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:textAppearance=
"?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:id="@+id/item_flow_name" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
列表视图
stream_feed.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="1">
<include layout="@layout/stream_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/stream_feed"
android:clipToPadding="false"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:layout_row="1">
</ListView>
</GridLayout>