仿微信主界面布局

package com.example.administrator.qq;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener{

    private ViewPager idViewpager;
    private LinearLayout llTabWeixn;
    private ImageButton ibTabweixinImg;
    private LinearLayout llTabFrd;
    private ImageButton ibTabwexinFrd;
    private LinearLayout llTabAddress;
    private ImageButton ibTabaddressImg;
    private LinearLayout llTabWeixnSet;
    private ImageButton ibTabsetImg;

    private List mView=new ArrayList();
    private PagerAdapter mMyPagerAdapter;

    //TAB

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();

    }

    private void initView(){

        idViewpager = (ViewPager) findViewById(R.id.id_viewpager);
        llTabWeixn = (LinearLayout) findViewById(R.id.ll_tab_weixn);
        ibTabweixinImg = (ImageButton) findViewById(R.id.ib_tabweixin_img);
        llTabFrd = (LinearLayout) findViewById(R.id.ll_tab_frd);
        ibTabwexinFrd = (ImageButton) findViewById(R.id.ib_tabwexin_frd);
        llTabAddress = (LinearLayout) findViewById(R.id.ll_tab_address);
        ibTabaddressImg = (ImageButton) findViewById(R.id.ib_tabaddress_img);
        llTabWeixnSet = (LinearLayout) findViewById(R.id.ll_tab_weixn_set);
        ibTabsetImg = (ImageButton) findViewById(R.id.ib_tabset_img);

        LayoutInflater mLayoutInflater=LayoutInflater.from(this);

        View tabOne=mLayoutInflater.inflate(R.layout.one,null);
        View tabTwo=mLayoutInflater.inflate(R.layout.two,null);
        View tabThree=mLayoutInflater.inflate(R.layout.three,null);
        View tabFour=mLayoutInflater.inflate(R.layout.four,null);

        mView.add(tabOne);
        mView.add(tabTwo);
        mView.add(tabThree);
        mView.add(tabFour);

        mMyPagerAdapter=new PagerAdapter() {

            @Override
            public int getCount() {

                return mView.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {

                return view==object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {

                container.removeView(mView.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view=mView.get(position);
                container.addView(view);
                return view;
            }
        };
        idViewpager.setAdapter(mMyPagerAdapter);
    }

    private void initEvent(){

        ibTabweixinImg.setOnClickListener(this);
        ibTabwexinFrd.setOnClickListener(this);
        ibTabaddressImg.setOnClickListener(this);
        ibTabsetImg.setOnClickListener(this);

        idViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                int currentItem=idViewpager.getCurrentItem();
                resetImg();
                switch(currentItem){
                    case 0:
                        ibTabweixinImg.setImageResource(R.drawable.tab_weixin_pressed);
                        break;
                    case 1:
                        ibTabwexinFrd.setImageResource(R.drawable.tab_find_frd_pressed);
                        break;
                    case 2:
                        ibTabaddressImg.setImageResource(R.drawable.tab_address_pressed);
                        break;
                    case 3:
                        ibTabsetImg.setImageResource(R.drawable.tab_settings_pressed);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        resetImg();
        switch (v.getId()){
            case R.id.ib_tabweixin_img:
               idViewpager.setCurrentItem(0);
                ibTabweixinImg.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case R.id.ib_tabwexin_frd:
                idViewpager.setCurrentItem(1);
                ibTabwexinFrd.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case R.id.ib_tabaddress_img:
                idViewpager.setCurrentItem(2);
                ibTabaddressImg.setImageResource(R.drawable.tab_address_pressed);
                break;
            case R.id.ib_tabset_img:
                idViewpager.setCurrentItem(3);
                ibTabsetImg.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
    }
    /**
     * 将图片切换为普通颜色
     *
     */
    private void resetImg(){
        ibTabweixinImg.setImageResource(R.drawable.tab_weixin_normal);
        ibTabwexinFrd.setImageResource(R.drawable.tab_find_frd_normal);
        ibTabaddressImg.setImageResource(R.drawable.tab_address_normal);
        ibTabsetImg.setImageResource(R.drawable.tab_settings_normal);
    }
}

你可能感兴趣的:(Android,界面)