0

我已经花了一天多的时间试图弄清楚这一点。我相当肯定我的注意力被布局中的某些东西偷走了。

我正在使用的适配器:

public class AudioAdapter extends ArrayAdapter<MusicContent>
{
private Context context;

private ImageView albumArt;
private TextView songName;
private TextView artistName;
private TextView albumName;
private TextView genre;
private TextView duration;

private CheckBox checkbox;

private List<MusicContent> content = new ArrayList<MusicContent>();

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;

    if (row == null)
    {
        // ROW INFLATION
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.testing_cell, parent, false);
    }

    // Get item
    MusicContent item = getItem(position);
    RelativeLayout root = (RelativeLayout) row.findViewById(R.id.list_cell_layout);

    // perform a series of checks to maintain customizability
    albumArt = (ImageView) row.findViewById(R.id.list_cell_image);
    if (albumArt != null)
    {
        if (item.hasAlbumArt())
            albumArt.setImageBitmap(item.getAlbumArt());
        else
            albumArt.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.ic_music_album));
        albumArt.setClickable(false);
        albumArt.setFocusable(false);
    }

    LinearLayout checkLL = (LinearLayout) row.findViewById(R.id.list_cell_music_info);
    if (checkLL != null)
    {
        // display some song info
        songName = (TextView) checkLL.findViewById(R.id.list_cell_title);
        if (songName != null)
        {
            songName.setText(item.getDisplayName());
            songName.setVisibility(View.VISIBLE);
        }

        checkLL = (LinearLayout) row.findViewById(R.id.list_cell_artist_info);
        if (checkLL != null)
        {
            // display artist info too
            artistName = (TextView) checkLL.findViewById(R.id.list_cell_artist_name);
            if (artistName != null)
                artistName.setText(item.getArtist());

            albumName = (TextView) checkLL.findViewById(R.id.list_cell_album);
            if (albumName != null)
                albumName.setText(item.getAlbum());

            duration = (TextView) checkLL.findViewById(R.id.list_cell_duration);
            if (duration != null)
                duration.setText(item.getDurationString());

            genre = (TextView) checkLL.findViewById(R.id.list_cell_genre);
            if (genre != null)
                genre.setText(item.getGenre());
        }

        FrameLayout checkFL = (FrameLayout) row.findViewById(R.id.endoflineinfo);
//            checkLL = (LinearLayout) row.findViewById(R.id.endoflineinfo);
        if (checkFL != null)
        {
            checkbox = (CheckBox) checkFL.findViewById(R.id.in_playlist);
            if (checkbox != null)
                checkbox.setChecked(!item.getPlaylist().isEmpty());

            checkbox.setFocusable(false);
            checkbox.setClickable(false);
        }
    }

    root.setDescendantFocusability(RelativeLayout.FOCUS_BLOCK_DESCENDANTS);
    return row;
}

我的 XML 布局:

<RelativeLayout
    android:id="@+id/list_cell_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/list_cell_menu_button"
    android:clickable="true"
    android:descendantFocusability="blocksDescendants"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/playlistItemPadding"
    android:paddingTop="@dimen/playlistItemPadding" >
    >

    <ImageView
        android:id="@+id/list_cell_image"
        android:layout_width="@dimen/playlistItemSize"
        android:layout_height="@dimen/playlistItemSize"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/playlistMargin"
        android:layout_marginRight="@dimen/playlistMargin"
        android:clickable="false"
        android:focusable="false"
        android:scaleType="centerCrop"
        android:tag="@string/image_type_browsing_list_item" />

    <ImageView
        android:id="@+id/list_cell_overlay"
        android:layout_width="@dimen/playlistItemSize"
        android:layout_height="@dimen/playlistItemSize"
        android:layout_marginLeft="@dimen/playlistMargin"
        android:layout_marginRight="@dimen/playlistMargin"
        android:clickable="false"
        android:focusable="false"
        android:padding="8.0dip"
        android:src="@drawable/ic_mc_play_overlay"
        android:visibility="gone" />

    <FrameLayout
        android:id="@+id/endoflineinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/playlistMargin"
        android:layout_marginRight="@dimen/playlistMargin"
        android:clickable="false"
        android:focusable="false" >

        <ImageView
            android:id="@+id/list_cell_item_locked"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:focusable="false"
            android:src="@drawable/ic_lock"
            android:visibility="invisible" />

        <CheckBox
            android:id="@+id/in_playlist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/btn_check"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:visibility="invisible" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/list_cell_music_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/endoflineinfo"
        android:layout_toRightOf="@id/list_cell_image"
        android:clickable="false"
        android:focusable="false"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/list_cell_title"
            style="@style/Text.Listview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:clickable="false"
            android:ellipsize="marquee"
            android:focusable="false"
            android:marqueeRepeatLimit="marquee_forever" />

        <LinearLayout
            android:id="@+id/list_cell_artist_info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/playlistMargin"
            android:clickable="false"
            android:focusable="false"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/list_cell_artist_name"
                style="@style/Text.Listview.Small"
                android:layout_width="0.0sp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="0.5"
                android:clickable="false"
                android:ellipsize="marquee"
                android:focusable="false"
                android:marqueeRepeatLimit="marquee_forever" />

            <TextView
                android:id="@+id/list_cell_album"
                style="@style/Text.Listview.Small"
                android:layout_width="0.0sp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="0.5"
                android:clickable="false"
                android:ellipsize="marquee"
                android:focusable="false"
                android:marqueeRepeatLimit="marquee_forever" />

            <TextView
                android:id="@+id/list_cell_genre"
                style="@style/Text.Listview.Small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="0.0"
                android:clickable="false"
                android:focusable="false"
                android:gravity="right"
                android:visibility="gone" />

            <TextView
                android:id="@+id/list_cell_duration"
                style="@style/Text.Listview.Small"
                android:layout_width="60.0sp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="0.0"
                android:clickable="false"
                android:focusable="false"
                android:gravity="right" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

<ImageView
    android:id="@+id/list_cell_menu_button"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:background="@drawable/button_background_square"
    android:clickable="true"
    android:descendantFocusability="blocksDescendants"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:padding="19.0dip"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_menu_source_config"
    android:visibility="gone" />

设置 onItemClick

public void onCreate(Bundle blahblah)
{
    setContentView(R.layout.fragment_list);
    contentListView = (ListView) findViewById(R.id.frag_item_browser_list);
    contentListView.setItemsCanFocus(true);
    contentListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    contentListView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);

    contentListView.setOnItemClickListener(new ListView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Log.d("SHARK", "click on " + contentAdapter.getItem(arg2));
        }
    });
    contentListView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Log.d("SHARK", "Long click on " + contentAdapter.getItem(arg2));
            return true;
        }
    });

    contentListView.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Log.d("SHARK", "something = " + arg1 + "\t" + arg0.getAdapter().getItem(arg2));
            // Log.d("SHARK", "onClick happened ? "+arg1.callOnClick());
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            Log.d("SHARK", "nothing");
        }
    });

    contentListView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("SHARK", "touching = " + v);
            return false;
        }
    });

    contentListView.setAdapter(contentAdapter);
    contentListView.postInvalidate();
    contentAdapter.notifyDataSetChanged();
}

onTouch 总是返回 ListView。OnSelectChange 正确返回 RelativeLayouts。OnItemClick 和 OnLongItemClick 根本不会触发。

我尝试过使用一个非常基本的 ArrayAdapter,当数据以简单的 toString() 形式出现在列表中时,单击就可以了。所以我相当确定要么是自定义适配器代码占用了 onClick -这有点怀疑* - 要么是 XML 可能在后台做一些时髦的事情..

请帮忙。

4

1 回答 1

0

最后,我采用了交叉点击方法,在适配器分配给每个视图onItemClickListener的内部onClick()侦听器中调用我想要的回调......

有点像这样

public SomeAdapter(Context context, int textViewResourceId, List<Content> objects,
        OnItemClickListener clickListener)
{
    super(context, textViewResourceId, objects);
    this.context = context;
    this.content = objects;
    this.clickListener = clickListener;
}


@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;

    if (row == null)
    {
        // ROW INFLATION
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.list_cell, parent, false);
    }
    // initiate helpers for onClick hack
    final AdapterView fParent = (AdapterView) parent;
    final View fView = row;
    final int fInt = position;
    final long fLong = row.getId();

    // Get item
    Content item = getItem(position);
    if (item == null)
        return row;

    RelativeLayout root = (RelativeLayout) row.findViewById(R.id.list_cell_layout);

    /** .... find views, do stuff .... */

    // magic happens where we bind an OnItemClick call to OnClick
    root.setDescendantFocusability(RelativeLayout.FOCUS_BLOCK_DESCENDANTS);

    if (clickListener != null)
    {
        OnClickListener cross = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("SHARK", "internal onClick from adapter!!");
                clickListener.onItemClick(fParent, fView, fInt, fLong);
            }
        };
        // assign this listener
        root.setOnClickListener(cross);
    }
    return row;
}
于 2013-06-20T12:29:03.337 回答