在ListView上捕获触摸事件

时间:2014-06-16 11:56:32

标签: android listview touch-event

我正在开发和Android应用程序,我遇到了listview的严重问题。

起初我有一个onItemClickListener,它运行良好,但是现在,我已经为布局添加了一些新功能,但它不起作用。

我的代码是:

lv = (ListView) findViewById(R.id.listChap);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(CapituloList.this,"Pulsado",Toast.LENGTH_SHORT).show();
        Capitulo chap = (Capitulo) mAdapter.getItem(i);
        Intent mIntent = new Intent(CapituloList.this, MangaView.class);
        mIntent.putExtra("capitulo", chap);
        startActivity(mIntent);
    }
});

在Capitulo Adapter我有:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view  = inflater.inflate(R.layout.list_manga_item_view,null);
    TextView tv = (TextView) view.findViewById(R.id.nombre_manga);
    ImageButton ib = (ImageButton) view.findViewById(R.id.checked_icon);
    ImageButton ib2 = (ImageButton) view.findViewById(R.id.unchecked_icon);
    tv.setText(mItems.get(i).getCapitulo());
    return view;
}

显然我想在ib,ib2和布局的其余部分之间进行差异化处理 可见的结果是:点击URL,我没有信誉发布图片 http://i.imgur.com/kNXU8lE.png

提前致谢

编辑:已解决

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view  = inflater.inflate(R.layout.list_manga_item_view,null);
    TextView tv = (TextView) view.findViewById(R.id.nombre_manga);
    final Capitulo chap = (Capitulo) this.getItem(i);
    ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton);
    ib.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(mContext,"Button1 pressed",Toast.LENGTH_LONG).show();
        }
    });
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent mIntent = new Intent(mContext, MangaView.class);
            mIntent.putExtra("capitulo", chap);
            mContext.startActivity(mIntent);
        }
    });
    ImageButton ib2 = (ImageButton) view.findViewById(R.id.imageButton2);
    ib2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(mContext,"Button2 pressed",Toast.LENGTH_LONG).show();
        }
    });
    tv.setText(mItems.get(i).getCapitulo());
    return view;
}

1 个答案:

答案 0 :(得分:0)

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) 
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view  = inflater.inflate(R.layout.list_manga_item_view,null);
TextView tv = (TextView) view.findViewById(R.id.nombre_manga);
final Capitulo chap = (Capitulo) this.getItem(i);
ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton);
ib.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(mContext,"Button1 pressed",Toast.LENGTH_LONG).show();
    }
});
view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent mIntent = new Intent(mContext, MangaView.class);
        mIntent.putExtra("capitulo", chap);
        mContext.startActivity(mIntent);
    }
});
ImageButton ib2 = (ImageButton) view.findViewById(R.id.imageButton2);
ib2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(mContext,"Button2 pressed",Toast.LENGTH_LONG).show();
    }
});
tv.setText(mItems.get(i).getCapitulo());
return view;
}