Android 片段和基础适配器之间的通信
Android communication between fragment and baseadapter
需要专家意见我应该如何组织这个问题。我有一个自定义方法 process_filter 驻留在片段中,因为它需要访问该片段的私有 TextView
和 List
。
在处理过程中,这个片段会访问一个BaseAdapter
,在这个BaseAdapter
里面我需要使用back 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) {
//Here I need to access back process_filter from fragment
process_filter ("string 1, string 2");
}
}
}
}
创建一个从适配器到片段的接口。
在您的适配器中创建接口并将其传递给您的适配器的构造函数
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) {
// Process the filter
}
}
抱歉回答晚了,可能对新手有用
注意:@Marko 的回答很完美,但在使用参数初始化适配器时需要进行一些小改动。
步骤 1: 创建侦听器接口。
Public interface AdapterListener{
Public void myListener();
}
步骤 2: 在 Fragment 中实现您的界面 class。
public class MyFragment extends Fragment implements AdapterListener{
@Override
public void myListener(){
//Do what you want
}
步骤 3: 初始化适配器 class inside MyFragment
class .
MyFragmentAdapter myfragmentAdapter=new MyFragmentAdapter(MyFragment.this);
步骤 4: 在适配器中初始化您的侦听器 class。
public class MyFragmentAdapter extends Adapter<Your Code Related>{
private AdapterListener adapterListener;
public MyFragmentAdapter(AdapterListener adapterListener){
this.adapterListener=adapterListener;
}
需要专家意见我应该如何组织这个问题。我有一个自定义方法 process_filter 驻留在片段中,因为它需要访问该片段的私有 TextView
和 List
。
在处理过程中,这个片段会访问一个BaseAdapter
,在这个BaseAdapter
里面我需要使用back 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) {
//Here I need to access back process_filter from fragment
process_filter ("string 1, string 2");
}
}
}
}
创建一个从适配器到片段的接口。
在您的适配器中创建接口并将其传递给您的适配器的构造函数
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) {
// Process the filter
}
}
抱歉回答晚了,可能对新手有用
注意:@Marko 的回答很完美,但在使用参数初始化适配器时需要进行一些小改动。
步骤 1: 创建侦听器接口。
Public interface AdapterListener{
Public void myListener();
}
步骤 2: 在 Fragment 中实现您的界面 class。
public class MyFragment extends Fragment implements AdapterListener{
@Override
public void myListener(){
//Do what you want
}
步骤 3: 初始化适配器 class inside MyFragment
class .
MyFragmentAdapter myfragmentAdapter=new MyFragmentAdapter(MyFragment.this);
步骤 4: 在适配器中初始化您的侦听器 class。
public class MyFragmentAdapter extends Adapter<Your Code Related>{
private AdapterListener adapterListener;
public MyFragmentAdapter(AdapterListener adapterListener){
this.adapterListener=adapterListener;
}