2

CursorAdapterのビューにデータをバインドするために を拡張していListViewます。アイテム内のサブビューにonClick()リスナーがあるonItemClick()場合、行に対して呼び出されません。サブビューonClick()のメソッドが呼び出され、メソッドが呼び出され、タッチされたときに行が強調表示されるようにします(サブビューにアタッチonItemClick()されていない場合のデフォルトのように)。onClickListener()他のタッチ メソッド ( などonLongClick())booleanは、タッチ イベントが消費されたかどうかを示す を返しますが、このonClick()メソッドは void を返し、 が返されたかのように動作しbooleanますtrue

私の activity_main.xml には次のものがあります。

<ListView ...[attributes] ...
    android:id="@+id/stations" >    
</ListView>

そして、データを取り込むビューの場合ListView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ...[attributes] ... >   
    <ImageView
        ...[attributes] ...
        android:id="@+id/station_drag" />
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        ...[attributes] ...
        android:id="@+id/station_title" >
    </TextView>
</RelativeLayout>

そして私のCursorAdapterクラス:

public class RadioCursorAdapter extends CursorAdapter {

    protected Activity mContext;

    public RadioCursorAdapter(Activity context, Cursor c, int flags) {
        super(context, c, flags);
        this.mContext = context;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView titleView = (TextView) view.findViewById(R.id.station_title);

        final String url = cursor.getString(cursor.getColumnIndexOrThrow(RadioDbContract.StationEntry.COLUMN_NAME_URL));
        titleView
                .setText(cursor.getString(cursor.getColumnIndexOrThrow(RadioDbContract.StationEntry.COLUMN_NAME_TITLE)));
        // commenting out the following makes the listView's onItemClickListener
        // work, and row highlights while pressed
        titleView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // this part works when the text is clicked, but prevents
                // onItemClick in listview
            }
        });
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.station_entry, null);
        return v;
    }
}

の親とその親を取得し、titleView手動で を呼び出してみましonClick()たが、うまくいかないようonItemClick()で、別のハンドラーです。のすべての組み合わせをandroid:descendantFocusability、考えられるビュー階層の各レベルで試しました。android:focusableandroid:clickable

私が別の方法で行う可能性があることについて、他のコメントと回答を分けてください (ビューごとに新しいリスナーを作成しないなど)。


編集:

true を返すか falseを返すかに関係なく、 との両方が短いクリックで起動しsetOnClickListener()ないsetOnLongClickListener()ように見えます。また、いずれかのサブ ビューをトリガーするには、を に設定し、サブ ビューを に設定する必要があります。他に何も機能しないか、効果があるようには見えません。onItemClick()setOnLongClickListener()onItemClick()RelativeLayoutandroid:descendantFocusability"blocksDescendants"android:clickable"false"

4

0 に答える 0