2

我正在扩展 aCursorAdapter以将数据绑定到 a 中的视图ListView。当项目内的子视图具有onClick()侦听器时,onItemClick()不会为该行调用。我希望调用子视图的onClick()方法,并调用该onItemClick()方法,并在触摸时突出显示该行(默认情况下,当子视图没有onClickListener()附加时)。其他触摸方法(如onLongClick())返回 a 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:focusable以及android:clickable我能想到的视图层次结构的每个级别的每种组合。

请将任何答案与其他关于我可能会采取不同做法的评论区分开来(比如不为每个视图创建新的侦听器?)


编辑:

似乎两者都可以setOnClickListener()防止setOnLongClickListener()onItemClick()短按时触发,无论setOnLongClickListener()返回真还是假。此外,对于要触发的任一子视图onItemClick(),应将RelativeLayout's设置为,并且将子视图'应设置为。似乎没有其他任何东西起作用或有任何效果。android:descendantFocusability"blocksDescendants"android:clickable"false"

4

0 回答 0