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的布局文件代码


    

        
    

    

        
    

    

        
    


buttom文件代码



    

        

        
    

    

        

        
    

    

        

        
    

    

        

        
    


  至于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 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();
        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);
    }
}

你可能感兴趣的:(Android Tab界面的实现-ViewPager+View)