长期单击停止单击ListView

时间:2016-08-13 05:41:06

标签: android listview

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

问题

我的点击手势用于注册,但在将LongClickListener附加到我的列表视图后,只有Long Click手势在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>

列表视图

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 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

更改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;
    }
});

答案 2 :(得分:1)

我找到了解决方案,现在看起来非常直观/愚蠢。也许有人可以验证我的假设?

看起来好像你启用了或者android:clickable =&#34; true&#34;机器人:LongClick =&#34;真&#34;在XML级别上,它将阻止程序化onClick Listeners运行

这对我来说是有道理的,因为我认为在XML级别上我们有onClick属性,当以编程方式实现点击时,该方法是空白的,视图正在寻找一个空白方法(即什么都不做)而不是onClick侦听器方法或者在设置onClick侦听器之前渲染视图?

不太确定,但这是我的解决方法(我的经验教训将使用程序设计或基于XML的点击方式)

ALTER TABLE tbl CONVERT TO CHARACTER SET utf8mb4 COLLATION utf8mb4_unicode_520_ci;

列出项目(请参阅没有可点击或可长时间的属性)

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;
            }
        });


    }