如何使用ViewPager删除TabPageIndicator选项卡

时间:2013-03-13 10:58:47

标签: android android-fragments

我在删除标签 com.viewpagerindicator.TabPageIndicator 方面遇到了问题我在很多方面尝试过,但是没有用。

这是我的代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

</LinearLayout>

这是我的java代码:

package haris.dautovic.main;

import haris.dautovic.facebook.MainFragment;
import haris.dautovic.fregmenti.FacebookObavijesti;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.crypto.spec.DESKeySpec;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.TabPageIndicator;




public class SampleTabsWithIcons extends SlidingFragmentActivity  {
    private static final List<String> CONTENT = new ArrayList<String>();
    private static final List<Integer> ICONS = new ArrayList<Integer> () ;


    TabPageIndicator indicator;
    ViewPager pager;
    FragmentStatePagerAdapter adapter;
    ListView meni ;
    ArrayList<MenuItem> lista;


    Boolean prviObrisan=false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CONTENT.add("Facebook");
        CONTENT.add("Twitter");
        CONTENT.add("Google+");
        CONTENT.add("Youtube");
        CONTENT.add("Povezivanje");

    ICONS.add(R.drawable.perm_group_calendar);
    ICONS.add(R.drawable.perm_group_camera);
    ICONS.add( R.drawable.perm_group_device_alarms);
    ICONS.add( R.drawable.perm_group_location);
    ICONS.add( R.drawable.perm_group_location);

        setContentView(R.layout.simple_tabs);
       setBehindContentView(R.layout.menu);

       meni  =(ListView)findViewById(R.id.listViewMenu2); 

       lista = new ArrayList<MenuItem> (); 

       lista.add(new MenuItem("Chat", R.drawable.chatblank64x64));
       lista.add(new MenuItem("Pronađi prijatelja", R.drawable.friendfinder64x64));
       lista.add(new MenuItem("Podijeli informacije", R.drawable.share64x64));
       lista.add(new MenuItem("Moj Profil", R.drawable.profile64x64));
       lista.add(new MenuItem("Postavke", R.drawable.postavke64x64));
       lista.add(new MenuItem("Povezivanje", R.drawable.postavke64x64));


         meni.setAdapter(new GlavniMeniAdapter(this,lista,getAssets()));

        getSlidingMenu().setBehindOffset(200);
         adapter = new GoogleMusicAdapter(getSupportFragmentManager());

         pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);

        indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);

       // PostaviSlidingMenuZaKonekcije();

        System.out.println("on create");


    }

    private void PostaviSlidingMenuZaKonekcije (){

         SlidingMenu menuKonekcija = new SlidingMenu (this); 

         menuKonekcija.setMode(SlidingMenu.RIGHT);
         menuKonekcija.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
         menuKonekcija.setBehindOffset(150);
         menuKonekcija.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
         menuKonekcija.setMenu(R.layout.uvezivanjedrugihmreza);

    }



    HashMap<Integer,Fragment>  map =new HashMap<Integer, Fragment>();


    public final ArrayList<Fragment> fregmenti = new ArrayList<Fragment>();


   public final class GoogleMusicAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter {




       public  void destroyAllItem() {
           int mPosition = pager.getCurrentItem();
           int mPositionMax = pager.getCurrentItem()+1;
           if (CONTENT.size() > 0 && mPosition < CONTENT.size()) {
               if (mPosition > 0) {
                   mPosition--;
               }

               for (int i = mPosition; i < mPositionMax; i++) {
                   try {
                       Object objectobject = this.instantiateItem(pager,Integer.valueOf(CONTENT.get(i))  );
                       if (objectobject != null)
                           destroyItem(pager,Integer.valueOf( CONTENT.get(i)), objectobject);
                   } catch (Exception e) {

                   }
               }
           }
       }




       public  GoogleMusicAdapter(FragmentManager fm) {

            super(fm);


            meni.setOnItemClickListener(new AdapterView.OnItemClickListener (){

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {


                    LinearLayout parent =(LinearLayout)arg1;

                   TextView naziv =(TextView)parent.getChildAt(1);


                    if (naziv.getText().equals("Chat")) {



                  //  adapter.notify();

                    }
                    else 
                    {
                         if (naziv.getText().equals("Pronađi prijatelja")) {



                             System.out.println("Pronađi prijatelja");
                            }
                            else 
                            {
                                 if (naziv.getText().equals("Podijeli informacije")) {

                                     System.out.println("Podijeli informacije");
                                    }
                                    else 
                                    {
                                         if (naziv.getText().equals("Moj Profil")) {

                                             System.out.println("Moj Profil");


//                                              CONTENT.remove(0);
//                                              ICONS.remove(0);
//                                              adapter.notifyDataSetChanged();


                                            }
                                            else 
                                            {
                                                 if (naziv.getText().equals("Postavke")) {




                                                    }else {

                                                        if (naziv.getText().equals("Povezivanje")){
                                                             pager.setCurrentItem(4);

                                                        }


                                                    }


                                            }

                                    }

                            }
                    }



                }


             });
        }



       @Override
       public void destroyItem(ViewGroup container, int position, Object object) {

           super.destroyItem(container, position, object);
           // map.remove(position);

//         if (position >= getCount()) {
//             FragmentManager manager = ((Fragment) object).getFragmentManager();
//             FragmentTransaction trans = manager.beginTransaction();
//             trans.remove((Fragment) object);
//             trans.commit();
//         }
       }

        @Override
        public Fragment getItem(int position) {

            if(position==4)// uvazivanje mreza
            {



                haris.dautovic.facebook.MainFragment f = new MainFragment();

                map.put(position,f);
                return f;


            }else
            {
                if (position==0)//facebook obavijesti
                {

                    FacebookObavijesti fb_obav = new FacebookObavijesti();
                    map.put(position, fb_obav);
                    return fb_obav;
                }
            }


            haris.dautovic.facebook.MainFragment f = new MainFragment();
             map.put(position,f);
              return f;         

        }

        @Override
        public CharSequence getPageTitle(int position) {

            return CONTENT.get(position);
        }

        @Override public int getIconResId(int index) {


          return ICONS.get(index);
        }

      @Override
        public int getCount() {


          return CONTENT.size();
        }




//  
//      @Override
//      public int getItemPosition(Object object){
//        super.getItemPosition(object);
//      
//          return adapter.POSITION_NONE;
//      }
//      


    }


}

2 个答案:

答案 0 :(得分:0)

这是解决方案:

删除标签的功能:

     public void RemoveTab(Integer position) {

      CONTENT.remove(position);
      ICONS.remove(position);

      adapter = new GoogleMusicAdapter(getSupportFragmentManager());
      pager.setAdapter(adapter);
      indicator = (TabPageIndicator) findViewById(R.id.indicator);
      indicator.setViewPager(pager);

      indicator.notifyDataSetChanged();
      adapter.notifyDataSetChanged();
}

和@Override getItem功能:

@Override
public Fragment getItem(int position) {

    if (CONTENT.get(position).equals("Connection")) {

        MainFragment f = new MainFragment();
        return f;

    } else {
        if (CONTENT.get(position).equals("Facebook")) {

            FacebookNews fb_news = new FacebookNews();
            return fb_news;
        }
    }

    return haris.dautovic.main.BaseFragment.newInstance(CONTENT.get(position), position);

}

答案 1 :(得分:-1)

试用以下XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:layout_height="0dp"
        android:layout_width="fill_parent"
        android:visibility="gone" />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

</LinearLayout>