android-fragments 在android中创建静态类示例

xxhby3vn  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(167)

我看到了一个代码here .还显示如下:
我在Fragment fragment = new DemoObjectFragment();中有点困惑,这里public static class DemoObjectFragment extends Fragment是一个静态类。那么我们如何调用fragment.setArguments(args);

public class CollectionDemoActivity extends FragmentActivity {
    // When requested, this adapter returns a DemoObjectFragment,
    // representing an object in the collection.
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_demo);

        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        mDemoCollectionPagerAdapter =
                new DemoCollectionPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }
}

// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

// Instances of this class are fragments representing a single
// object in our collection.
public static class DemoObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(
                R.layout.fragment_collection_object, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                Integer.toString(args.getInt(ARG_OBJECT)));
        return rootView;
    }
}
vjhs03f7

vjhs03f71#

我创建了一个base class来使用fragments。我不知道这是否对你有用。在base class中你可以放所有fragments都相同的信息。在我的例子中,a想在所有fragments中使用shared view model。所以我把shared view model放在base class中:

/**
 * base class for the application
 */
public abstract class BaseFragment extends Fragment {

    // use shared view model for exchanging information between fragments.
    SharedViewModel sharedViewModel;

    /**
     * this function creates view and shared view model
     * @param inflater - inflater creates the view
     * @param container - container with views
     * @param savedInstanceState - for saving state
     * @return - return view
     */
     @Nullable
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
         ViewGroup container, @Nullable Bundle savedInstanceState) {

         // call base class first
         super.onCreateView(inflater, container, savedInstanceState);

         // then create view
         View fragView = inflater.inflate(getFragment(), container, false);

         // get shared view model.
         sharedViewModel = new 
             ViewModelProvider(requireActivity()).get(SharedViewModel.class);

         // inject fragment depended code
         createView(fragView);

         // return created view
         return fragView;
  }

  /**
   * get fragment
   * (override in fragments and return xml layout name)
   */
   protected abstract int getFragment();

   /**
    * inject fragment depended code while creating the view
    * (override in fragments with fragment depended code)
    */
    protected abstract void createView(View fragView);
}

相关问题