CoderAnswer - 程序员编程问答

fragment和baseadapter之间的Android通信

by @Bih Cheng

java android android-fragments baseadapter

需要专家意见我应该如何构建这个问题。我有一个自定义方法 process_filter ,它驻留在一个片段中,因为它需要访问该片段的私有TextViewList

在处理过程中,这个片段将访问BaseAdapter并在BaseAdapter里面我需要使用返回 process_filter 方法

基本上这里是结构:

MyFragment.java

public class MyFragment extends Fragment {

   private List<String> filter_list;
   private TextView no_of_filter;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

   View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
   no_of_filter = (TextView) view.findViewById(R.id.no_of_filter_tv);
   .
   MyAdapter custom_adapter = new MyAdapter(context, "string 1", "string 2");
   .
   process_filter("string 1", "string 2");
   .
   }

   public void process_filter(String in_preference, String current_value)
   {
       no_of_filter.setText(in_preference);
   }

MyAdapter.java

   class MyAdapter extends BaseAdapter {

       public View getView( final int position, View convertView, ViewGroup   parent)
       {
             holder.checkBox.setOnClickListener( new View.OnClickListener() {
                public void onClick(View v) {
                    //这里我需要从fragment访问process_filter
                    process_filter ("string 1, string 2");
                }
             }
       }
   }

2个答案

9 by @Marko

从适配器到片段创建一个接口。

在你的适配器中创建接口并将其传递给适配器的构造函数

class MyAdapter extends BaseAdapter {

    public interface IProcessFilter {
        void onProcessFilter(String string1, String string2)
    }

    private IProcessFilter mCallback;

    public MyAdapter(Context context, String string1, String string2, IProcessFilter callback) {
        mCallback = callback;
    }

    public View getView( final int position, View convertView, ViewGroup   parent)
    {
        holder.checkBox.setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {
                mCallback.onProcessFilter("string1", "string2");
            }
        }
   }
}

最后一件事,在你的片段中实现它

public class MyFragment extends Fragment implements IProcessFilter {
    ...
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
        no_of_filter = (TextView) view.findViewById(R.id.no_of_filter_tv);

        MyAdapter custom_adapter = new MyAdapter(context, "string 1", "string 2", this);  
    }

    @Override
    public void onProcessFilter(String string1, String string2) {
        //处理过滤器
    }
}
-1 by @brahmy adigopula

抱歉,对于迟到的回答,它可能对新手有用

注意: @Marko答案很完美,但在使用参数初始化适配器时需要进行一些小改动。

步骤1:创建一个监听器接口。

Public Interface AdapterListener{
Public void myListener();
}

步骤2:在Fragment类中实现您的接口。

public class MyFragment extends Fragment implements AdapterListener{

@Override
public void myListener(){
//完成你想要的
}  

步骤3:MyFragment类中初始化Adapter类。

MyFragmentAdapter myfragmentAdapter=new MyFragmentAdapter(MyFragment.this);

步骤4:在适配器类中初始化您的侦听器。

 public class MyFragmentAdapter extends Adapter<Your Code Related>{
    private AdapterListener adapterListener;

    public MyFragmentAdapter(AdapterListener adapterListener){
    this.adapterListener=adapterListener;
}

相关问题