6

リスト項目が OnClick イベントに応答する ListActivity を拡張するクラスがあります。OnItemLongClickListener を追加しても機能しません。onItemLongClick() 関数は呼び出されませんが (ログ出力やトーストは表示されません)、代わりに通常の OnClick() イベントが処理されます。

ロングクリック時にコンテキスト アクション バーを表示したい。新しいプロジェクトで私のコードを使用した最小限の例は正常に動作します。私の質問は次のとおりです: onItemLongClick() トリガーがトリガーされるのを妨げる可能性があるのは何ですか?

私の最小 API は 11 です。また、listView を に設定していlongClickable="true"ます。

活動コード (選択された機能):

public class EventListActivity extends ListActivity {

    private ArrayList<Event> arrEvents = null;
    private ArrayAdapter<Event> adpEvents = null;

    private ActionMode mActionMode = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // only create list adapter and set it
        arrEvents = new ArrayList<Event>();
        adpEvents = new ArrayAdapter<Event>(this, android.R.layout.simple_list_item_activated_2, android.R.id.text1, arrEvents) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView text1 = (TextView) view.findViewById(android.R.id.text1);
                TextView text2 = (TextView) view.findViewById(android.R.id.text2);
                text1.setText(arrEvents.get(position).getTitle());
                text2.setText(arrEvents.get(position).getDateTimeFormatted());
                return view;
            }
        };

        setListAdapter(adpEvents);

        // add CAB to ListView
        setupCAB();
    }


    @Override
    protected void onResume() {
        super.onResume();

        // populate list and refresh adapter
        createEventList();
        adpEvents.notifyDataSetChanged();

        // if list empty show emtpy msg, otherwise hide it
        setContentView(R.layout.activity_event_list);
        TextView empty = (TextView) findViewById(R.id.text_empty);
        if(arrEvents.isEmpty()) {
            empty.setVisibility(View.VISIBLE);
        } else {
            empty.setVisibility(View.GONE);
        }
    }


    private void setupCAB() {
        // Important: to select single mode
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            // Called when the user long-clicks an item on the list
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View row, int position, long rowid) {
                Log.w("EventListActivity", "Long click detected!");
                Toast.makeText(EventListActivity.this, "Long click detected!", Toast.LENGTH_SHORT).show();
                if (mActionMode != null) {
                    return false;
                }

                // Important: to mark the editing row as activated
                getListView().setItemChecked(position, true);

                // Start the CAB using the ActionMode.Callback defined above
                mActionMode = EventListActivity.this.startActionMode(mActionModeCallback);
                return true;
            }
        });
    }

    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.event_context, menu);
            return true;
        }

        // Called when the user enters the action mode
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // Disable the list to avoid selecting other elements while editing one
            EventListActivity.this.getListView().setEnabled(false);
            return true; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.mnu_share_event:
                //TODO share event
                mode.finish();
                return true;
            default:
                return false;
            }
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // Re-enable the list after edition
            EventListActivity.this.getListView().setEnabled(true);
            mActionMode = null;
        }
    };

}

activity_event_list.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EventListActivity" >

    <TextView
        android:id="@+id/text_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="@string/empty"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:visibility="gone" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:longClickable="true" >
    </ListView>

</RelativeLayout>
4

3 に答える 3

5

リストビュー内のイベントに応答するボタンがある場合はonClick()、それらのボタンを保持するコンテナーで次を設定する必要があります。

android:descendantFocusability="blocksDescendants"

あなたが持っているのがテキストビューの場合、問題は少しトリッキーです。これを参照してください: ListView 内のフォーカス可能な EditText

于 2013-11-29T20:05:47.393 に答える
2

この回答はuser1の質問を解決しませんが、症状は私の問題と似ていました(つまりOnItemClickListener、呼び出されましたが、呼び出さOnItemLongClickListenerれませんでした)。私の問題を解決しようとしたときのように、他の誰かがこの質問に出くわした場合に備えて、ここに回答を投稿しています。

Fragment 内で ListView を使用し、リスナー メソッドを実装しました。

public class MyFragment extends Fragment implements OnClickListener,
        OnLongClickListener, OnItemClickListener, OnItemLongClickListener {

onItemClickうまく機能していた方法は次のとおりです。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long rowId) {

    Log.i("Chimee", "short click working");

}

そして、ここにonItemLongClick起動していないメソッドがあります:

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
        long rowId) {

    Log.i("Chimee", "Long click working");
    return false;
}

もちろん、簡単な答えは、忘れていたということsetOnItemLongClickListenerです。私はずっと持っていたの後にそれを追加しましたがsetOnItemClickListener、それはうまくいきました。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.my_fragment, container, false);

    lvSuggestions = (ListView) v.findViewById(R.id.lvSuggestions);
    lvSuggestions.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lvSuggestions.setOnItemClickListener(this);
    lvSuggestions.setOnItemLongClickListener(this); // Forgot this

    ...
}
于 2014-08-20T02:48:42.550 に答える