Android Tab界面的實(shí)現(xiàn)-ViewPager+View

??這里,我將說下使用viewpager加上view來實(shí)現(xiàn)tab界面。其實(shí)這個并不陌生,這個就是我們在學(xué)習(xí)最初的viewpager的時候,viewpager的使用方式。
??基本思想就是:將多個xml布局文件轉(zhuǎn)換為view對象,再加到Adapter上去。
??其中實(shí)現(xiàn)xml文件往view對象的轉(zhuǎn)換的三種方式:

???1.LayoutInflate Lf = getLayoutInflate.from(this)
???2.View view = Lf.inflate(resource, root)
???3.View view = View.inflate(Context, resource, root)

??在我這個事例中有一個小問題要注意的是:我在這里設(shè)置的是LineaLayout的監(jiān)聽事件,同時給ImageButton加一個屬性:android:onclickable = "false"。這樣能保證當(dāng)用戶點(diǎn)擊textview或者imagebutton(凡是點(diǎn)擊在LinearLayout上,都可以執(zhí)行)。話不多說,直接貼代碼

MainActivity的布局文件代碼
<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"
    tools:context="com.example.android_tab1.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include layout="@layout/top" />
    </LinearLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp" >

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include layout="@layout/bottom" />
    </LinearLayout>

</LinearLayout>
buttom文件代碼
<?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="@drawable/bottom_bar"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/linear_weixin"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imagebutton_weixin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
            android:src="@drawable/tab_weixin_pressed" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="微信"
            android:clickable="false"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/linear_friend"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
             android:id="@+id/imagebutton_friend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
            android:src="@drawable/tab_find_frd_normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="朋友"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_address"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imagebutton_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
            android:src="@drawable/tab_address_normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通訊錄"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_setting"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imagebutton_setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
            android:src="@drawable/tab_settings_normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="設(shè)置"
            android:textColor="#ffffff" />
    </LinearLayout>

</LinearLayout>

??至于view2,view3,view4代碼與view1差不多,只是在text上的內(nèi)容改變了。

MainActivity代碼
package com.example.android_tab1;
import java.util.ArrayList;
import java.util.List;

/*
 * 既能滑動又能點(diǎn)擊
 */
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener{
    private ViewPager viewpager = null;
    private List<View> list = null;
    private ImageButton imagebutton_weixin = null;
    private ImageButton imagebutton_friend = null;
    private ImageButton imagebutton_address = null;
    private ImageButton imagebutton_setting = null;
    
    private LinearLayout linear_weixin = null;
    private LinearLayout linear_friend = null;
    private LinearLayout linear_address = null;
    private LinearLayout linear_setting = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initview();
    }
    private void initview()
    {
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        
        imagebutton_weixin = (ImageButton) findViewById(R.id.imagebutton_weixin);
        imagebutton_friend = (ImageButton) findViewById(R.id.imagebutton_friend);
        imagebutton_address = (ImageButton) findViewById(R.id.imagebutton_address);
        imagebutton_setting = (ImageButton) findViewById(R.id.imagebutton_setting);
        
        linear_weixin = (LinearLayout) findViewById(R.id.linear_weixin);
        linear_friend = (LinearLayout) findViewById(R.id.linear_friend);
        linear_address = (LinearLayout) findViewById(R.id.linear_address);
        linear_setting = (LinearLayout) findViewById(R.id.linear_setting);
        list = new ArrayList<View>();
        LayoutInflater lf = LayoutInflater.from(this);
        View view1 = lf.inflate(R.layout.view1, null);
        View view2 = lf.inflate(R.layout.view2, null);
        View view3 = lf.inflate(R.layout.view3, null);
        View view4 = lf.inflate(R.layout.view4, null);
        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(view4);
        Myadapter myadapter = new Myadapter(list);
        viewpager.setAdapter(myadapter);
        viewpager.setOnPageChangeListener(new OnPageChangeListener() {
            public void onPageSelected(int arg0) {
                resetimage();
                switch(arg0)
                {
                    case 0:
                    {
                        imagebutton_weixin.setImageResource(R.drawable.tab_weixin_pressed);
                        break;
                    }
                    case 1:
                    {
                        imagebutton_friend.setImageResource(R.drawable.tab_find_frd_pressed);
                        break;
                    }
                    case 2:
                    {
                        imagebutton_address.setImageResource(R.drawable.tab_address_pressed);
                        break;
                    }
                    case 3:
                    {
                        imagebutton_setting.setImageResource(R.drawable.tab_settings_pressed);
                        break;
                    }
                    
                }
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0) {
                
            }
        });
        Log.i("main", "1");
        linear_weixin.setOnClickListener(this);
        Log.i("main", "2");
        linear_friend.setOnClickListener(this);
        linear_address.setOnClickListener(this);
        linear_setting.setOnClickListener(this);
    }
    public void onClick(View v) {
        resetimage();
        switch(v.getId())
        {
        
            case R.id.linear_weixin:
            {
                Log.i("main", "3");
                viewpager.setCurrentItem(0);
                Log.i("main", "4");
                imagebutton_weixin.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            }
            case R.id.linear_friend:
            {
                viewpager.setCurrentItem(1);
                imagebutton_friend.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            }
            case R.id.linear_address:
            {
                viewpager.setCurrentItem(2);
                imagebutton_address.setImageResource(R.drawable.tab_address_pressed);
                break;
            }
            case R.id.linear_setting:
            {
                viewpager.setCurrentItem(3);
                imagebutton_setting.setImageResource(R.drawable.tab_settings_pressed);
                break;
            }
        }
    }
    private void resetimage()
    {
        imagebutton_weixin.setImageResource(R.drawable.tab_weixin_normal);
        imagebutton_friend.setImageResource(R.drawable.tab_find_frd_normal);
        imagebutton_address.setImageResource(R.drawable.tab_address_normal);
        imagebutton_setting.setImageResource(R.drawable.tab_settings_normal);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容