gpt4 book ai didi

android - 在 ListView 上长按停止单击操作

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:55 25 4
gpt4 key购买 nike

我已经通读了 ListView 的所有 longclick + click 监听器 SO 帖子,但找不到解决此问题的方法。根据我读过的帖子,我确信我正在做正确的事情。

问题

我的点击手势用于注册,但在将 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();
}

列表项

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>

ListView

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>

最佳答案

更改 onItemLongClick 方法以返回 false 而不是 true 并且通过这样做你会告诉 android 你希望事件仍然是由其他听众处理。

它应该是这样的:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
showPopUpMenu(TheStreamActivity.this, view);
return false;
}
});

关于android - 在 ListView 上长按停止单击操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38929627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com