Android: кнопки в составном элементе управления, взаимодействующие с ContextMenu ListView - PullRequest
3 голосов
/ 30 августа 2011

Проблема, с которой я столкнулся здесь, состоит в том, что две кнопки в элементе списка xml, кажется, мешают работе с 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;
}

Ответы [ 2 ]

3 голосов
/ 01 сентября 2011

Я наконец понял, как решить проблему с помощью кнопок, мешающих работе с 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" />
0 голосов
/ 30 августа 2011

Я сделал с просмотром списка по клику

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

Спасибо.

...