Android仿QQ滑动类Tab 和 底部TabHost 的讲解

效果图:

 

MainActivity.java

package com.android.qq.activity;

import android.app.ActivityGroup;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends ActivityGroup {

	private RelativeLayout layout;
	
	private RelativeLayout layout1;
	private RelativeLayout layout2;
	private RelativeLayout layout3;
	private RelativeLayout layout4;
	
	private RelativeLayout bodylayout;
	
	private ImageView tab1;
	private ImageView tab2;
	private ImageView tab3;
	private ImageView tab4;
	
	private ImageView first;
	private int current = 1; //默认选中第一个,可以动态的改变此参数值
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initUI();
	}
	
	private void initUI(){
		layout = (RelativeLayout) findViewById(R.id.root);
		
		layout1 = (RelativeLayout) findViewById(R.id.layout1);
		layout2 = (RelativeLayout) findViewById(R.id.layout2);
		layout3 = (RelativeLayout) findViewById(R.id.layout3);
		bodylayout = (RelativeLayout) findViewById(R.id.bodylayout);
		
		tab1 = (ImageView) findViewById(R.id.tab1);
		tab1.setOnClickListener(onClickListener);
		tab2 = (ImageView) findViewById(R.id.tab2);
		tab2.setOnClickListener(onClickListener);
		tab3 = (ImageView) findViewById(R.id.tab3);
		tab3.setOnClickListener(onClickListener);
		tab4 = (ImageView) findViewById(R.id.tab4);
		tab4.setOnClickListener(onClickListener);
		
		RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		rl.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		first = new ImageView(this);
		first.setTag("first");
		first.setImageResource(R.drawable.topbar_select);
		
		// 默认选中项
		switch (current) {
		case 1:
			layout1.addView(first, rl);
			current = R.id.tab1;
			break;
		case 2:
			layout2.addView(first, rl);
			current = R.id.tab2;
			break;
		case 3:
			layout3.addView(first, rl);
			current = R.id.tab3;
			break;
		case 4:
			layout4.addView(first, rl);
			current = R.id.tab4;
			break;
		default:
			break;
		}
		View view = getLocalActivityManager().startActivity("index",
				new Intent(MainActivity.this, Tab1.class))
				.getDecorView();
		bodylayout.addView(view);
		
	}

	private boolean isAdd = false; // 是否添加过 top_select
	private int select_width; // top_select_width
	private int select_height; // top_select_height
	private int firstLeft; // 第一次添加后的左边距*****
	private int startLeft; // 起始左边距
	
	// 添加一个view,移除一个view
	private void replace() {
		switch (current) {
		case R.id.tab1:
			changeTop(layout1);
			break;
		case R.id.tab2:
			changeTop(layout2);
			break;
		case R.id.tab3:
			changeTop(layout3);
			break;
		case R.id.tab4:
			changeTop(layout4);
			break;
		default:
			break;
		}
	}
	
	private void changeTop(RelativeLayout relativeLayout){
		ImageView old = (ImageView) relativeLayout.findViewWithTag("first");;
		select_width = old.getWidth();
		select_height = old.getHeight();
		
		RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(select_width, select_height);
		rl.leftMargin = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft();
		rl.topMargin = old.getTop() + ((RelativeLayout)old.getParent()).getTop();
		
		// 获取起始位置
		firstLeft = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft();
		
		ImageView iv = new ImageView(this);
		iv.setTag("move");
		iv.setImageResource(R.drawable.topbar_select);
		
		layout.addView(iv , rl);
		relativeLayout.removeView(old);
	}
	
	private OnClickListener onClickListener = new OnClickListener(){
		public void onClick(View v) {
			if(!isAdd){
				replace(); // 初次使用移除old 添加新的top_select为RelativeLayout所使用
				isAdd = true;
			}
			
			ImageView top_select = (ImageView) layout.findViewWithTag("move");
			int tabLeft;
			int endLeft = 0;
			
			boolean run = false;

			switch (v.getId()) {
			case R.id.tab1:
				if (current != R.id.tab1) {
					// 中心位置
					tabLeft = ((RelativeLayout) tab1.getParent()).getLeft() + tab1.getLeft() + tab1.getWidth() / 2;
					// 最终位置
					endLeft = tabLeft - select_width / 2;
					current = R.id.tab1;
					run = true;
					bodylayout.removeAllViews();
					View view = getLocalActivityManager().startActivity("index",
							new Intent(MainActivity.this, Tab1.class))
							.getDecorView();
					bodylayout.addView(view);
				}
				break;
			case R.id.tab2:
				if (current != R.id.tab2) {
					tabLeft = ((RelativeLayout) tab2.getParent()).getLeft() + tab2.getLeft() + tab2.getWidth() / 2;
					endLeft = tabLeft - select_width / 2;
					current = R.id.tab2;
					run = true;
					bodylayout.removeAllViews();
					View view = getLocalActivityManager().startActivity("index",
							new Intent(MainActivity.this, Tab2.class))
							.getDecorView();
					bodylayout.addView(view);
				}
				break;
			case R.id.tab3:
				if (current != R.id.tab3) {
					tabLeft = ((RelativeLayout) tab3.getParent()).getLeft() + tab3.getLeft() + tab3.getWidth() / 2;
					endLeft = tabLeft - select_width/2;
					current = R.id.tab3;
					run = true;
					bodylayout.removeAllViews();
					View view = getLocalActivityManager().startActivity("index",
							new Intent(MainActivity.this, Tab3.class))
							.getDecorView();
					bodylayout.addView(view);
				}
				break;
			case R.id.tab4:
				if (current != R.id.tab4) {
					tabLeft = ((RelativeLayout) tab4.getParent()).getLeft() + tab4.getLeft() + tab4.getWidth() / 2;
					endLeft = tabLeft - select_width/2;
					current = R.id.tab4;
					run = true;
					bodylayout.removeAllViews();
					View view = getLocalActivityManager().startActivity("index",
							new Intent(MainActivity.this, Tab4.class))
							.getDecorView();
					bodylayout.addView(view);
				}
				break;
			default:
				break;
			}
			
			if(run){
				TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft - firstLeft, 0, 0);
				startLeft = endLeft - firstLeft; // 重新设定起始位置
				animation.setDuration(400);
				animation.setFillAfter(true);
				top_select.bringToFront();
				top_select.startAnimation(animation);
			}
			
		}

	};
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // This is our one standard application action -- inserting a
        // new note into the list.
        menu.add(0, 0, 0, R.string.app_name)
                .setShortcut('4', 'a')
                .setIcon(android.R.drawable.ic_menu_add);

        // Generate any additional actions that can be performed on the
        // overall list.  In a normal install, there are no additional
        // actions found here, but this allows other applications to extend
        // our menu with their own actions.
        Intent intent = new Intent(null, getIntent().getData());
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
                new ComponentName(this, MainActivity.class), null, intent, 0, null);

        return true;
    }

}


 

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/default_bg">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="6.0"
            android:orientation="horizontal" >

            <RelativeLayout
                android:id="@+id/layout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <ImageView
                    android:id="@+id/tab1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/icon_img1_nor" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layout2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <ImageView
                    android:id="@+id/tab2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/icon_img2_nor" />
            </RelativeLayout>
            
            <RelativeLayout
                android:id="@+id/layout3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <ImageView
                    android:id="@+id/tab3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/icon_img3_nor" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/layout4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <ImageView
                    android:id="@+id/tab4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/icon_img3_nor" />
            </RelativeLayout>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/bodylayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:background="#ffffff"
            android:gravity="center"
            android:orientation="horizontal" >
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>


 

 

Tab1.java

package com.android.qq.activity;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;


public class Tab1 extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab1);
	}
}


 

tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/a1" >
    </ImageView>

</LinearLayout>


 

 

资源下载地址: http://download.csdn.net/detail/niejing654092427/4844539

 

 

 

效果图:

 

资源下载地址: http://download.csdn.net/detail/niejing654092427/4844574

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值