簡體   English   中英

Android:ViewPager僅顯示第一個片段,其他頁面為空白

[英]Android: ViewPager displays only first fragment, other pages blank

我有一個ViewPager,它將一系列FoodItem顯示為片段。 那部分工作正常。

現在,我希望具有一個功能,以便用戶單擊按鈕時,他們可以查看有關該FoodItem的更多信息。

為此,我想用另一個片段替換單擊的片段。 在這里修改了此代碼以適合我的目的。 它有效-當我單擊按鈕時,它將替換該特定片段。 但它僅顯示並適用於一個片段。 其他片段完全空白。

這是我的適配器和片段:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<FoodItem> items;

public SectionsPagerAdapter(FragmentManager fm, ArrayList<FoodItem> items) {
    super(fm);
    this.items = items;
}

@Override
public Fragment getItem(int position) {
    System.out.println("PRINT: POS " + position);
    RootFragment f = new RootFragment();
    return f.newInstance(position);


}

@Override
public int getCount() {
    return items.size();
}


public class PlaceholderFragment extends Fragment{

    private final String ARG_SECTION_NUMBER = "section_number";


    public PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        System.out.println("PRINT: INSTANCE" + sectionNumber);
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_card_layout, container, false);
        singleItem = items.get(getArguments().getInt(ARG_SECTION_NUMBER));

        // populate views here

        //Below is the button for replacing the Fragment with another Fragment.

        Button testMore = (Button) view.findViewById(R.id.testMore);
        testMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction trans = getFragmentManager()
                        .beginTransaction();
                                trans.replace(R.id.root_frame, new MoreInfoFragment().newInstance(getArguments().
                        getInt(ARG_SECTION_NUMBER)));
                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                trans.addToBackStack(null);

                trans.commit();
            }
        });

        return view;
    }

}

public class MoreInfoFragment extends Fragment {

    private final String ARG_SECTION_NUMBER = "section_number";


    public MoreInfoFragment newInstance(int sectionNumber) {
        MoreInfoFragment fragment = new MoreInfoFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public MoreInfoFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.more_info_layout, container, false);
        singleItem = items.get(getArguments().getInt(ARG_SECTION_NUMBER));

        //populate views here

        return view;

    }
}

public class RootFragment extends Fragment {

    private static final String TAG = "RootFragment";
    private final String ARG_SECTION_NUMBER = "section_number";

    public RootFragment newInstance(int sectionNumber) {
        RootFragment fragment = new RootFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.root_fragment, container, false);

       FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();

      transaction.replace(R.id.root_frame, new PlaceholderFragment().newInstance(getArguments().
        getInt(ARG_SECTION_NUMBER)));

        System.out.println("PRINT: ROOT" + getArguments().getInt(ARG_SECTION_NUMBER));


        transaction.commit();

        return view;
    }

}
}

這是我的主要活動:

private ViewPager mViewPager;
private static ArrayList<FoodItem> sample;
private PagerContainer mContainer;


public CardLayout(){

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    setContentView(R.layout.activity_card_layout);

   //Sample array
    sample = new ArrayList<>();
    sample.add(new FoodItem("French Fries", "Super delicious and crispy!"))
    sample.add(new FoodItem("Burger", "Made with real Krabby Patty"))
    sample.add(new FoodItem("Pickles", "So green")) // This is the only one that displays

   //PagerContainer is a FrameLayout that has a ViewPager as a child. 
   //I want my ViewPager to display edges of the previous and next part so    
   //PagerContainer is used for that purpose.
    mContainer = (PagerContainer) findViewById(R.id.pager_container);

    mViewPager = (ViewPager) findViewById(R.id.viewPager);

   // This is to adjust the viewpager according to size of screen 
    mContainer.getViewTreeObserver().
            addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @SuppressLint("NewApi")
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {
                    height = mContainer.getHeight();

                    double heightSize =  height * 0.8;
                    height = (int) heightSize;
                    double widthSize = height*0.6;
                    width = (int) widthSize;


                    mViewPager.setLayoutParams(new FrameLayout.LayoutParams(width, height, Gravity.CENTER));
                    mViewPager.setPageMargin(width /30);


                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
                        mContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    else
                        mContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), sample);
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(mSectionsPagerAdapter.getCount());


    mViewPager.setClipChildren(false);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

}

哦,正在顯示的Fragment甚至不是我ArrayList中的第一個! 這是最后一次。

我覺得自己似乎遺漏了一些明顯的東西,但是我已經看了十遍這段代碼,但仍然找不到阻止其他頁面顯示的原因。

例如,這是首頁的外觀: 在此處輸入圖片說明

如果我滑到第二頁,那是全白的(我的root_fragment背景色)

在此處輸入圖片說明

這適用於之后的每個片段。

你們有什么想法/建議嗎?

覆蓋SelectionsPageAdapter中的方法getItem()

 @Override
    public Fragment getItem(int position) {
        // if the position is 0 we are returning the First tab
        if (position == 0) 
        {
            Fragment tab1 = new PlaceholderFragment ();
            return tab1;

        } else if (position == 1)           
        {
            Fragment tab2 = new MoreInfoFragment ();
            return tab2;

        } else if (position == 2) {

            Fragment tab3 = new RootFragment ();
            return tab3;

        } 

    }

暫無
暫無

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

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