尝试使用此代码从适配器类启动片段
public void startFragment(Context context, int position) {
Activity activity = (Activity) context;
android.app.FragmentManager manager = activity.getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = manager.beginTransaction();
DepartmentDetails departmentDetails = new DepartmentDetails();
Bundle bundle = new Bundle();
bundle.putString("id", departmentList.get(position).id);
bundle.putSerializable("details", departmentList);
departmentDetails.setArguments(bundle);
fragmentTransaction.add(R.id.container, departmentDetails);
fragmentTransaction.commit();
}
但它在fragmentTransaction.add(R.id.container, departmentDetails);
并在执行时显示
Error:(90, 28) error: no suitable method found for add(int,DepartmentDetails)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment)
这是我正在使用的片段..
public class DepartmentDetails extends Fragment implements CallBacks{
ImageView ivProfile;
TextView tvName, tvBasicInfo, tvDescription;
String id, dean;
Departments details;
ArrayList<String> faculty, programs;
RequestParams params;
MyHttp myHttp;
String url;
public DepartmentDetails() {
// Required empty public constructor
}
答案 0 :(得分:1)
您可以使用以下代码从适配器类打开片段;
Fragment fragment = new MyFragment();
FragmentManager fm = context.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
答案 1 :(得分:1)
添加DepartmentDetails片段类
import android.support.v4.app.Fragment ;
而不是
import android.app.Fragment
答案 2 :(得分:0)
请尝试使用以下代码。
Fragment fragment = new MyFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();