在ListView上长按停止点击行为

3
我已经阅读了所有与ListView的长按和点击监听器相关的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();
}

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>
3个回答

1
使用showPopUpMenu(view);替代showPopUpMenu(TheStreamActivity.this,view);
创建方法
public void showPopUpMenu(View v) {
PopupMenu popup = new PopupMenu(TheStreamActivity.this, 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();
}

不幸的是,我需要将上下文和位置信息传递给菜单,因此我不想删除上下文引用。除非你能想到其他的方法? - aquaflamingo

1

Change onItemLongClick methods to return false instead of true and by doing so you'll tell android that you want the event to still be handled by other listeners.

It should be look like this:

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


不幸的是,这就是代码最初的实现方式,但也无法工作。 - aquaflamingo

1
我已经找到了解决方案,现在看起来相当直观/愚蠢。也许有人可以验证我的假设?
似乎如果您在XML级别上启用或android:clickable="true" android:LongClick="true",它将阻止编程onClick监听器的功能。
这对我来说有些合理,因为我认为在XML级别上,我们有一个onClick属性,当通过程序实现点击时,该方法为空,视图正在寻找空方法(即不执行任何操作),而不是onClick监听器方法,或者视图在设置onClick监听器之前被渲染?
不太确定,但这是我的解决方法(无论如何,我的教训是使用基于程序或XML的可点击性)。
private void setItemOnClicks() {
        // TODO On Click Listeners not working
        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
                      */
                Toast.makeText(TheStreamActivity.this, "SELected", Toast.LENGTH_LONG).show();
                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.setLongClickable(true);

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


    }

列表项(请注意没有可点击或长按属性)
<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:divider="@android:color/transparent"
    android:dividerHeight="15dp">
<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp"
        android:foreground="?android:attr/selectableItemBackground">
        <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" />
        <!-- Flow Symbol Icon item_flow_name-->


        <ImageView
            android:contentDescription="@string/task_flag_description"
            android:id="@+id/item_element_icon"
            android:src="@drawable/flag_small_black_outline"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginEnd="125dp"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true" />
            <!-- Insert Flow Element Icon -->

        <TextView
            android:id="@+id/item_element_count"
            android:textAppearance=
                "?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/item_element_icon"
            android:layout_marginLeft="25dp" />

        <ImageView
            android:contentDescription="@string/timer_description"
            android:id="@+id/item_time_icon"
            android:src="@drawable/timer_black_48dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:layout_alignStart="@+id/item_element_icon"
            android:layout_toRightOf="@+id/item_element_icon"
            android:layout_marginLeft="55dp" />
        <!-- Insert Flow Element Icon -->

        <TextView
            android:id="@+id/item_total_time"
            android:textAppearance=
                "?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/item_time_icon"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="2dp" />

        <TextView
            android:id="@+id/item_time_units"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/item_time_icon"
            android:layout_marginLeft="50dp"
            android:layout_alignLeft="@+id/item_time_icon"
            android:layout_marginTop="2dp" />
        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接