简体   繁体   English

单击项目时具有ListView打开片段的Android活动

[英]Android activity with ListView open fragment on item click

I have an Activity with ListView, when clicking on a list item opens a new fragment. 我有一个带有ListView的Activity,单击一个列表项时会打开一个新片段。 Do I need to create a new fragment every time like this? 我每次都需要创建一个新片段吗?

     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.root_layout,new MyFragment());

Or will be enough to create a fragment once and then use it? 还是足以一次创建一个片段然后使用它?

in activity: 活动中:

     MyFragment myFragment = new MyFragment();
     ......

in onItemClickListener: 在onItemClickListener中:

     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.root_layout,myFragment);

No, you don't need to create it every time. 不,您不需要每次都创建它。 First, instead of using "add", use "replace". 首先,使用“替换”代替使用“添加”。 If there is no fragment in fragment manager, your fragment will be added, instead it will be replaced. 如果片段管理器中没有片段,则将添加您的片段,而是将其替换。 If you use "add", then you could accidentally add more than one fragment. 如果使用“添加”,则可能会意外添加多个片段。

You should check for the fragment in the fragment manager and call methods for content updates. 您应该在片段管理器中检查片段,并调用方法来更新内容。

Example: 例:

myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
if (myFragment == null) {
  myFragment = MyFragment.getInstance(someDataIfNeeded);
  fragmentManager.beginTransaction().replace(R.id.my_fragment, myFragment).commit();
} else {
  myFragment.updateFragmentContent(someData);
}

It depends on your case. 这取决于您的情况。 In most cases every list item opens a different fragment (with different data). 在大多数情况下,每个列表项都会打开一个不同的片段(具有不同的数据)。 Then you have to make a static newInstance(Data mySerializableData) method in your fragment, use default constructor inside of it, pass data over Fragment arguments See DetailFragment and use fragmentTransaction.replace() in your activity to add this fragment. 然后,您必须在片段中创建一个静态newInstance(Data mySerializableData)方法,在其中使用默认构造函数,通过Fragment参数传递数据( 请参见DetailFragment),并在活动中使用fragmentTransaction.replace()来添加此片段。

When you dont want your fragment to be changed you can create it only once as you say but there is no need of adding it on every item click. 如果您不希望更改片段,则只能按您的说明创建一次,但是无需在每次单击项目时都添加它。 So one creation and only one add. 因此,一次创建,一次添加。

check instance of that fragment everytime like this- 每次像这样检查该片段的实例-

In your fragment class - 在您的片段类中-

public static final MyFragment newInstance()
{
    MyFragment f = new MyFragment();
    return f;
}

and in your activity when you want to create fragment - 在您要创建片段的活动中-

 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
 fragmentTransaction.add(R.id.root_layout,MyFragment.newInstance());

this is well n good manner... 这是很好的方式...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM