从适配器调用片段方法

时间:2017-02-14 18:30:18

标签: android android-fragments interface

我需要在我的适配器中调用Fragment方法,但是我收到错误:

  

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

我在我的片段中实现了我的界面,但我仍然得到这个错误,因为朋友已经告诉我-Xmx1024(我的适配器上下文)不能是mContext

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

My Fragment方法:

Fragment

接口:

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);
}

2 个答案:

答案 0 :(得分:6)

如果你想在Fragment上定义你的界面,那么你需要在适配器上明确地设置它,而不是依赖于Context来处理它,因为ActivitymContext,而不是你的分段。

所以,这是你的适配器定义了一个接口。单击按钮时会激活它。

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);
                }

在片段中,您可以实现该界面(也就是您的回调)

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类上的接口实现。

答案 1 :(得分:0)

您看到的错误是因为mContext是一个活动,而不是您的PlayPauseClick。我建议如下:

1)当你创建Fragment时,给它一个标签并将其保存在活动中:

    MyFragment fragment = new MyFragment();
    final FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    View containerView = getContainerView();
    ft.add(containerView.getId(), fragment, tag);

2)在适配器内部执行以下操作:

holder.playPause.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        FragmentManager fm = ((MyActivity)mContext).getFragmentManager();
        final MyFragment dismissedFragment = (MyFragment)fm.findFragmentByTag(((MyActivity)mContext).getMyFragmentTag());
        dismissedFragment.imageButtonOnClick(v, position);        
    }
}