我使用MultipleFragment类扩展Fragment。
我想从MainActivity打开此片段。
为此,我使用MultipleFragment myfragment的实例:
MultipleFragment myfragment = new MultipleFragment();
myfragmentTransaction.replace(R.id.fragment_multiple_list_view , myfragment).commit();
我得到一个错误,认为“ myfragment”参数应为Fragment类型。
错误:类型不兼容:MultipleFragment无法转换为Fragment
myfragmentTransaction.replace(R.id.fragment_multiple_list_view,myfragment).commit();
但是MultipleFragment扩展了Fragment ...
正确的做法是什么?
如何使函数在其参数中识别扩展类的类型?
public class MainActivity extends AppCompatActivity {
private Button btnFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFragment = (Button)findViewById(R.id.btnFragment);
btnFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager myfragmentManager = getFragmentManager ();
FragmentTransaction myfragmentTransaction = myfragmentManager.beginTransaction ();
MultipleFragment myfragment = new MultipleFragment();
myfragmentTransaction.replace(R.id.fragment_multiple_list_view , myfragment).commit();
}
});
}
}
public class MultipleFragment extends Fragment implements AdapterView.OnItemClickListener {
MultipleAdapter mAdapter;
public MultipleFragment() {
// Required empty public constructor
}
public static MultipleFragment newInstance() {
return new MultipleFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_multiple, container, false);
ListView listView = (ListView) view.findViewById(R.id.fragment_multiple_list_view);
MultipleData android = new MultipleData("Android", false);
MultipleData iPhone = new MultipleData("iPhone", false);
MultipleData windowsMobile = new MultipleData("WindowsMobile", false);
MultipleData blackberry = new MultipleData("Blackberry", false);
MultipleData webOS = new MultipleData("WebOS", false);
MultipleData ubuntu = new MultipleData("Ubuntu", false);
MultipleData windows7 = new MultipleData("Windows7", false);
MultipleData max = new MultipleData("Max OS X", false);
MultipleData linux = new MultipleData("Linux", false);
MultipleData os = new MultipleData("OS/2", false);
MultipleData symbian = new MultipleData("Symbian", false);
List<MultipleData> list = new ArrayList<>();
list.add(0, android);
list.add(1, iPhone);
list.add(2, windowsMobile);
list.add(3, blackberry);
list.add(4, webOS);
list.add(5, ubuntu);
list.add(6, windows7);
list.add(7, max);
list.add(8, linux);
list.add(9, os);
list.add(10, symbian);
mAdapter = new MultipleAdapter(getActivity(),list);
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
mAdapter.setSelectedIndex(position);
mAdapter.notifyDataSetChanged();
}
}
最佳答案
您应该使用getSupportFragmentManager()
,而不是getFragmentManager()
。
FragmentManager myfragmentManager = getSupportFragmentManager()
不建议使用Fragments(
android.app.Fragment
,android.app.FragmentManager
)框架,而应该使用支持库/ AndroidX版本。