如何使用片段自定义标签?

时间:2014-08-19 14:36:55

标签: java android android-fragments android-tabs

我只是想将我的应用上的标签自定义为带选择器的图片或我需要开始的东西?这是我的代码。 现在我的应用程序是常规的黑色和蓝色标签,但我想让它自定义

我该怎么办?     这是我的代码:

A类

  public class ListFragment extends Fragment{

        basAdapterCustom adapter;
        ListView lv;
        ArrayList<Clock> list;
        private DbHandler hand;
        Context context;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.list_fragment, container, false);
            context = getActivity();

            Log.d(TAG, "onCreateView");

            hand = new DbHandler(context);
            list = new ArrayList<Clock>();

            lv = (ListView) v.findViewById(R.id.listOfShifts);
            adapter = new basAdapterCustom(list, getActivity());
            lv.setAdapter(adapter);
            refreshList();
            return v;
        }



        //like on "resume":
        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            // Make sure that we are currently visible
            if (this.isVisible()) {
                refreshList();
                if (!isVisibleToUser) {
                    // TODO stop
                }
            }
        }



        private void refreshList() {

            list = hand.getByWorkName();
            adapter = new basAdapterCustom(list,getActivity());

            lv.setAdapter(adapter);
        }

b级:

public class ClockFragment extends Fragment{


    DbHandler hand;


    Context context;
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.clock, container,false);

        context = getActivity();

        hand = new DbHandler(context);

        return v;
    }


    // ADD to .Db
    public void addToDb(View v){
        hand.add(new Clock(0, dateDay));    

    }


}

class mainActivity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener, OnPageChangeListener{

    public static final String TAG = "myClock";

    String[] tabMenu = {"FRAG A","FRAG B","FRAG C"};

    private ViewPager viewPager;
    private TabPagerAdapter mAdapter;
    private ActionBar actionBar;


    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);





        Log.d(MainActivity.TAG, "OnCreate = MainActivity (Pager");
        viewPager = (ViewPager) findViewById(R.id.pager );
        actionBar = getActionBar();
        mAdapter = new TabPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (String tabsNames : tabMenu) {
            actionBar.addTab(actionBar.newTab().setText(tabsNames).setTabListener(this));
        }
        viewPager.setOnPageChangeListener(this);

    }

    //  public void transDialog(){
    //      Dialog mDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    //  }





    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }




    @Override
    public void onPageScrollStateChanged(int arg0) {        
    }


    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }


    @Override
    public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);      
    }

}

PagetAdapter.class

public class TabPagerAdapter extends FragmentPagerAdapter {

    private static final String TAG = "myClock";


    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {
        Log.d(TAG, " CLASS : TabPagerAdapter");
        switch (index) {
        case 0:

            return new ListFragment();
        case 1:
            return new ClockFragment();
        case 2:
            return new SettingFragment();

        default:
            break;
        }       
        Log.d(TAG, " CLASS : TabPagerAdapter = "+index);
        return null;
    }


    @Override
    public int getCount() {
        return 3;
    }

}

1 个答案:

答案 0 :(得分:0)

for (String tabsNames : tabMenu) {
    actionBar.addTab(actionBar.newTab().setText(tabsNames).setTabListener(this));
}

将其更改为:

for (String tabsNames : tabMenu) {
    actionBar.addTab(actionBar.newTab().setText(tabsNames).setTabListener(this).setIcon(resources.getDrawable(<drawable_id>));
}

也许,你应该首先创建一个带有可绘制id的数组,然后在&#39;中用于&#39;循环使用.setIcon(resources.getDrawable(icons[i])

之类的id

如果您已将图标的drawables放入相应的文件夹,您可以给我他们的名字,我会告诉您完整的代码。