簡體   English   中英

從ListFragment android啟動另一個Fragment

[英]Start another Fragment from ListFragment android

您能告訴我如何從列表片段TrickFragment開始另一個片段LearnFragment嗎?

這是listfragment的代碼:

public class LearnFragment extends ListFragment {
    int mCurCheckPosition = 0;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.listview_item,R.id.TextBox, getResources().getStringArray(R.array.Titles)));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }

    void showDetails(int index) {
        mCurCheckPosition = index;

        getActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.fg_learn, new TrickFragment())
                .commit();
    }
}

這是它的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"
    android:id="@+id/fg_learn"
    tools:context=".LearnFragment">

</FrameLayout>

TrickFragment

public class TrickFragment extends Fragment {

    public TrickFragment() {
        // Required empty public constructor
    }

    public static TrickFragment newInstance(int index) {
        TrickFragment fragment = new TrickFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

        View view =inflater.inflate(R.layout.fragment_trick, container, false);
        return view;
    }
}

及其xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fg_trick"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TrickFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fg_trick_textview"/>

</LinearLayout>

我提到我有一個名為listview_item的listview項(如果有的話)

<?xml version="1.0" encoding="utf-8"?>
<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=".PlayFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/TextBox"/>

</FrameLayout>

主要活動

 public class MainActivity extends AppCompatActivity {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      updateContainerFrame(new LearnFragment()); // show ListFragment.
    }

    public void updateContainerFrame(Fragment fragment){
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container_frame, fragment)
                .commit();
    }
  }

LearnFragment

    public class LearnFragment extends ListFragment {
        int mCurCheckPosition = 0;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    inflater.getContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.Titles));
            setListAdapter(adapter);
            return super.onCreateView(inflater, container, savedInstanceState);
        }

        private void showDetails(int index) {
            mCurCheckPosition = index;
            if (getActivity() instanceof MainActivity)
                ((MainActivity) getActivity()).updateContainerFrame(TrickFragment.newInstance(index));
        }
     }

TrickFragment

    public class TrickFragment extends Fragment {

        public TrickFragment() {
            // Required empty public constructor
        }

        public static TrickFragment newInstance(int index) {
            TrickFragment fragment = new TrickFragment();
            Bundle args = new Bundle();
            args.putInt("index", index);
            fragment.setArguments(args);
            return fragment;
        }
    }

activity_main.xml中

<!--ViewPager -->
<!--<android.support.v4.view.ViewPager-->
    <!--android:layout_height="fill_parent"-->
    <!--android:layout_width="match_parent"-->
    <!--android:id="@+id/view_pager"/>-->

<!--Use frame container instead of ViewPager-->
<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"
    android:id="@+id/container_frame"
    tools:context=".MainActivity"/>

Github上的完整示例。

用這個 :

getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fg_learn, TrickFragment.newInstance(index))
                        .commit();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM