FragmentTabHost的使用

来源:互联网 发布:java网上订餐系统 编辑:程序博客网 时间:2024/06/05 09:12

今天带给大家的是FragmentTabHost,更加加单快捷的实现了Fragment的切换页面等功能。

FragmentTabHost的精华在于那个Indicate(指示器),通过指示器来实现切换页面等功能。逻辑很简单的,我就直接上代码了。

首先我们需要知道我们的项目要求是什么,需要切换几个Fragment视图,展示下效果图。
MianActivity中的代码:
import android.os.Bundle;import android.support.annotation.NonNull;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TabHost;import android.widget.TextView;import com.example.fragmenttabhostdemo.Fragment.AFragment;import com.example.fragmenttabhostdemo.Fragment.BFragment;import com.example.fragmenttabhostdemo.Fragment.CFragment;public class MainActivity extends FragmentActivity {    private FragmentTabHost tabHost;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        // 1.初始化tabhost        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);        // 2.设置TabHost的参数 , 第一个参数:上下文 , 第二个:参数碎片管理者 , 第三个参数:要操作fragment应放的位置        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);        View view = getIndicatorView(R.string.chat,R.drawable.chat);        View view2 = getIndicatorView(R.string.contact,R.drawable.concatc);        View view3 = getIndicatorView(R.string.mine,R.drawable.mine);        // 3.设置下方Indicate(指示器)        TabHost.TabSpec tabSpec = tabHost.newTabSpec("a");//创建指示器“a”是这个指示器的一个tag(标记)任意写        tabSpec.setIndicator(view); // 设置tabHost的样式        Bundle bundle = new Bundle();        bundle.putInt("name", 1);        tabHost.addTab(tabSpec, AFragment.class, bundle);        Bundle bundle2 = new Bundle();        bundle.putInt("name", 2);        TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("b");        tabSpec2.setIndicator(view2);        tabHost.addTab(tabSpec2, BFragment.class, bundle2);        Bundle bundle3 = new Bundle();        bundle.putInt("name", 3);        TabHost.TabSpec tabSpec3 = tabHost.newTabSpec("c");        tabSpec3.setIndicator(view3);        tabHost.addTab(tabSpec3, CFragment.class, bundle3);    }    @NonNull    private View getIndicatorView(int textid , int imgid) {        View view = LayoutInflater.from(this).inflate(R.layout.bottom_icon, null);        TextView textView = (TextView) view.findViewById(R.id.bottom_tv);        ImageView imageView = (ImageView) view.findViewById(R.id.bottom_imgs);        textView.setText(textid);        imageView.setImageResource(imgid);        return view;    }    @Override    public void onStart() {        super.onStart();    }    @Override    public void onStop() {        super.onStop();    }}
我们需要几个Activity取接收MainActivity传递过来的指示器,因为共同的方法所以都写到一个基类里面了,这个就是面向对象思想了。
import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import com.example.fragmenttabhostdemo.R;public abstract  class BaseFragment extends Fragment {    public View view;    public TextView tView;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return initView();    }    // 因为每一都需要查找控件给控件设置内容,索性我就抽取出来,共同继承这个抽象方法    public  View initView(){        view = LayoutInflater.from(getContext()).inflate(R.layout.afragment,null);        tView = (TextView) view.findViewById(R.id.tv);        setting(tView);        return view;    }    public abstract  void setting(TextView tView);}
再写一个FragmentA,B.C我就不写了  原理是一样的
import android.os.Bundle;import android.support.annotation.Nullable;import android.util.Log;import android.widget.TextView;import static android.R.attr.name;public class AFragment extends BaseFragment {    private static final String TAG = "AFragment";        // 实现基类的抽象方法    @Override    public void setting(TextView textView){        textView.setText("我是A+Fragment");    }    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 获取bundle        Bundle bundle = getArguments();        bundle.getInt("name");        Log.e(TAG, "onCreate: "+name );    }


xml中就定义这个根据个人需求自己定义我就不写了。希望可以一起成长。



1 0