我的OnItemClicked没有被调用

时间:2013-06-11 12:38:19

标签: android android-layout android-emulator android-listview onitemclicklistener

我花了一天多的时间试图解决这个问题。我很确定我的重点是被布局中的某些东西偷走了。

适配器我正在使用:

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可能会在背景..

请帮忙。

1 个答案:

答案 0 :(得分: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;
}