从ListFragment移动到Fragment

时间:2014-06-03 22:01:48

标签: android android-fragments

我希望通过项目的onClick()从我的自定义ListView(这是一个ListFragment)移动到另一个Fragment。我想传递一些我做的数据

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;
}
}

在description.xml中我是否必须实现FrameLayout ??

然而,description.xml看起来像这样

<?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 个答案:

答案 0 :(得分:0)

在您的活动中创建框架布局。

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

调用片段时使用

transaction.replace(frameLayoutId, fragmentToReplace);
transaction.commit()

以下是我在App中使用的片段转换示例

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();

答案 1 :(得分:0)

fragmentTransaction.add(containerViewId, fragment);

在您声明片段对象的上一行中,因此它给出了错误。你删除f片段,但没有任何有效的片段给出,所以它给你error.just声明你的片段与f片段相同。多数民众赞成......