【问题标题】:Android: Buttons in compound control interfering with ListView's ContextMenuAndroid:复合控件中的按钮干扰 ListView 的 ContextMenu
【发布时间】:2011-08-30 11:08:19
【问题描述】:

我在这里遇到的问题是列表项 xml 中的 2 个按钮似乎干扰了主要活动中的 ContextMenu,防止它在长按列表项时膨胀。请注意,按钮本身可以正常工作。当我删除它们时,ContextMenu 可以完美运行。

列表项xml:

<com.anna.mytallykeeper.views.TallyItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/item_description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textSize="40px"
    android:textColor="@color/dijon" />
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:id="@+id/decrement_button"
        android:layout_width="80px"
        android:layout_height="80px"
        android:layout_centerVertical="true"
        android:background="@drawable/minus_button_1" />
    <Button android:id="@+id/increment_button"
        android:layout_width="80px"
        android:layout_height="80px"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/add_button_1" />
    <TextView android:id="@+id/item_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/increment_button"
        android:layout_toRightOf="@id/decrement_button"
        android:gravity="center_horizontal"
        android:textSize="80px"
        android:textColor="@color/dijon" />
</RelativeLayout>
</com.anna.mytallykeeper.views.TallyItemView>

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/piplup">
    <ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

从主活动(从 ListActivity 继承):

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    app = (MyTallyKeeperApplication) getApplication();
    adapter = new TallyItemListAdapter(this, app.getTallyItems());
    setListAdapter(adapter);
    Utility.logI((getListView() == null ?
            "ListView is null" : "ListView is not null"));
    Utility.logI((getListView().getId() ==
            android.R.id.list ? "ListView ID is correct" :"ListView ID is not correct"));
    registerForContextMenu(getListView());
}
//
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)     
{
    Utility.logI(MyTallyKeeperMain.class.getSimpleName() + ".onCreateContextMenu");
    super.onCreateContextMenu(menu, v, menuInfo);

    if (v.getId() == android.R.id.list) {
        getMenuInflater().inflate(R.menu.item_menu, menu);
    }
}

从列表适配器类(继承自 BaseAdapter):

    public View getView(int position, View view, ViewGroup parent) {
    TallyItemView tiv;
    if (view == null) {
        tiv = (TallyItemView)View.inflate(context, R.layout.tally_item_row, null);
    } else {
        tiv = (TallyItemView)view;
    }
    tiv.setTallyItem(tallyItems.get(position));
    tiv.getDecrementButton().setOnClickListener(new DecrementListener(position));
    tiv.getIncrementButton().setOnClickListener(new IncrementListener(position));

    return tiv;
}

【问题讨论】:

  • 表示你想在按钮点击时打开上下文菜单?
  • 长按列表项

标签: android contextmenu android-listview textview android-button


【解决方案1】:

我终于想出了如何解决按钮干扰 ContextMenu 的问题:将每个按钮的可聚焦属性设置为 false。

    <Button android:id="@+id/increment_button"
        android:layout_width="80px"
        android:layout_height="80px"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:background="@drawable/add_button_1" />

【讨论】:

    【解决方案2】:

    我已经完成了 onClick 列表视图

    public void onItemClick(AdapterView<?> view, View arg1, int arg2,
                        long id) {
    
                    openContextMenu(arg1);
    

    长按

     registerForContextMenu(list);
    

    更多详情http://groups.google.com/group/android-developers/browse_thread/thread/fe5691bf03af949c

    谢谢。

    【讨论】:

    • 我刚试过这样做,但没有运气。我认为我的应用程序的问题在于每个 ListItem 都包含按钮。
    • 我终于弄清楚了它的问题所在。无论如何感谢您的建议。
    猜你喜欢
    • 2019-06-08
    • 2020-06-02
    • 1970-01-01
    • 2012-12-08
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多