开发者

Android: Buttons in compound control interfering with ListView's ContextMenu

开发者 https://www.devze.com 2023-04-01 12:35 出处:网络
The problem I\'ve been having here is that the 2 buttons in the list item xml seems to be interfering with the ContextMenu in the main activity, preventing it from inflating when a list item is long-p

The problem I've been having here is that the 2 buttons in the list item xml seems to be interfering with the ContextMenu in the main activity, preventing it from inflating when a list item is long-pressed. Just as a note, the buttons themselves work fine. When I removed them, the ContextMenu works perfectly.

The list item 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>

From the main activity (inherits from 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);
    }
}

From the list adapter class (inherits from 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;
}


I finally figured out how to fix the issue with the buttons interfering with the ContextMenu: set each button's focusable attribute to 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" />


I have done with list view onClick

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

                openContextMenu(arg1);

and for long click

 registerForContextMenu(list);

for more details http://groups.google.com/group/android-developers/browse_thread/thread/fe5691bf03af949c

Thank you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号