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 滑動(dòng)小圓點(diǎn)ViewPager的兩種設(shè)置方法詳解流程
Viewpager,視圖翻頁(yè)工具,提供了多頁(yè)面切換的效果。Android 3.0后引入的一個(gè)UI控件,位于v4包中。低版本使用需要導(dǎo)入v4包,現(xiàn)在我們一般不再兼容3.0及以下版本,另外使用Android studio開(kāi)發(fā),默認(rèn)導(dǎo)入v7包,v7包含了v4,所以不用導(dǎo)包,越來(lái)越方便了2021-11-11
Rocksdb?Memtable數(shù)據(jù)結(jié)構(gòu)源碼解析
這篇文章主要為大家介紹了Rocksdb?Memtable數(shù)據(jù)結(jié)構(gòu)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android PopupWindow使用方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析(上)
這篇文章主要介紹了Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析,具有很好的參考價(jià)值,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android編程滑動(dòng)效果之Gallery+GridView實(shí)現(xiàn)圖片預(yù)覽功能(附demo源碼下載)
這篇文章主要介紹了Android編程滑動(dòng)效果之Gallery+GridView實(shí)現(xiàn)圖片預(yù)覽功能,結(jié)合實(shí)例形式分析了Android通過(guò)GridView和Gallery兩個(gè)控件模仿Gallery圖像集圖片預(yù)覽功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02
Android中通過(guò)view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法
這篇文章主要介紹了Android中通過(guò)view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法,本文方法相對(duì)簡(jiǎn)單,容易理解,需要的朋友可以參考下2014-09-09
Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁(yè)面效果
本文通過(guò)實(shí)例代碼較詳細(xì)的給大家介紹了Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁(yè)面效果,需要的朋友可以參考下2018-06-06
Android應(yīng)用程序窗口(Activity)窗口對(duì)象(Window)創(chuàng)建指南
本文將詳細(xì)介紹Android應(yīng)用程序窗口(Activity)的窗口對(duì)象(Window)的創(chuàng)建過(guò)程,需要了解的朋友可以參考下2012-12-12
Android通過(guò)SharedPreferences實(shí)現(xiàn)自動(dòng)登錄記住用戶名和密碼功能
最近使用SharedPreferences實(shí)現(xiàn)了一個(gè)android自動(dòng)登錄功能,特此分享到腳本之家平臺(tái)供大家參考2017-07-07

