ViewPager+Fragment實(shí)現(xiàn)側(cè)滑導(dǎo)航欄
本文實(shí)例為大家分享了ViewPager+Fragment實(shí)現(xiàn)側(cè)滑導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
本文主要整理和記錄下
本來(lái)想用Gif圖片,這里暫時(shí)就用圖片代替下吧:

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個(gè)按鈕
private LinearLayout main_home_layout,main_msg_layout,main_pal_layout,main_me_layout;
private ViewPager main_mViewPager;
//ViewPager的適配器
private FragmentPagerAdapter mAdapter;
//4個(gè)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();
//設(shè)置選中時(shí)的圖片
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()) {
//點(diǎn)擊首頁(yè)時(shí),設(shè)置ViewPager的下標(biāo)為0
case R.id.main_home_layout:
main_mViewPager.setCurrentItem(0);
break;
//點(diǎn)擊消息時(shí),設(shè)置ViewPager的下標(biāo)為1
case R.id.main_msg_layout:
main_mViewPager.setCurrentItem(1);
break;
//點(diǎn)擊好友時(shí),設(shè)置ViewPager的下標(biāo)為2
case R.id.main_pal_layout:
main_mViewPager.setCurrentItem(2);
break;
//點(diǎn)擊我時(shí),設(shè)置ViewPager的下標(biāo)為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="首頁(yè)"
/>
</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>
這個(gè)是ViewPager中的其中一個(gè)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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android入門之彈出式對(duì)話框的實(shí)現(xiàn)
Android Studio里有一種Dialog叫PopWindow,它是一種“可阻塞式Dialog”,即彈出后除非你給它一個(gè)“動(dòng)作”否則就一直顯示在那。本文就將實(shí)現(xiàn)這樣的彈出式對(duì)話框,感興趣的可以了解一下2022-11-11
android開(kāi)發(fā)教程之判斷是手機(jī)還是平板的方法
判斷是平板還是手機(jī),通過(guò)很多的方式都可以實(shí)現(xiàn),如:設(shè)備尺寸、DPI、版本號(hào)、是否具備電話功能等,不過(guò)有些沒(méi)有那么的精準(zhǔn),這里分享一個(gè)比較簡(jiǎn)潔的方法2014-04-04
android用java動(dòng)態(tài)增添刪除修改布局
這篇文章主要介紹了android用java動(dòng)態(tài)增添刪除修改布局,感興趣的小伙伴們可以參考一下2016-03-03
android的ListView點(diǎn)擊item使item展開(kāi)的做法的實(shí)現(xiàn)代碼
這篇文章主要介紹了android的ListView點(diǎn)擊item使item展開(kāi)的做法的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
詳解Android數(shù)據(jù)存儲(chǔ)之SQLCipher數(shù)據(jù)庫(kù)加密
對(duì)于已經(jīng)ROOT的手機(jī)來(lái)說(shuō)的沒(méi)有任何安全性可以,一旦被利用將會(huì)導(dǎo)致數(shù)據(jù)庫(kù)數(shù)據(jù)的泄漏,本篇文章主要介紹了Android數(shù)據(jù)存儲(chǔ)之SQLCipher數(shù)據(jù)庫(kù)加密,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
Android ViewPagerIndicator詳解及實(shí)例代碼
這篇文章主要介紹了Android ViewPagerIndicator詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05

