Android仿微信左右滑動點擊切換頁面和圖標
本文實例為大家分享了Android仿微信左右滑動點擊切換頁面和圖標的具體代碼,供大家參考,具體內(nèi)容如下
目標效果:

使用鼠標滑動屏幕或者點擊下邊的小圖標,可以更改頁面和圖標,因為沒有那么多素材所以只用了兩張圖片區(qū)分。
1.layout文件夾下新建top.xml頁面,作為頂部標題。
top.xml頁面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_gravity="center"
android:textStyle="bold" />
</LinearLayout>
2.新建bottom.xml頁面,作為底部導航。
bottom.xml頁面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#000000"
android:orientation="horizontal" >
<!-- ImageButton沒加android:clickable="false"時,點擊下方的ImageBuutton不會改變頁面,點擊TextView才會改變頁面,這是因為每個tab是一個LinearLayout,每個LinearLayout都有一個ImageButton,當點擊ImageButton位置時,點擊事件首先會到LinearLayout上,LinearLayout會去判斷,發(fā)現(xiàn)內(nèi)部有一個ImageButton可以解決點擊事件,所以就把點擊事件交給ImageButton,而ImageButton又沒有寫點擊事件,所以點擊事件就失效了。-->
<LinearLayout
android:id="@+id/tab_weixin"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/tab_weixin_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:clickable="false"
android:src="@drawable/search" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_friend"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/tab_friend_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:clickable="false"
android:src="@drawable/study" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朋友"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_tongxunlu"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/tab_tongxunlu_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:clickable="false"
android:src="@drawable/study" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通訊錄"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_set"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/tab_set_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:clickable="false"
android:src="@drawable/study" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="設置"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
這里注意ImageButton的clickable屬性,如果不設置false,那么鼠標點擊不起作用,只有點擊下邊的TextView才會跳轉頁面。
3.新建tab01.xml頁面,復制三個,只更改顯示文本,作為切換頁面。
tab01.xml頁面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Weixin Tab"
android:textSize="30sp"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
4.activity_main.xml頁面導入頂部和底部xml文件,并放置ViewPager。
activity_main.xml頁面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/top"/>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp">
</android.support.v4.view.ViewPager>
<include layout="@layout/bottom"/>
</LinearLayout>
5.因為ViewPager是在jar包里,添加該控件需要寫出路徑,當記不住的時候,按下Ctrl+Shift+t,彈出框里輸入“ViewPager”并選擇,顯示的頁面中就包含該控件的路徑。


6.新建pageAdapter.java,繼承PageAdapter,實現(xiàn)四個方法。
pageAdapter.java頁面:
package com.example.adapter;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class pageAdapter extends PagerAdapter {
private List<View> mViews;
public pageAdapter(List<View> mViews) {
this.mViews = mViews;
}
/**
* 重寫四個方法 boolean isViewFromObject(View arg0, Object arg1) int getCount()
* void destroyItem(ViewGroup container, int position,Object object) Object
* instantiateItem(ViewGroup container, int position)
*/
// 從當前container中刪除指定位置的view
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViews.get(position));
}
// 初始化view
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = mViews.get(position);
container.addView(view);
return view;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
// 返回要滑動的view個數(shù)
@Override
public int getCount() {
return mViews.size();
}
}
7.MainActivity.java頁面編寫點擊和滑動事件。
MainActivity.java頁面:
package com.example.studytab;
import java.util.ArrayList;
import java.util.List;
import com.example.adapter.pageAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private ViewPager mViewPager;
private List<View> mViews = new ArrayList<View>();
private pageAdapter mAdapter = new pageAdapter(mViews);
// Tab
private LinearLayout mTabWeixin, mTabFriend, mTabTongxunlu, mTabSet;
private ImageButton mWeixinImg, mFriendImg, mTongxunluImg, mSetImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題
setContentView(R.layout.activity_main);
initView();
initEvents();
}
//View的滑動事件
private void initEvents() {
mTabWeixin.setOnClickListener(this);
mTabFriend.setOnClickListener(this);
mTabTongxunlu.setOnClickListener(this);
mTabSet.setOnClickListener(this);
//滑動切換頁面
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
resetImg(); //將圖片全部默認為不選中
int currentItem = mViewPager.getCurrentItem();
switch (currentItem) {
case 0:
mWeixinImg.setImageResource(R.drawable.search);
break;
case 1:
mFriendImg.setImageResource(R.drawable.search);
break;
case 2:
mTongxunluImg.setImageResource(R.drawable.search);
break;
case 3:
mSetImg.setImageResource(R.drawable.search);
break;
default:
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
//實例化控件
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
// Tab
mTabWeixin = (LinearLayout) findViewById(R.id.tab_weixin);
mTabFriend = (LinearLayout) findViewById(R.id.tab_friend);
mTabTongxunlu = (LinearLayout) findViewById(R.id.tab_tongxunlu);
mTabSet = (LinearLayout) findViewById(R.id.tab_set);
// img
mWeixinImg = (ImageButton) findViewById(R.id.tab_weixin_img);
mFriendImg = (ImageButton) findViewById(R.id.tab_friend_img);
mTongxunluImg = (ImageButton) findViewById(R.id.tab_tongxunlu_img);
mSetImg = (ImageButton) findViewById(R.id.tab_set_img);
LayoutInflater mInflater = LayoutInflater.from(this);
View tab01 = mInflater.inflate(R.layout.tab01, null);
View tab02 = mInflater.inflate(R.layout.tab02, null);
View tab03 = mInflater.inflate(R.layout.tab03, null);
View tab04 = mInflater.inflate(R.layout.tab04, null);
mViews.add(tab01);
mViews.add(tab02);
mViews.add(tab03);
mViews.add(tab04);
mViewPager.setAdapter(mAdapter);
}
//ImageButton的點擊事件
@Override
public void onClick(View view) {
resetImg();
switch (view.getId()) {
case R.id.tab_weixin:
mViewPager.setCurrentItem(0);// 跳到第一個頁面
mWeixinImg.setImageResource(R.drawable.search); // 圖片變?yōu)檫x中
break;
case R.id.tab_friend:
mViewPager.setCurrentItem(1);
mFriendImg.setImageResource(R.drawable.search);
break;
case R.id.tab_tongxunlu:
mViewPager.setCurrentItem(2);
mTongxunluImg.setImageResource(R.drawable.search);
break;
case R.id.tab_set:
mViewPager.setCurrentItem(3);
mSetImg.setImageResource(R.drawable.search);
break;
default:
break;
}
}
// 將所有的圖片切換為未選中
private void resetImg() {
mWeixinImg.setImageResource(R.drawable.study);
mFriendImg.setImageResource(R.drawable.study);
mTongxunluImg.setImageResource(R.drawable.study);
mSetImg.setImageResource(R.drawable.study);
}
}
8.運行就可以顯示目標效果了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 深入解析Android中的setContentView加載布局原理
- 淺析Android Dialog中setContentView()方法
- Android開發(fā)中setContentView和inflate的區(qū)別分析
- Android開發(fā)微信小程序頁面的圖文教程
- Android 登錄頁面的實現(xiàn)代碼(密碼顯示隱藏、EditText 圖標切換、限制輸入長度)
- Android Studio使用recyclerview實現(xiàn)展開和折疊功能(在之前的微信頁面基礎之上)
- Android Webview的postUrl與loadUrl加載頁面實例
- Android通過ViewModel保存數(shù)據(jù)實現(xiàn)多頁面的數(shù)據(jù)共享功能
- Android仿淘寶訂單頁面效果
- Android使用setContentView實現(xiàn)頁面的轉換效果
相關文章
簡略分析Android的Retrofit應用開發(fā)框架源碼
這篇文章主要介紹了Android的Retrofit應用開發(fā)框架的源碼分析,作者對Volley和Retrofit兩個框架進行了一些對比,比較精彩,需要的朋友可以參考下2016-02-02
Android中Toolbar隨著ScrollView滑動透明度漸變效果實現(xiàn)
這篇文章主要介紹了Android中Toolbar隨著ScrollView滑動透明度漸變效果實現(xiàn),非常不錯,具有參考借鑒價值,需要的的朋友參考下2017-01-01
AndroidStudio升級4.1后啟動失敗Plugin問題解決
這篇文章主要介紹了AndroidStudio升級4.1后啟動失敗Plugin問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
Android編程實現(xiàn)輸入框動態(tài)自動提示功能
這篇文章主要介紹了Android編程實現(xiàn)輸入框動態(tài)自動提示功能,結合實例形式分析了AutoCompleteTextView相關使用技巧,需要的朋友可以參考下2017-03-03
Android利用BitMap獲得圖片像素數(shù)據(jù)的方法
這篇文章主要介紹了Android利用BitMap獲得圖片像素數(shù)據(jù)的方法,結合實例對比分析了Android獲取圖片像素數(shù)據(jù)的相關技巧,需要的朋友可以參考下2016-02-02

