从ListFragment android

时间:2018-08-02 16:43:31

标签: java android xml listview android-fragments

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

这是列表片段的代码:

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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

  

使用此:

np.where((df.iloc[:,1:4].isin(code_flags)).any(1),1,0)