从适配器调用片段方法 [英] Calling a fragment method from an Adapter

查看:57
本文介绍了从适配器调用片段方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的适配器中调用一个Fragment方法,但出现错误:

I need to call a Fragment method inside my Adapter, but I get an error:

ClassCastException:Main(MainActivity)无法强制转换为PlayPauseClick(Interface)

ClassCastException: Main(MainActivity) cannot be cast to PlayPauseClick(Interface)

我在我的Fragment中实现了我的接口,但是仍然出现此错误,因为一个朋友已经告诉我 mContext (我的适配器上下文)不能是 Fragment .

I implemented my Interface in my Fragment but I still get this error cause a friend already told me that mContext (my Adapter context) cannot be a Fragment.

那我该如何解决这个问题?

So how can I solve this problem?

我的片段方法:

private FunDapter<Product> adapter;
private ArrayList<Product> productList;
private ListView lvProduct;

adapter = new FunDapter<>(getActivity(), productList, R.layout.d_layout_list_d, dict);
lvProduct.setAdapter(adapter);

// My method
@Override
public void imageButtonOnClick(View v, int position){
  playPause=(ImageView)v.findViewById(R.id.playPause);
  Product m = productList.get(position);
  playPause.setImageResource(m.getPlayPauseId());
  playPause.setTag(position);
  playPause.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
            int pos = (Integer) v.getTag();
            Product m =(Product) productList.get(pos);
            if (paused) {
            m.setPlayPauseId(R.drawable.ic_pause);
            paused=false;
            }else {
             m.setPlayPauseId(R.drawable.ic_play);
             paused = true;
            }
       }

    });
}

接口:

public interface PlayPauseClick{
 void imageButtonOnClick(View v, int position);
}

还有我的适配器:

public class FunDapter<T> extends BaseAdapter implements Filterable  {

protected List<T> mDataItems;
protected LongExtractor<T> idExtractor;
protected final Context mContext;
private final int mLayoutResource;
private final BindDictionary<T> mBindDictionary;
private FunDapterFilter<T> funDapterFilter;


public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 BindDictionary<T> dictionary) {
  this(context, dataItems, layoutResource, null, dictionary);
}

public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 LongExtractor<T> idExtractor, BindDictionary<T> dictionary) {
    this.mContext = context;
    this.mDataItems = dataItems;
    this.mOrigDataItems = dataItems;
    this.mLayoutResource = layoutResource;
    this.idExtractor = idExtractor;
    this.mBindDictionary = dictionary;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View v = convertView;
    final GenericViewHolder holder;
    if (null == v) {
        LayoutInflater vi =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(mLayoutResource, null);
        holder = new GenericViewHolder();
        holder.root = v;



    holder.playPause=(ImageView)v.findViewById(R.id.playPause); 
    holder.playPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((PlayPauseClick)mContext).imageButtonOnClick(v, position);        
        }
    });


    return v;
}

推荐答案

如果要在Fragment上定义接口,则需要将其显式设置为Adapter,而不要依赖Context来为您处理该接口,因为 Activity mContext ,不是您的Fragment.

If you want to define your interface on the Fragment, then you need to explicitly set that onto the Adapter, not rely on the Context to handle that for you because Activity is mContext, not your Fragment.

因此,这是定义了接口的适配器.当您单击按钮时,它将被激活.

So, here is your adapter with an interface defined. It gets activated when you click the button.

public class FunDapter<T> extends BaseAdapter implements Filterable  {

    public interface PlayPauseClick {
     void imageButtonOnClick(View v, int position);
    }

    private PlayPauseClick callback;

    ...

    public void setPlayPauseClickListener(PlayPauseClick listener) {
        this.callback = listener;
    }

    ...

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ...
        holder.playPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (callback != null) {
                    callback.imageButtonOnClick(v, position);
                }

在Fragment中,您可以实现该接口(也称为回调)

And in the Fragment, you can implement that interface (aka your callback)

public class YourFragment extends Fragment 
    implements YourAdapter.PlayPauseClick // see here

    // private ... adapter;
    private List<Product> data = new ArrayList<Product>();

    @Override 
    public View onCreateView(...) {
        // Set your adapter
        // Pass the listener here
        this.adapter = new FunDapter<Product>(...); 
        this.adapter.setPlayPauseClickListener(this);
    }

    @Override
    public void imageButtonOnClick(View v, int position) {
        // TODO: Implement this
    }

并且您应该删除Activity类上的接口实现.

And you should remove the interface implementation on the Activity class.

这篇关于从适配器调用片段方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆