[英]Android communication between fragment and baseadapter
Need expert opinion how should i structure this issue. 需要专家意见,我应该如何构造这个问题。 I have a custom method process_filter that resides in a fragment as it needs to access a private
TextView
and List
of this fragment. 我有一个自定义方法process_filter ,它驻留在一个片段中,因为它需要访问私有
TextView
和此片段的List
。
In the middle of processing, this fragment will access a BaseAdapter
and inside this BaseAdapter
I need to use back process_filter method 在处理过程中,此片段将访问
BaseAdapter
并且在此BaseAdapter
内部,我需要使用back process_filter方法
Basically here is the structure: 基本上,这里是结构:
MyFragment.java 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 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) {
//Here I need to access back process_filter from fragment
process_filter ("string 1, string 2");
}
}
}
}
Create an interface from your adapter to your fragment. 创建一个从适配器到片段的接口。
In your adapter create the interface and pass it in your adapter's constructor 在您的适配器中创建接口,并将其传递给适配器的构造函数
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");
}
}
}
}
Last thing, implement it in your fragment like this 最后一件事,像这样在片段中实现它
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) {
// Process the filter
}
}
Sorry For late answer, It may useful for newbies 抱歉,答案太晚了,可能对新手有用
Note: @Marko answer was perfect but it required a small change while initializing adapter with parameters. 注意: @Marko的答案是完美的,但是在初始化带有参数的适配器时需要做一些改动。
Step 1: Create an listener interface. 步骤1:创建一个侦听器界面。
Public interface AdapterListener{
Public void myListener();
}
Step 2: Implement your interface in Fragment class. 步骤2:在Fragment类中实现您的接口。
public class MyFragment extends Fragment implements AdapterListener{
@Override
public void myListener(){
//Do what you want
}
Step 3: Initializing Adapter class inside MyFragment
class . 步骤3:在
MyFragment
类内初始化Adapter类。
MyFragmentAdapter myfragmentAdapter=new MyFragmentAdapter(MyFragment.this);
Step 4: Initialize your listener in adapter class. 步骤4:在适配器类中初始化您的侦听器。
public class MyFragmentAdapter extends Adapter<Your Code Related>{
private AdapterListener adapterListener;
public MyFragmentAdapter(AdapterListener adapterListener){
this.adapterListener=adapterListener;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.