我想从我的BaseAdapter类调用Fragement。在这个课程中我点击了我想要调用新片段的按钮,但是我无法得到它。我必须将点击按钮的值传递给片段。
BaseAdapter类
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;
}
}
我想点击view_log
从BaseAdapter类调用此Fragment。请帮助我们如何执行此操作
Martin Cazares 之后我做了这个
在活动
中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;
}
在 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"));
}
});
答案 0 :(得分:15)
老实说,如果你必须从BaseAdapter调用你的片段,那么你的应用程序的体系结构是非常错误的,那么你就是紧密耦合组件而Spaghetti Code很快就会出现问题,如果你想让它保持干净,做一个监听器或发送从它那里广播,并像往常一样从你的活动中调用你的片段,重点是让组件完成他们想要的工作而不是一个单独的类,这是一个可怕的事情,代码变得不那么清晰
正如所解释的,在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"))
}
});
现在,在你的活动中,你所要做的就是用“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...
}
};
完成后不要忘记取消注册,如果你需要将一些参数传递给片段,你可以在发送广播消息时使用意图中的额外内容......
注意:现在最好使用LocalBroadcastManager。
问候!
答案 1 :(得分:11)
使用
((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();
}
});
答案 2 :(得分:2)
只需使用它。
public void onClick(View view) {
LogFrag fragment2 = new LogFrag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
答案 3 :(得分:0)
最简单的方法: 在Fragment中进行
Adapter a = new Adapter(arg,Fragment.this);// or only this. This will pass the fragment object to the 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
}
});
答案 4 :(得分:0)
将FragmentManager xxx 对象解析为Adpter构造函数,
**xxx**.beginTransaction().replace(your_container, new YourNewFragment()).addToBackStack(null).commit();
答案 5 :(得分:0)
FragmentManager manager = ((AppCompatActivity)
context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new AlbumFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
答案 6 :(得分:-2)
从Custom Base Adapter调用片段的完美方式
fragmentManager.beginTransaction()
.add(R.id.content_frame, new YourFragmentName())
.addToBackStack("fragBack")).commit();
((Activity) context).setTitle("Title For Action Bar");`