如何在ListView中为ImageView设置onClick事件

时间:2018-04-01 18:01:40

标签: android listview android-arrayadapter

我有一个自定义ListView显示歌曲标题,每个包含两个ImageView,用于添加到收藏夹和分享歌曲。

custom_listview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

     <TextView android:id="@+id/song_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

     <ImageView
         android:id="@+id/add_to_favorites"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

     <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

要填写listview,我需要使用ArrayAdapter:

public class Media_adapter extends ArrayAdapter<Media> {

                Context context;
                int layoutResourceId;
                Media data[] = null;


                public Media_adapter(Context context, int layoutResourceId, Media[] data) {
                    super(context, layoutResourceId, data);
                    this.layoutResourceId = layoutResourceId;
                    this.context = context;
                    this.data = data;
                }

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

                    if(row == null)
                    {
                        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                        row = inflater.inflate(layoutResourceId, parent, false);

                        holder = new MediaHolder();
                        holder.song_title = (TextView)row.findViewById(R.id.song_title);
                        holder.add_to_favorites = (ImageView)row.findViewById(R.id.add_to_favorites);
                        holder.share = (ImageView)row.findViewById(R.id.share);

                        row.setTag(holder);

                    }
                    else
                    {
                        holder = (MediaHolder)row.getTag();
                    }

                    Media media = data[position];
                    holder.song_title.setText(media.title);
                    holder.add_to_favorites.setImageResource(media.image_favorite);
                    holder.share.setImageResource(media.image_share);

                    return row;
                }

                class MediaHolder
                {
                    TextView song_title;
                    ImageView add_to_favorites, share;
                }

        }

班级媒体:

package com...;

public class Media {

   public String title;
   public int image_favorite, image_share;

   public Media() {
       super();
   }

}

要播放一首歌,我已拨打setOnItemClickListener(...)

mylistview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> av,
                                View view, int position, long l) {
            //code for playing song
        }
    });

最后,我想为Listview项目中使用的两个ImageView add_to_favoritesshare添加onClick事件。

1 个答案:

答案 0 :(得分:1)

您应该在适配器的构造函数中发送View.onClickListener。 然后将其分配给您有兴趣单击它们的视图 从那里只需在您的activity / fragment中实现onClickListener。

公共类Media_adapter扩展了ArrayAdapter {

            Context context;
            int layoutResourceId;
            Media data[] = null;
            View.onClickListener listener; 

            public Media_adapter(Context context, int layoutResourceId, Media[] data,View.onClickListener) {
                super(context, layoutResourceId, data);
                this.layoutResourceId = layoutResourceId;
                this.context = context;
                this.data = data;
                this.listener = listener;
            }

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

                if(row == null)
                {
                    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                    row = inflater.inflate(layoutResourceId, parent, false);

                    holder = new MediaHolder();
                    holder.song_title = (TextView)row.findViewById(R.id.song_title);
                    holder.add_to_favorites = (ImageView)row.findViewById(R.id.add_to_favorites);
                    holder.share = (ImageView)row.findViewById(R.id.share);
                    **holder.share.setTag(position);
                    holder.share.setOnClickListener(listener);**
                    row.setTag(holder);

                }
                else
                {
                    holder = (MediaHolder)row.getTag();
                }

                Media media = data[position];
                holder.song_title.setText(media.title);
                holder.add_to_favorites.setImageResource(media.image_favorite);
                holder.share.setImageResource(media.image_share);

                return row;
            }

            class MediaHolder
            {
                TextView song_title;
                ImageView add_to_favorites, share;
            }

    }

在您的活动中,您将实现View.onClicklistener

public class MyActivity extends Activity implements View.onClickListener
{

   when you create your adapter send **this** as the last parameter of the adapter constructor.


    public void onClick(View view) {
        switch (view.getId())
        { 
            case R.id.share:

            //here you need to retrieve the position of the item
            int position = (int)view.getTag();
            Media med = media[position];
            //here just use med where you want to use it.

            do the same thing for other componenets in your adapter layout. 

            break;
        }

}

需要注意的一些事项:

1)我建议你使用recycleview(它有点复杂,但知道非常有用)而不是使用listview

2)我没有测试我的计算机上的代码我只是在记事本中写的,也许你会得到一些语法错误

<强>更新

我创建了一个简单的项目,它具有您正在寻找的相同功能,并且一切正常

<强> MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ListView mList;
    private Media_adapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mList = (ListView)findViewById(R.id.listview);

        Media[] data = new Media[5];
        for(int i=0;i<data.length;i++)
        {
            data[i] = new Media();
            data[i].title = "pos"+i;
            data[i].image_favorite = R.mipmap.ic_launcher;
            data[i].image_share = R.mipmap.ic_launcher;
        }

        mAdapter = new Media_adapter(this,R.layout.custom_listview,data,this);
        mList.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View view) {
        int position;
        switch (view.getId())
        {
            case R.id.add_to_favorites:
                position = (int) view.getTag(); Log.d("test", position + " add_to_favorites");
                break;
            case R.id.share:
                position = (int) view.getTag(); Log.d("test", position + " share");
                break;
        }

    }
}

<强> Media_adapter

public class Media_adapter extends ArrayAdapter<Media>
{

    Context                 context;
    int                     layoutResourceId;
    Media                   data[];
    View.OnClickListener    listener;

    public Media_adapter(Context context, int layoutResourceId, Media[] data, View.OnClickListener listener)
    {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        this.listener = listener;
    }

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

        if (row == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MediaHolder();
            holder.song_title = (TextView) row.findViewById(R.id.song_title);
            holder.add_to_favorites = (ImageView) row.findViewById(R.id.add_to_favorites);
            holder.share = (ImageView) row.findViewById(R.id.share);

            holder.add_to_favorites.setTag(position);
            holder.add_to_favorites.setOnClickListener(listener);
            holder.share.setTag(position);
            holder.share.setOnClickListener(listener);

            row.setTag(holder);

        }
        else
        {
            holder = (MediaHolder) row.getTag();
        }

        Media media = data[position];
        holder.song_title.setText(media.title);
        holder.add_to_favorites.setImageResource(media.image_favorite);
        holder.share.setImageResource(media.image_share);

        return row;
    }

    class MediaHolder
    {
        TextView    song_title;
        ImageView   add_to_favorites, share;
    }
}