Android 我的孩子被舔得不舒服';不要被叫

Android 我的孩子被舔得不舒服';不要被叫,android,android-layout,android-emulator,android-listview,onitemclicklistener,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 alb

我花了一天多的时间试图弄明白这一点。我相当确定我的焦点被布局中的某些东西偷走了

我正在使用的适配器:

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;
}
公共类音频适配器扩展了ArrayAdapter
{
私人语境;
私人影像艺术;
私有文本视图songName;
私有文本视图艺术家名称;
私有文本视图名称;
私有文本视图体裁;
私有文本视图持续时间;
私有复选框;
私有列表内容=新的ArrayList();
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
if(行==null)
{
//行通货膨胀
LayoutFlater充气器=(LayoutFlater)this.getContext().getSystemService(
上下文。布局(充气机和服务);
row=充气机。充气(R.layout.testing\u单元,父单元,错误);
}
//获取项目
音乐内容项=获取项(位置);
RelativeLayout root=(RelativeLayout)row.findViewById(R.id.list\u cell\u布局);
//执行一系列检查以保持可定制性
albumArt=(ImageView)row.findViewById(R.id.list\u cell\u image);
如果(albumArt!=null)
{
if(item.hasAlbumArt())
setImageBitmap(item.getAlbumArt());
其他的
albumArt.setImageDrawable(context.getResources().getDrawable(
R.drawable.ic_音乐专辑);
albumArt.setClickable(假);
albumArt.setFocusable(假);
}
LinearLayout checkLL=(LinearLayout)row.findViewById(R.id.list\u cell\u music\u info);
if(checkLL!=null)
{
//显示一些歌曲信息
songName=(TextView)checkLL.findViewById(R.id.list\u cell\u title);
if(songName!=null)
{
songName.setText(item.getDisplayName());
songName.setVisibility(View.VISIBLE);
}
checkLL=(LinearLayout)row.findViewById(R.id.list\u cell\u artist\u info);
if(checkLL!=null)
{
//也显示艺术家信息
artistName=(TextView)checkLL.findViewById(R.id.list\u cell\u artist\u name);
if(artistName!=null)
artistName.setText(item.getArtist());
albumName=(TextView)checkLL.findViewById(R.id.list\u cell\u相册);
if(albumName!=null)
albumName.setText(item.getAlbum());
duration=(TextView)checkLL.findViewById(R.id.list\u cell\u duration);
if(持续时间!=null)
duration.setText(item.getDurationString());
类型=(文本视图)检查ll.findViewById(R.id.list\u cell\u类型);
如果(类型!=null)
genre.setText(item.getGenre());
}
FrameLayout checkFL=(FrameLayout)row.findviewbyd(R.id.endoflineinfo);
//checkLL=(LinearLayout)row.findViewById(R.id.endoflineinfo);
if(checkFL!=null)
{
复选框=(复选框)选中fl.findViewById(R.id.in_播放列表);
如果(复选框!=null)
checkbox.setChecked(!item.getPlaylist().isEmpty());
复选框.setFocusable(false);
复选框。可点击设置(错误);
}
}
root.setDegenantFocusability(RelativeLayout.FOCUS\u BLOCK\u后代);
返回行;
}
我的XML布局:


>

设置mclick

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();
}
public void onCreate(Bundle blahblah)
{
setContentView(R.layout.fragment_列表);
contentListView=(ListView)findViewById(R.id.frag\u项目\u浏览器\u列表);
contentListView.setItemsCanFocus(true);
contentListView.setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
contentListView.SetDegenantFocusability(ListView.FOCUS\u块\u子体);
contentListView.setOnItemClickListener(新的ListView.OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
Log.d(“SHARK”,“单击”+contentAdapter.getItem(arg2));
}
});
setOnItemLongClickListener(新的OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
Log.d(“SHARK”,“长时间单击”+contentAdapter.getItem(arg2));
返回true;
}
});
contentListView.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3){
Log.d(“SHARK”、“something=“+arg1+”\t”+arg0.getAdapter().getItem(arg2));
//Log.d(“SHARK”,“onClick发生了?”+arg1.callOnClick());
}
@凌驾
未选择公共无效(AdapterView arg0){
Log.d(“SHARK”,“nothing”);
}
});
setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
日志d(“鲨鱼”,“触摸=”+v);
返回false;
}
});
setAdapter(contentAdapter);
contentListView.postInvalidate();
contentAdapter.notifyDataSetChanged();
}
onTouch始终返回ListView。OnSelectChange正确地返回RelativeLayouts.OnItemClick和OnLongItemClick。只需不触发即可

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


请提供帮助。

最后,我采用了点击交叉的方法,在分配给每个视图b的内部
onClick()
侦听器中调用我预期的
onItemClickListener
回调
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();
}
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;
}