分别使用了ViewPager和Fragment来实现Tab。实现思路基本差不多 都是定义几个不同的布局文件 进行加载。分别最终结果都已经实现 但是还有些许不同:
1.View Pager实现的Tab可以直接重写左右滑动 Fragment没。
2.ViewPager所有的布局文件都在activity中进行初始化,List的声明 事件的处理均在activity中体现 造成代码过多,不易改善维护
3.Fragment管理自己的布局,每个页面布局中的元素+事件都由自己处理,activity起到一个调度的作用,activity代码少方便管理。PS:Fragment在不同屏幕分辨率适配有得天独厚的优势。
**码中出错:fragment引入包的时候一定要统一 否则运行时 不显示。现在fragment两个版本:普通fragment包和V4包。**
Activity代码:
package cc.myfragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Fragment;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements View.OnClickListener{
private Fragment mFragment;
private LinearLayout mLayoutweixin,mLayoutfriend,mLayoutaddress,mLayoutsetting;
private ImageButton weixin_img,friend_img,address_img,setting_img;
private Fragment tab01,tab02,tab03,tab04;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
InitView();
InitEvent();
setSelect(0);
}
private void resetImageBtn() {
weixin_img.setImageResource(R.drawable.tab_weixin_normal);
friend_img.setImageResource(R.drawable.tab_find_frd_normal);
address_img.setImageResource(R.drawable.tab_address_normal);
setting_img.setImageResource(R.drawable.tab_settings_normal);
}
private void InitEvent() {
mLayoutweixin.setOnClickListener(this);
mLayoutfriend.setOnClickListener(this);
mLayoutaddress.setOnClickListener(this);
mLayoutsetting.setOnClickListener(this);
}
private void InitView() {
mLayoutweixin=(LinearLayout)findViewById(R.id.tab_weixin);
mLayoutfriend=(LinearLayout)findViewById(R.id.tab_friend);
mLayoutaddress=(LinearLayout)findViewById(R.id.tab_address);
mLayoutsetting=(LinearLayout)findViewById(R.id.tab_setting);
weixin_img=(ImageButton)findViewById(R.id.tab_weixinimg);
friend_img=(ImageButton)findViewById(R.id.tab_friendimg);
address_img=(ImageButton)findViewById(R.id.tab_addressimg);
setting_img=(ImageButton)findViewById(R.id.tab_settingimg);
}
@Override
public void onClick(View view) {
resetImageBtn();
switch (view.getId()){
case R.id.tab_weixin:
setSelect(0);
weixin_img.setImageResource(R.drawable.tab_weixin_pressed);
break;
case R.id.tab_friend:
setSelect(1);
friend_img.setImageResource(R.drawable.tab_find_frd_pressed);
break;
case R.id.tab_address:
setSelect(2);
address_img.setImageResource(R.drawable.tab_address_pressed);
break;
case R.id.tab_setting:
setSelect(3);
setting_img.setImageResource(R.drawable.tab_settings_pressed);
break;
}
}
private void hideAllFragment(FragmentTransaction transaction) {
if(tab01!=null){
transaction.hide(tab01);
}
if(tab02!=null){
transaction.hide(tab02);
}
if(tab03!=null){
transaction.hide(tab03);
}
if(tab04!=null){
transaction.hide(tab04);
}
}
private void setSelect(int i){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideAllFragment(transaction);
switch (i){
case 0:
if(tab01==null)
{
tab01=new weixin_fragment();
transaction.add(R.id.fragment,tab01);
}else{
transaction.show(tab01);
}
break;
case 1:
if(tab02==null)
{
tab02=new friend_fragment();
transaction.add(R.id.fragment,tab02);
}else{
transaction.show(tab02);
}
break;
case 2:
if(tab03==null)
{
tab03=new address_fragment();
transaction.add(R.id.fragment,tab03);
}else{
transaction.show(tab03);
}
break;
case 3:
if(tab04==null)
{
tab04=new setting_fragment();
transaction.add(R.id.fragment,tab04);
}else{
transaction.show(tab04);
}
break;
}
transaction.commit();
}
}
Fragment页面代码:
package cc.myfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2016/8/15.
*/
public class weixin_fragment extends android.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tab01,container,false);
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="cc.myfragment.MainActivity">
<include layout="@layout/top"></include>
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"></FrameLayout>
<include layout="@layout/bottom"></include>
</LinearLayout>