本文实例为大家分享了ViewPager Fragment实现侧滑导航栏的具体代码,供大家参考,具体内容如下

本文主要整理和记录下

本来想用Gif图片,这里暂时就用图片代替下吧:

Activity:

package com.example.administrator.android006;
 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
 
import com.example.administrator.android006.Fragment.fragment1;
import com.example.administrator.android006.Fragment.fragment2;
import com.example.administrator.android006.Fragment.fragment3;
import com.example.administrator.android006.Fragment.fragment4;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends FragmentActivity implements View.OnClickListener {
 
    //顶部4个按钮
    private LinearLayout main_home_layout,main_msg_layout,main_pal_layout,main_me_layout;
    private ViewPager main_mViewPager;
    //ViewPager的适配器
    private FragmentPagerAdapter mAdapter;
    //4个Fragment碎片的集合
    private List<Fragment> mFragments = new ArrayList<>();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //初始化,加载碎片
        initView();
        initAdapter();
    }
 
    public void initAdapter(){
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }
 
            @Override
            public int getCount() {
                return mFragments.size();
            }
        };
        main_mViewPager.setAdapter(mAdapter);
        main_mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 
            }
 
            @Override
            public void onPageSelected(int position) {
                //重置ImageView的颜色
                resetImg();
                //设置选中时的图片
                switch (position) {
                    case 0:
                        ((ImageView) main_home_layout.findViewById(R.id.main_home_img))
                                .setImageResource(R.drawable.home_black);
                        break;
                    case 1:
                        ((ImageView) main_msg_layout.findViewById(R.id.main_msg_img))
                                .setImageResource(R.drawable.msg_black);
                        break;
                    case 2:
                        ((ImageView) main_pal_layout.findViewById(R.id.main_pal_img))
                                .setImageResource(R.drawable.pal_black);
                        break;
                    case 3:
                        ((ImageView) main_me_layout.findViewById(R.id.main_me_img))
                                .setImageResource(R.drawable.me_black);
                        break;
                }
            }
 
            @Override
            public void onPageScrollStateChanged(int state) {
 
            }
        });
    }
 
    //重置ImageView的图片
    protected void resetImg(){
        ((ImageView) main_home_layout.findViewById(R.id.main_home_img))
                .setImageResource(R.drawable.home_gray);
        ((ImageView) main_msg_layout.findViewById(R.id.main_msg_img))
                .setImageResource(R.drawable.msg_gray);
        ((ImageView) main_pal_layout.findViewById(R.id.main_pal_img))
                .setImageResource(R.drawable.pal_gray);
        ((ImageView) main_me_layout.findViewById(R.id.main_me_img))
                .setImageResource(R.drawable.me_gray);
    }
 
    public void initView(){
        main_home_layout = findViewById(R.id.main_home_layout);
        main_msg_layout = findViewById(R.id.main_msg_layout);
        main_pal_layout = findViewById(R.id.main_pal_layout);
        main_me_layout = findViewById(R.id.main_me_layout);
        main_mViewPager = findViewById(R.id.main_mViewPager);
 
        fragment1 vp_fr1 = new fragment1();
        fragment2 vp_fr2 = new fragment2();
        fragment3 vp_fr3 = new fragment3();
        fragment4 vp_fr4 = new fragment4();
        mFragments.add(vp_fr1);
        mFragments.add(vp_fr2);
        mFragments.add(vp_fr3);
        mFragments.add(vp_fr4);
        main_home_layout.setOnClickListener(this);
        main_msg_layout.setOnClickListener(this);
        main_pal_layout.setOnClickListener(this);
        main_me_layout.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //点击首页时,设置ViewPager的下标为0
            case R.id.main_home_layout:
                main_mViewPager.setCurrentItem(0);
                break;
            //点击消息时,设置ViewPager的下标为1
            case R.id.main_msg_layout:
                main_mViewPager.setCurrentItem(1);
                break;
            //点击好友时,设置ViewPager的下标为2
            case R.id.main_pal_layout:
                main_mViewPager.setCurrentItem(2);
                break;
            //点击我时,设置ViewPager的下标为3
            case R.id.main_me_layout:
                main_mViewPager.setCurrentItem(3);
                break;
        }
    }
}

.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        >
        <LinearLayout
            android:id="@ id/main_home_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@ id/main_home_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/home_black"
                android:scaleType="fitXY"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="首页"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@ id/main_msg_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@ id/main_msg_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/msg_gray"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="消息"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@ id/main_pal_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@ id/main_pal_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/pal_gray"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="好友"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@ id/main_me_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@ id/main_me_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/me_gray"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我"
                />
        </LinearLayout>
    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@ id/main_mViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
    </android.support.v4.view.ViewPager>
</LinearLayout>

这个是ViewPager中的其中一个Fragment:

public class fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1,container,false);
    }
}

其Fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="我是Fragment1"
        />
 
</android.support.constraint.ConstraintLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

ViewPager+Fragment实现侧滑导航栏的更多相关文章

  1. android – 尝试commitAllowingStateLoss时的FragmentManager NullPointerException

    上下文:我有一个带片段的活动和3个InnerFragments.当调用FragmentonDestroy()时,我想从FragmentManager中删除内部片段.onDestroy()的代码如下.问题:FragmentManager抛出NullPointerException,可能是在调用commitAllowingStateLoss()时.我不明白为什么.堆栈跟踪:解决方法FragmentMa

  2. android – 在listview中添加viewpager作为滚动标题

    我试图在列表标题中添加Viewpager(使用支持库4),但它没有显示任何内容.这是我的代码请帮忙.它将在列表标题中不作为项目,因此它不应该是一个问题.解决方法listadapter之后的listview页眉和页脚显示.如果你尝试setadapter,并且看不到viewpager.检查viewpager的宽度和高度.如果viewpager的宽度或高度值为0.在viewgroup中创建LienarL

  3. 从ViewPager Fragments中隐藏Android应用程序中的软键盘

    我有一个Android应用程序,其中包含一个包含2个片段的ViewPager.第一个片段包含EditText字段.当应用程序启动时,该字段立即获得焦点并启动软键盘.第二个片段仅包含一个列表.当我从片段1滑动到片段2时,我希望键盘能够消失.我尝试过的任何东西似乎都没有用.键盘不仅保持在视图中,还继续更新片段1的EditText字段.我想我要么使用不正确的代码来隐藏键盘或将其放在错误的位置.如果任何人都可以发布正确实现的示例,将不胜感激!

  4. android – Slider Menu片段中的可交换选项卡

    我已经通过引用thistutorial实现了导航抽屉,现在我想要做的是在片段内显示滑动标签.即当我点击导航抽屉中的一个项目时,让我们说第一个项目,它应显示该项目的滑动标签.如果item1是Events,当我点击它时,它应该显示滑动标签.但我面临以下问题:–>如何在片段内实现视图寻呼机?

  5. android – 如何在ViewPager中使用cursorLoader?

    解决方法我无法评论,所以我正在写一个答案..您有一个实现LoaderCallbacks的活动.加载数据时,您的活动会收到onLoadFinished回调.在此方法中,您有一个应该在ViewPager中显示的Cursor.要显示Cursor中的数据,请在适配器上调用swapCursor方法.因此,每次加载数据时都不要创建适配器.创建一次,然后只需调用swapCursor即可.此外,每次都找不到ViewPager–findViewById是一个繁重的操作,它应该在创建视图层次结构后执行.所以,你的onLoad

  6. android – FragmentPagerAdapter不会在方向更改时重新创建片段吗?

    可能是其他一些解决方案?这些碎片有什么问题?

  7. android – Horizo​​ntalScrollView还是Carrousel?

    我在SO上找到的最接近的例子是here.这正是我需要的,但我已经测试了给出的答案,但它对我不起作用……但问题中的图像正是我想要的.任何人都可以指导我这样做吗?编辑2:根据你给出的两个例子,你看过this和this吗?这就是你要找的东西吗?

  8. android – setRetainInstance(true)setCustomAnimations(…)=每个方向更改的动画?

    >为什么我仍然可以获得添加片段的动画?>其他配置发生变化时会发生什么?

  9. android – 来自Fragment的getActionBar和AppCompatLibrary

    我正在寻找使用AppCompatLibrary和API8从Fragment获取ActionBar实例的最简单方法.已经尝试过像但没有运气.解决方法试着施展它:

  10. android – onActivityCreated总是被调用?

    有人可以提供一些链接来做解释生命周期行为的文档吗?>究竟什么是Fragmentrestart()?>Android可以决定删除不可见的片段但是保留包含它们的活动吗?注1:我测试过,由于活动重新创建而添加了Fragment时调用了onActivityCreated,并且在活动完全启动并激活后手动添加片段时也是如此.注2:我正在使用23.3.0支持版本进行测试.某些行为是否有可能从以前的版本发生变化?我相信Android操作系统杀死唯一可管理的量子是一个过程.对于Fragment,您可以查看FragmentA

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部