【问题标题】:Start another Fragment from ListFragment android从 ListFragment android 启动另一个 Fragment
【发布时间】:2019-01-10 12:26:01
【问题描述】:

你能告诉我如何从一个列表片段LearnFragment开始另一个片段TrickFragment吗?

这是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 的列表视图项(如果重要的话)

<?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>

【问题讨论】:

  • 能否添加activity_layout.xml
  • 我在评论中添加了

标签: java android xml listview android-fragments


【解决方案1】:

使用这个:

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

【讨论】:

    【解决方案2】:

    MainActivity

     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 上的完整示例。

    【讨论】:

    • java.lang.IllegalArgumentException:未找到片段 TrickFragment{1fc6342d #2 id=0x7f090051} 的 id 0x7f090051 (studio.pinbu.learnmath:id/fg_learn) 的视图
    • W/art:在 Android 4.1 之前,方法 int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) 会错误地覆盖 android.widget.ListView 中的包私有方法
    • 我认为我不能使用替换,因为没有要替换的 xml,我生成列表视图...
    • 检查我的答案更新,顺便说一句,当您需要添加额外信息时,请编辑您的问题并且不要将其添加为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多