如何在Adapter中设置setOnClickListener并从Fragment访问?

时间:2014-12-06 09:54:52

标签: android gridview fragment onclicklistener

我正在开发像Google Play音乐这样的应用! 事情就像那个app我在gridview上的每一行都有一个popupmenu。 我从实现onClickListener的fragment设置网格视图适配器。 问题是: 如何为每个弹出菜单设置onclicklistener并处理片段中的onclick事件 我的popupmenu包含两个项目: 删除 添加到播放列表

我想从片段访问onClick的原因是我要删除特定文件并使用notifydatasetchanged()更新网格视图!

如何执行此操作或如何从适配器n更新网格视图中删除特定文件的任何建议!

谢谢!

这是我的GridView适配器:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;
    item = songs.get(position);

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(com.irangrammy.irangrammy.R.layout.mchannel_row, parent, false);
        holder = new ViewHolder();
        holder.thumbnail = (ImageView) row.findViewById(com.irangrammy.irangrammy.R.id.image);
        holder.title = (TextView) row.findViewById(com.irangrammy.irangrammy.R.id.singer);
        holder.mMenu = (ImageView) row.findViewById(R.id.itemOption);

        holder.mMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                PopupMenu popUp = new PopupMenu(context, v);
                MenuInflater inflater = popUp.getMenuInflater();
                inflater.inflate(R.menu.mchannel_popup, popUp.getMenu());
                popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem arg0) {
                        // TODO Auto-generated method stub
                        if(arg0.getItemId()==R.id.delete)
                        {

                        }
                    return true;
                    }
                });
                popUp.show();       
            }
        });
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

3 个答案:

答案 0 :(得分:2)

您可以将片段传递给Adapter类并将片段转换为onClickListener,但请确保在Fragment类上实现了onClickListener。

答案 1 :(得分:1)

我还没有找到你!但如果我理解你的解决方案: 尝试将广播发送到您的片段,当片段获得广播时,请根据需要进行处理。 更新:如果要将侦听器设置为视图,则可以执行此操作:

convertView.setOnClickListener(new View.ClickListener() {
                    @Override
                    public boolean onLongClick(View view) {

                     // Do what you want when the view clicked!
                    }
                });

如果这不是答案 - 请用您的母语发送信息

答案 2 :(得分:1)

首先你的View holder设计模式是错误的,它必须如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;
    item = songs.get(position);

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(com.irangrammy.irangrammy.R.layout.mchannel_row, parent, false);
        holder = new ViewHolder();
        holder.thumbnail = (ImageView) row.findViewById(com.irangrammy.irangrammy.R.id.image);
        holder.title = (TextView) row.findViewById(com.irangrammy.irangrammy.R.id.singer);
        holder.mMenu = (ImageView) row.findViewById(R.id.itemOption);


        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

        holder.mMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                PopupMenu popUp = new PopupMenu(context, v);
                MenuInflater inflater = popUp.getMenuInflater();
                inflater.inflate(R.menu.mchannel_popup, popUp.getMenu());
                popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem arg0) {
                        // TODO Auto-generated method stub
                        if(arg0.getItemId()==R.id.delete)
                        {
                           songs.get(position).remove();
                           notifyDataSetChanged();

                        }
                    return true;
                    }
                });
                popUp.show();       
            }
        });
}