13

我正在我的应用程序中实现可滑动的标签。我为此实现了一个演示。下面是我的代码,

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />
        </FrameLayout>

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</TabHost>

MainActivity.java

public class MainActivity extends Activity implements OnTabChangeListener, OnPageChangeListener{  
    private TabHost host;  
    private ViewPager pager;  
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.activity_main);  
   host = (TabHost)findViewById(android.R.id.tabhost);  
   pager = (ViewPager) findViewById(R.id.pager);  

   host.setup();  
   TabSpec spec = host.newTabSpec("tab1");  
   spec.setContent(R.id.tab1);  
   spec.setIndicator("Check In");   
   host.addTab(spec);  

   spec = host.newTabSpec("tab2");  
   spec.setContent(R.id.tab2);  
   spec.setIndicator("Buddies");  
   host.addTab(spec);  

   spec = host.newTabSpec("tab3");  
   spec.setContent(R.id.tab3);  
   spec.setIndicator("Recommendation");  
   host.addTab(spec); 

   spec = host.newTabSpec("tab4");  
   spec.setContent(R.id.tab4);  
   spec.setIndicator("Feed");  
   host.addTab(spec);  

   pager.setAdapter(new MyPagerAdapter(this));  
   pager.setOnPageChangeListener(this);  
   host.setOnTabChangedListener(this);  

 }  
    @Override  
    public void onTabChanged(String tabId){  
         int pageNumber = 0;  
         if(tabId.equals("tab1"))
         {  
              pageNumber = 0;  
         }

         else if(tabId.equals("tab2"))
         {  
              pageNumber = 1;  
         }

         else if(tabId.equals("tab3"))
         {  
              pageNumber = 2;  
         }


         else
         {  
              pageNumber = 3;  
         }  

         pager.setCurrentItem(pageNumber);  
    }  
    @Override  
    public void onPageSelected(int pageNumber) {  
         host.setCurrentTab(pageNumber);  
    }
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }
    }

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter {  
    private Context ctx;  
    public MyPagerAdapter(Context ctx){  
        this.ctx = ctx;  
    }  
    @Override  
    public Object instantiateItem(ViewGroup container, int position) {  
         TextView tView = new TextView(ctx);  
         position++;  
         tView.setText("Page number: " + position);  
         tView.setTextColor(Color.RED);  
         tView.setTextSize(20);  
         container.addView(tView);  
         return tView;  
    }  
    @Override  
    public int getCount() {  
         return 3;  
    }  
    @Override  
    public boolean isViewFromObject(View view, Object object) {  
         return (view == object);  
    }  


}  

但是当我滑动标签时出现以下异常,

10-15 05:31:43.146: E/AndroidRuntime(1789): java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:192)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:124)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1002)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.view.Choreographer.doFrame(Choreographer.java:531)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.os.Handler.handleCallback(Handler.java:725)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.os.Handler.dispatchMessage(Handler.java:92)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.os.Looper.loop(Looper.java:137)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at java.lang.reflect.Method.invokeNative(Native Method)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at java.lang.reflect.Method.invoke(Method.java:511)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    10-15 05:31:43.146: E/AndroidRuntime(1789):     at dalvik.system.NativeStart.main(Native Method)

需要做什么?

4

4 回答 4

25

覆盖destroyItem

  @Override
    public void destroyItem(View container, int position, Object object) {
         ((ViewPager) container).removeView((View) object);
    }

从文档中引用

实现 PagerAdapter 时,必须至少重写以下方法:

instantiateItem(ViewGroup, int)
destroyItem(ViewGroup, int, Object)
getCount()
isViewFromObject(View, Object)

http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html

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

Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

Parameters
container   The containing View from which the page will be removed.
position    The page position to be removed.
object  The same object that was returned by instantiateItem(View, int).
于 2013-10-15T05:49:46.420 回答
3

一个完整的解决方案..

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
    public class MyPagerAdapter extends PagerAdapter {  
        private Context ctx;  
        public MyPagerAdapter(Context ctx){  
            this.ctx = ctx;  
        }  
        @Override  
        public Object instantiateItem(ViewGroup container, int position) {  
             TextView tView = new TextView(ctx);  
             position++;  
             tView.setText("Page number: " + position);  
             tView.setTextColor(Color.RED);  
             tView.setTextSize(20);  
             container.addView(tView);  
             return tView;  
        }  
        @Override  
        public int getCount() {  
             return 3;  
        }  
        @Override  
        public boolean isViewFromObject(View view, Object object) {  
             return (view == object);  
        }  

        @Override
        public void destroyItem(View container, int position, Object object) {
             ((ViewPager) container).removeView((View) object);
        }
    }  
于 2013-10-15T05:58:59.203 回答
2

答案是日志猫消息本身,

java.lang.UnsupportedOperationException: Required method destroyItem was not overridden

覆盖必须覆盖的destroyItem,我认为在pageradapter类中。

于 2013-10-15T05:47:04.697 回答
1
// try this
public class MyPagerAdapter extends PagerAdapter {
        private Context ctx;
        public MyPagerAdapter(Context ctx){
            this.ctx = ctx;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView tView = new TextView(ctx);
            tView.setText("Page number: " + (position+1));
            tView.setTextColor(Color.RED);
            tView.setTextSize(20);
            container.addView(tView);
            return tView;
        }
        @Override
        public int getCount() {
            return 4;
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }
         @Override
         public void destroyItem(ViewGroup container, int position, Object object) {
             ((ViewPager) container).removeView((View) object);
         }

     }
于 2013-10-15T06:06:22.370 回答