Android Tab界面的实现-ViewPager+View

  这里,我将说下使用viewpager加上view来实现tab界面。其实这个并不陌生,这个就是我们在学习最初的viewpager的时候,viewpager的使用方式。
  基本思想就是:将多个xml布局文件转换为view对象,再加到Adapter上去。
  其中实现xml文件往view对象的转换的三种方式:

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

  在我这个事例中有一个小问题要注意的是:我在这里设置的是LineaLayout的监听事件,同时给ImageButton加一个属性:android:onclickable = "false"。这样能保证当用户点击textview或者imagebutton(凡是点击在LinearLayout上,都可以执行)。话不多说,直接贴代码

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="设置"
            android:textColor="#ffffff" />
    </LinearLayout>

</LinearLayout>

  至于view2,view3,view4代码与view1差不多,只是在text上的内容改变了。

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

/*
 * 既能滑动又能点击
 */
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);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容