Android 从ListFragment移动到Fragment

Android 从ListFragment移动到Fragment,android,android-fragments,Android,Android Fragments,我想通过项的onClick()从自定义ListView(它是一个ListFragment)移动到另一个片段。我还想传递一些数据,我通过 Fragment f = null; f = new DescriptionFragment();// it extends Fragment Bundle args = new Bundle(); args.putString("title", "This is title"); args.putString("desc", "This is Descript

我想通过项的onClick()从自定义ListView(它是一个ListFragment)移动到另一个片段。我还想传递一些数据,我通过

Fragment f = null;
f = new DescriptionFragment();// it extends Fragment
Bundle args = new Bundle();
args.putString("title", "This is title");
args.putString("desc", "This is Description");
f.setArguments(args);
但使用此代码不会发生任何事情

我在我的级别上尝试过,但现在我认为我应该使用FragmentManager和FragmentTransaction。我试图通过写作来实现它

 FragmentManager fragmentManager;
 android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.remove(f);
 fragmentTransaction.commit();
 fragmentManager.executePendingTransactions();
 fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.add(containerViewId, fragment);// this line gives error & i dont know what to write here
 fragmentTransaction.commit();
我想我的目的地班很好。看起来像这样

public class DescriptionFragment extends Fragment {

public DescriptionFragment(){}

TextView title,desc;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.description,container,false);
    Bundle bundle = this.getArguments();
    String stitle = bundle.getString("title");
    String sdesc = bundle.getString("desc");

    TextView title = (TextView)rootView.findViewById(R.id.textView4);
    TextView desc = (TextView)rootView.findViewById(R.id.textView3);

    title.setText(stitle);
    desc.setText(sdesc);

    return rootView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="120dp" 
     android:background="@drawable/flagc">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:gravity="center"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>
</ScrollView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ashokb" >

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
             android:textAppearance="?android:attr/textAppearanceMedium"
             />

    </ScrollView>

</RelativeLayout>

</LinearLayout>
在description.xml中,我是否必须实现FrameLayout

然而description.xml看起来是这样的

public class DescriptionFragment extends Fragment {

public DescriptionFragment(){}

TextView title,desc;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.description,container,false);
    Bundle bundle = this.getArguments();
    String stitle = bundle.getString("title");
    String sdesc = bundle.getString("desc");

    TextView title = (TextView)rootView.findViewById(R.id.textView4);
    TextView desc = (TextView)rootView.findViewById(R.id.textView3);

    title.setText(stitle);
    desc.setText(sdesc);

    return rootView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="120dp" 
     android:background="@drawable/flagc">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:gravity="center"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>
</ScrollView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ashokb" >

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
             android:textAppearance="?android:attr/textAppearanceMedium"
             />

    </ScrollView>

</RelativeLayout>

</LinearLayout>

我希望我把我的问题说清楚


有什么帮助吗?

在活动中创建框架布局

为每个片段创建2个不同的片段布局

当一个片段被调用时,使用

transaction.replace(frameLayoutId, fragmentToReplace);
transaction.commit()
下面是我在应用程序中使用的片段转换示例

public void changeTab(View view) {

    int id = view.getId();
    if (modelSelected) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        InfoFragment info = new InfoFragment();
        SettingsFragment settings = new SettingsFragment();
        ReportFragment reports = new ReportFragment();

        switch (id) {
        case R.id.infoTab:
            transaction.replace(R.id.loadTabFrag, info);
            break;
        case R.id.settingsTab:
            transaction.replace(R.id.loadTabFrag, settings);
            break;
        case R.id.reportsTab:
            transaction.replace(R.id.loadTabFrag, reports);
            break;
        default:
            break;
        }
        transaction.commit();
        estheticTabHandler(id);
    } else {
        createSelectModelToast();
    }
}
如果您想将数据从一个片段传递到另一个片段,那么应该使用Bundle。在这种情况下,您应该创建一个片段并设置其参数

例如:

MyFragment myFrag = new MyFragment();
Bundle myBundle = new Bundle();
myBundle.putWhateverData(data); // Where whatever data is the object that you are passing
myFragment.setArguments(myBundle);
在您的
MyFragment onCreateView()
中,您可以使用:

Bundle args = this.getArguments();

在上面的一行中,您声明了片段对象,所以它给出了错误。您删除了f片段,但没有给出任何有效的片段,因此会出现错误。只需声明您的片段与f片段相同。就是这样…

fragmentTransaction.add(containerWebId,fragment);//这行给出了错误&我不知道在它的参数中写什么谢谢你的回复。解决了第二个参数中的错误。你能告诉我第一个参数中的错误吗?它应该像
R.id.***
你的第一个参数容器,第二个是你要填充到该容器中的参数。因此,第一个参数是framelayout id,第二个参数是fragment。