片段到片段android调用

时间:2017-08-26 22:25:21

标签: android android-fragments

当我点击listitem(片段)时我想要打开片段但是失败了,没有错误logcat。

fragment_item_two.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.ItemTwoFragment"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!-- TODO: Update blank fragment layout -->
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</FrameLayout>

fragment1(listview):( ItemTwoFragment.java)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_item_two, container, false);
    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            view.findViewById(R.id.navigation);

    contactList = new ArrayList<>();
    lv = (ListView) view.findViewById(R.id.list);
    new GetContacts().execute();

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> pa, View v, int p, long id) {
            int itm = (int) pa.getItemAtPosition(p);

            fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragmentContainer, new NewsFragment());
            transaction.commit();

        }
    });

    return view;
}

NewsFragment:

public class NewsFragment extends Fragment {
}

fragment_news:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.NewsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="news fragment" />

</FrameLayout>

如何解决这个问题?或者你可以给我这个案例的参考?感谢

1 个答案:

答案 0 :(得分:0)

你忘了给你的片段充气

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_news, container, false);

    // Your code ....

    return rootView;
}