天天看點

解決網友 ViewPager嵌套Fragment 回退問題

        在QQ群裡有一位朋友提出想實作這種效果:他的viewpager的每個page是一個fragment,假設有兩個page,每個page上有一個按鈕,當滑動到第page2後,按下page2頁面上按鈕時不需要退出整個activity,而是退到page1,再點選page1的按鈕,才推出activity,同理,在點選手機後退鍵的時候需實作相同效果。

        我本身是一個菜,經常關注qq群裡其他人的提問,因為看别人提問,會想想自己是不是在遇到相同問題時有思路去獨立解決,看别人回答,會知道自己的思路是否正确。我想這個是進步的一個有效方法。于是我就想嘗試幫他解決。

        最後我是這樣解決的:

-------- 接口-------

package com.example.buxiaohui.myapplication2;

/**
 * Created by buxiaohui on 2/26/15.
 */
public interface FragmentCallback {
    public void back(int index);
}
           

-------- Activity-------

package com.example.buxiaohui.myapplication2;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.ViewGroup;
import java.util.ArrayList;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity implements FragmentCallback{
    ViewPager mViewPager;
    ArrayList<Fragment> fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager)findViewById(R.id.pager);
        fragmentList = new ArrayList<Fragment>();
        FragmentOne fragmentOne = new FragmentOne();
        FragmentTwo fragmentTwo = new FragmentTwo();
        fragmentTwo.setFragmentCallback(this);
        fragmentOne.setFragmentCallback(this);
        fragmentList.add(fragmentOne);
        fragmentList.add(fragmentTwo);
        mViewPager.setAdapter(new MyFrageStatePagerAdapter(getSupportFragmentManager()));
    }
    public class MyFrageStatePagerAdapter extends FragmentStatePagerAdapter{
        public MyFrageStatePagerAdapter(FragmentManager fm)
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

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

        @Override
        public void finishUpdate(ViewGroup container)
        {
            super.finishUpdate(container);

        }

    }

    @Override
    public void onBackPressed() {
        if(mViewPager.getCurrentItem()==1){
            mViewPager.setCurrentItem(0);
        }else{
            this.finish();
        }
    }

    @Override
    public void back(int index) {
        if(index==1){
            mViewPager.setCurrentItem(0);
        }
        else{
            this.finish();
        }

    }
}
           

-------- Fragment 1-------

package com.example.buxiaohui.myapplication2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by buxiaohui on 2/26/15.
 */
public class FragmentOne extends Fragment {
    private FragmentCallback fragmentCallback;
    private void callBack(int index){
        this.fragmentCallback.back(index);
    }
    public  void setFragmentCallback(FragmentCallback fragmentCallback){
        this.fragmentCallback = fragmentCallback;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


            return inflater.inflate(R.layout.fragment_one, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle bundle) {
            Button button = (Button)view.findViewById(R.id.b1);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack( 0);
                }
            });
        }
}
           

-------- Fragment 2-------

package com.example.buxiaohui.myapplication2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by buxiaohui on 2/26/15.
 */
public class FragmentTwo extends Fragment {
    private FragmentCallback fragmentCallback;
    private void callBack(int index){
        this.fragmentCallback.back(index);
    }
    public  void setFragmentCallback(FragmentCallback fragmentCallback){
        this.fragmentCallback = fragmentCallback;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


            return inflater.inflate(R.layout.fragment_two, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle bundle) {
            Button button = (Button)view.findViewById(R.id.b2);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack(1);
                }
            });
        }
}
           

activity的xml包含一個viewpager,兩個fragment的xml各自隻包含一個button。有點基礎的話代碼稍微看下就懂了。

可以建個工程然後copy一下試試看效果。另外,demo寫的太簡單了,沒有資料和其他布局,是以我不清楚我的寫法會不會導緻由page2回到page1時page1的資料或者頁面出問題。

繼續閱讀