Android开发中常用框架(ViewPager+RadioButton+Fragment)

1.截图:

Android开发中常用框架(ViewPager+RadioButton+Fragment)_第1张图片

2.实现

首先页面布局(图片自己找哈)

activity_main.xml

​
​



    

    
    

        

        

        
    

fragment_one.xml




    

fragment_two.xml




    

fragment_three.xml




    

详细布局内容自己填充哦

然后上Java代码:

activity:

package com.example.wallpaper.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

public abstract class BaseActivity extends FragmentActivity {
    protected BaseActivity act;
    protected final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        act = this;
        setContentView(getLayoutID());
        initView();
        initData();
        initListener();
        // 透明状态栏
      /*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }*/
    }

    @LayoutRes
    protected abstract int getLayoutID();

    protected abstract void initListener();

    protected abstract void initView();

    protected abstract void initData();

    @SuppressWarnings("unchecked")
    protected  E f(int id) {
        return (E) findViewById(id);
    }
}
package com.example.wallpaper.activity;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.wallpaper.R;
import com.example.wallpaper.adapter.PagerMainAdapter;
import com.example.wallpaper.fragment.FragmentOne;
import com.example.wallpaper.fragment.FragmentThree;
import com.example.wallpaper.fragment.FragmentTwo;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends BaseActivity {

    private ViewPager vp;
    private RadioGroup rg;
    private int[] rbs = {R.id.rb_home, R.id.rb_system, R.id.rb_nav};
    private List mFragments;
    private boolean mBackKeyPressed = false;//记录是否有首次按键

    //简化后的方法
    @Override
    protected int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        vp = f(R.id.vp);
        rg = f(R.id.rg);
    }

    @Override
    protected void initData() {
        mFragments = new ArrayList<>();
        FragmentOne one = new FragmentOne();
        FragmentTwo two = new FragmentTwo();
        FragmentThree three = new FragmentThree();
        mFragments.add(one);
        mFragments.add(two);
        mFragments.add(three);
        // 设置填充器
        vp.setAdapter(new PagerMainAdapter(getSupportFragmentManager(), mFragments));
        // 设置缓存页面数
        vp.setOffscreenPageLimit(2);
    }

    @Override
    protected void initListener() {
        //radioGroup的点击事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                for (int i = 0; i < rbs.length; i++) {
                    if (rbs[i] != checkedId) continue;
                    //加载滑动
                    vp.setCurrentItem(i);
                }
            }
        });
        //ViewPager的点击事件 vp-rg互相监听:vp
        vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                rg.check(rbs[position]);
            }
        });
        //设置一个默认页
        rg.check(rbs[0]);
    }

    public void onBackPressed() {
        if (!mBackKeyPressed) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mBackKeyPressed = true;
            new Timer().schedule(new TimerTask() {//延时两秒,如果超出则清除第一次记录

                @Override
                public void run() {
                    mBackKeyPressed = false;
                }
            }, 2000);
        } else {
            finish();
        }
    }
}

adapter:

package com.example.wallpaper.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

public class PagerMainAdapter extends FragmentPagerAdapter {
    private final List frags;

    public PagerMainAdapter(FragmentManager fm, List frags) {
        super(fm);
        this.frags = frags;
    }

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

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

最后fragment:

package com.example.wallpaper.fragment;

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 com.example.wallpaper.R;

public class FragmentOne extends Fragment{

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

        return view;
    }

}
package com.example.wallpaper.fragment;

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 com.example.wallpaper.R;

public class FragmentTwo extends Fragment{

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

        return view;
    }
}
package com.example.wallpaper.fragment;

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 com.example.wallpaper.R;

public class FragmentThree extends Fragment {

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

        return view;
    }
}

​

还有coloe和drawable资源文件:

selector_tv_fen.xml



    
    

tebbar_one.xml



    
    

tebbar_two.xml



    
    

tebbar_three.xml



    
    

大致目录如下:

Android开发中常用框架(ViewPager+RadioButton+Fragment)_第2张图片

代码下载:https://download.csdn.net/download/juer2017/10866572

你可能感兴趣的:(Android)