如何从 BaseAdapter Class Android 调用片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22265378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a fragment from BaseAdapter Class Android?
提问by Pooja Dubey
I want to call a Fragement from my BaseAdapter Class. In this class I have button on click of which I want to call the new fragment, but I am not able to get this. I have to pass values from the click of the button to the fragment.
我想从我的 BaseAdapter 类调用 Fragment。在这门课中,我点击了按钮,我想调用它的新片段,但我无法得到这个。我必须将点击按钮的值传递给片段。
BaseAdapter Class
基本适配器类
public class StatusAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public StatusAdapter(Activity a,
ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.approval_selftrip_inner, null);
TextView approved_by = (TextView) vi.findViewById(R.id.approved_by);
TextView status = (TextView) vi.findViewById(R.id.status);
TextView trip = (TextView) vi.findViewById(R.id.trip);
Button view_log = (Button)vi.findViewById(R.id.view_log);
HashMap<String, String> list = new HashMap<String, String>();
list = data.get(position);
approved_by.setText(list.get("first_id"));
status.setText(list.get("status"));
trip.setText(list.get("trip"));
view_log.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Here i want to call my fragment
}
});
return vi;
}
}
Fragement
片段
public class Log extends Fragment {
Context context ;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
rootView = inflater.inflate(R.layout.activity_log,
container, false);
return rootView;
}
}
I want to call this Fragment from the BaseAdapter Class on click of view_log
.Please help me how can we do this
我想在单击时从 BaseAdapter 类中调用此 Fragment view_log
。请帮助我我们如何执行此操作
After Martin CazaresI have done this
在Martin Cazares 之后我做了这个
In Activity
在活动
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
rootView = inflater.inflate(R.layout.activity_main,
container, false);
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Recived",
Toast.LENGTH_LONG).show();
ApprovalLog fragment2 = new ApprovalLog();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment2);
fragmentTransaction.commit();
}
};
return rootView;
}
In The AdapterClass
在适配器类中
view_approvallog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CommonUtils.showAlert("Test in Adapter", activity);
activity.registerReceiver(mBroadcastReceiver, new IntentFilter(
"start.fragment.action"));
}
});
采纳答案by Piyush
Simply use this.
简单地使用这个。
public void onClick(View view) {
LogFrag fragment2 = new LogFrag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
回答by Martin Cazares
Honestly if you have to call your fragment from a BaseAdapter something is terribly wrong with your application's architecture, you are tightly coupling components and Spaghetti Code will be a problem soon, if you want to keep it clean, make a listener or send a broadcast from it, and call your fragment from your activity as you usually do, the point is to keep components doing the job they are intended for and not having all in one single class, that's a terrible thing to do and code becomes less legible.
老实说,如果你必须从 BaseAdapter 调用你的片段,那么你的应用程序的架构就会出现严重错误,你是紧密耦合的组件,意大利面条代码很快就会成为一个问题,如果你想保持干净,创建一个监听器或发送一个广播它,并像往常一样从您的活动中调用您的片段,重点是让组件完成它们预期的工作,而不是将所有组件都放在一个类中,这是一件可怕的事情,代码变得不那么清晰。
As explained a simple approach to decouple things in android is by sending broadcast messages, so this is one way of doing it:
正如所解释的,在 android 中解耦事物的一种简单方法是发送广播消息,所以这是一种方法:
view_log.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Here i want to call my fragment
//You need to pass a reference of the context to your adapter...
context.sendBroadcast(new Intent("start.fragment.action"))
}
});
Now in your activity all you have to do is register a BroadcastReceiver with the "start.fragment.action" and that's it inside of it just call your fragment:
现在,在您的活动中,您所要做的就是使用“start.fragment.action”注册一个 BroadcastReceiver,然后在其中调用您的片段:
//In your activity...
context.registerReceiver(mBroadcastReceiver, new IntentFilter("start.fragment.action"))
.
.
.
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//This piece of code will be executed when you click on your item
// Call your fragment...
}
};
Do not forget to unregister when done, and if you need to pass some parameters to the fragment you can use extras in the intent when sending the broadcast message...
完成后不要忘记取消注册,如果您需要将一些参数传递给片段,您可以在发送广播消息时在意图中使用额外内容...
NOTE: LocalBroadcastManagerwould be better to use now.
注意:现在使用LocalBroadcastManager会更好。
Regards!
问候!
回答by Tigran Sarkisian
Use
用
((Activity) mContext).getFragmentManager();//use this
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new CallThisFragment();
FragmentManager fragmentManager = ((Activity) mContext).getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_activity, fragment).commit();
}
});
回答by shailesh
Easiest way: Do in Fragment
最简单的方法:在片段中进行
Adapter a = new Adapter(arg,Fragment.this);// or only this. This will pass the fragment object to the adapter.
and in Adapter
并在适配器中
v.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(fragment!=null)
fragment.ValidateList();// call any public method of the fragment
}
});
回答by user3878959
Parse FragmentManager xxxobject to Adpter Constructor,
将 FragmentManager xxx对象解析为 Adpter 构造函数,
**xxx**.beginTransaction().replace(your_container, new YourNewFragment()).addToBackStack(null).commit();
回答by Mr. RasTazZ
FragmentManager manager = ((AppCompatActivity)
context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new AlbumFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
回答by Sachin Mohurle
Perfect way to call fragment from Custom Base Adapter
从自定义基础适配器调用片段的完美方式
fragmentManager.beginTransaction()
.add(R.id.content_frame, new YourFragmentName())
.addToBackStack("fragBack")).commit();
((Activity) context).setTitle("Title For Action Bar");`