gpt4 book ai didi

java - 从主要 Activity 中打开多个 fragment

转载 作者:行者123 更新时间:2023-12-02 01:18:25 27 4
gpt4 key购买 nike

我使用类 MultipleFragment 扩展 Fragment。我想从 MainActivity 打开这个 fragment 。为此,我使用 MultipleFragment myfragment 的实例:

MultipleFragment myfragment = new MultipleFragment();
myfragmentTransaction.replace(R.id.fragment_multiple_list_view , myfragment).commit();

我收到一个错误,参数“myfragment”应该是 Fragment 类型。

error: incompatible types: MultipleFragment cannot be converted to 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()

框架 fragment (android.app.Fragmentandroid.app.FragmentManager)已弃用,您应该改用支持库/AndroidX 版本。

关于java - 从主要 Activity 中打开多个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57642182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com