android的Tab实现布局

来源:互联网 发布:建筑设计模型制作软件 编辑:程序博客网 时间:2024/06/04 19:56

方法一:

    ViewPager

   关键代码:package com.example.viewpage_buttom;


import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;


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.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;


public class MainActivity extends Activity {
ImageButton ib1, ib2, ib3, ib4;
LinearLayout ll1, ll2, ll3, ll4;
ViewPager myViewPager;
List<View> list = new ArrayList<View>();
private LayoutInflater inflater;
PagerAdapter pagerAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initData();


}


private void initData() {
inflater = LayoutInflater.from(this);
View view1 = inflater.inflate(R.layout.fir, null);
View view2 = inflater.inflate(R.layout.sec, null);
View view3 = inflater.inflate(R.layout.thir, null);
View view4 = inflater.inflate(R.layout.four, null);
list.add(view1);
list.add(view2);
list.add(view3);
list.add(view4);
pagerAdapter = new PagerAdapter() {


@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
View view = list.get(position);
container.addView(view);
return view;
}


@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
container.removeView(list.get(position));


}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
};
myViewPager.setAdapter(pagerAdapter);
}


private void initUI() {
ib1 = (ImageButton) findViewById(R.id.imageButton1);
ib2 = (ImageButton) findViewById(R.id.imageButton2);
ib3 = (ImageButton) findViewById(R.id.imageButton3);
ib4 = (ImageButton) findViewById(R.id.imageButton4);
ll1 = (LinearLayout) findViewById(R.id.ll1);
ll2 = (LinearLayout) findViewById(R.id.ll2);
ll3 = (LinearLayout) findViewById(R.id.ll3);
ll4 = (LinearLayout) findViewById(R.id.ll4);
myViewPager = (ViewPager) findViewById(R.id.myviewpagers);
}


}

方法二:fragment

package com.example.frametab;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.method.HideReturnsTransformationMethod;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;


public class MainActivity extends FragmentActivity implements
android.view.View.OnClickListener {


ImageButton ib1, ib2, ib3, ib4;
Tab1Activity fragment1;
Tab2Activity fragment2;
Tab3Activity fragment3;
Tab4Activity fragment4;
private FragmentManager fm;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib1 = (ImageButton) findViewById(R.id.imageButton1);
ib2 = (ImageButton) findViewById(R.id.imageButton2);
ib3 = (ImageButton) findViewById(R.id.imageButton3);
ib4 = (ImageButton) findViewById(R.id.imageButton4);
ib1.setOnClickListener(this);
ib2.setOnClickListener(this);
ib3.setOnClickListener(this);
ib4.setOnClickListener(this);
selectframement(1);


}


@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.imageButton1:
Toast.makeText(getApplicationContext(), "1", 0).show();
selectframement(1);
break;
case R.id.imageButton2:
selectframement(2);
break;
case R.id.imageButton3:
selectframement(3);
break;
case R.id.imageButton4:
selectframement(4);
break;


default:
break;
}


}


private void selectframement(int i) {
fm = getSupportFragmentManager();
FragmentTransaction tramscation = fm.beginTransaction();
hideFragment(tramscation);
switch (i) {
case 1:
if (fragment1 == null) {
fragment1 = new Tab1Activity();
tramscation.add(R.id.my_content, fragment1);
} else {
tramscation.show(fragment1);
}
break;
case 2:
if (fragment2 == null) {
fragment2 = new Tab2Activity();
tramscation.add(R.id.my_content, fragment2);
} else {
tramscation.show(fragment2);
}
break;
case 3:
if (fragment3 == null) {
fragment3 = new Tab3Activity();
tramscation.add(R.id.my_content, fragment3);
} else {
tramscation.show(fragment3);
}
break;
case 4:
if (fragment4 == null) {
fragment4 = new Tab4Activity();
tramscation.add(R.id.my_content, fragment4);
} else {
tramscation.show(fragment4);
}
break;


default:
break;
}
tramscation.commit();
}


private void hideFragment(FragmentTransaction tramscation) {
if (fragment1 != null) {
tramscation.hide(fragment1);
}
if (fragment2 != null) {
tramscation.hide(fragment2);
}
if (fragment3 != null) {
tramscation.hide(fragment3);
}
if (fragment4 != null) {
tramscation.hide(fragment4);
}
}


}

本文中的图片均为android自带

1 0
原创粉丝点击