解决网友 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的数据或者页面出问题。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值