android在ListView项目中实现多次单击事件

时间:2014-09-13 13:30:59

标签: android android-listview click adapter baseadapter

我想在ListView项目中实现多个按钮。但问题是,当点击按钮时我无法处理点击事件,我的意思是它们是Play按钮和图像按钮这样

enter image description here

这是我的适配器:

public class ArtistsAdapter extends BaseAdapter implements SectionIndexer {

private HashMap<String, Integer> alphaIndexer;
private String[] sections;
private LayoutInflater inflater;
private int resId;
private Context context;
private ArrayList<Artist> artistList;

public ArtistsAdapter(Context context, int resId, ArrayList<Artist> artistList) {
    this.resId = resId;
    this.context = context;
    this.artistList = artistList;
    this.inflater = LayoutInflater.from(context);
}

@Override
public Object[] getSections() {
    return sections;
}

@Override
public int getPositionForSection(int section) {
    return alphaIndexer.get(sections[section]);
}

@Override
public int getSectionForPosition(int position) {
    return 1;
}

private static class ViewHolder {
    ImageView artistImgItem;
    ImageButton artistPlayItem;
    TextView artistNameItem;
    TextView artistAgeItem;
    LinearLayout artistItem;
}

@Override
public int getCount() {
    return artistList.size();
}

@Override
public Object getItem(int position) {
    return artistList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Artist artist = (Artist) getItem(position);
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = this.inflater.inflate(resId, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.artistImgItem = (ImageView) convertView.findViewById(R.id.artistImg);
        viewHolder.artistPlayItem = (ImageButton) convertView.findViewById(R.id.artistPlay);
        viewHolder.artistNameItem = (TextView) convertView.findViewById(R.id.artistName);
        viewHolder.artistAgeItem = (TextView) convertView.findViewById(R.id.artistAge);
        viewHolder.artistItem = (LinearLayout) convertView.findViewById(R.id.artistItem);
        convertView.setTag(viewHolder);
    } else viewHolder = (ViewHolder) convertView.getTag();
    viewHolder.artistNameItem.setText(artist.getName());

    viewHolder.artistPlayItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("aaaaa", position+"ssss");
            Toast.makeText(context, "Music playin " + position+1 +"", Toast.LENGTH_SHORT).show();
        }
    });
    viewHolder.artistItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((ArtistsActivity) context).showArtistView();
        }
    });
    viewHolder.artistImgItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((ArtistsActivity) context).showArtistView();
        }
    });
    return convertView;
}

}

我的活动看起来像那样

public class ArtistsActivity extends Activity {

private IndexableListView artistListUI;
private ImageView maleArtist, femaleArtist, groupArtist, artistImg;
private ArtistsAdapter artistsAdapter;
private ArrayList<Artist> maleArtistList, femaleArtistList, groupArtistList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.artist_list);

    artistListUI = (IndexableListView) findViewById(R.id.artistListView);
    maleArtist = (ImageView) findViewById(R.id.maleArtist);
    femaleArtist = (ImageView) findViewById(R.id.femaleArtist);
    groupArtist = (ImageView) findViewById(R.id.groupArtist);
    artistImg = (ImageView) findViewById(R.id.artistImg);

    artistListUI.setFastScrollEnabled(true);     

    artistsAdapter = new ArtistsAdapter(ArtistsActivity.this, R.layout.artist_item, artistList);
    artistListUI.setAdapter(artistsAdapter);



public void showArtistView() {
    startActivity(new Intent(ArtistsActivity.this, AlbumsActivity.class));
}

}

2 个答案:

答案 0 :(得分:1)

 you have to set clickable property of listview to false
 because of entire listview click is fire

答案 1 :(得分:0)

这在适配器中得到解决。看看这个例子:

https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/tracks/TrackAdapter.java

在bindView或getView方法中,您必须定义链接到视图的Clickhandler:

holder.hLayout.setOnClickListener(new MentionClickhandler());

在这个功能中做些坏事:

public class MentionClickhandler implements View.OnClickListener 
{
public void onClick( View view ){

    Track trackTag = new Track();
    trackTag = (Track) view.getTag();
    Log.i(TAG, "item: "+trackTag.getSearchTrack());

    Bundle bundle = new Bundle();
    bundle.putInt("idTrack", trackTag.getIdTrack());
    bundle.putString("searchTrack", trackTag.getSearchTrack());
    bundle.putInt("topTrack", trackTag.getTopTrack());
    bundle.putLong("lastId", trackTag.getLastId());
    bundle.putInt("count", trackTag.getCount());
    bundle.putString("type", "LastMentions");

    Intent i = new Intent(context, GDMentionsList.class);
    i.putExtras(bundle);
    context.startActivity(i);

}
}

希望它有所帮助...