2014-05-06 6 views
5
  • ich eine swipe view in tab activity .In diese unter xml hinzufügen müssen hinzufügen Codierung Ich habe gerade eine six tabs nämlich zu Hause, Blog, Audio, Video, mehr und Galerie.wie eine Swipe Blick auf diese bestehenden Codierung

  • Die Tabs erstellt werden perfectly.But ich nicht in der Lage kann zu wissen, wie ein Swipe hinzufügen für diese sechs tabs.I brauchen eine rechts nach links Streichen und von links nach rechts Swipe hinzuzufügen.

layout_home.xml:

<?xml version="1.0" encoding="utf-8"?> 

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

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

       <TabWidget 
       android:id="@android:id/tabs" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:background="@drawable/bottom_bar" 
       android:layout_weight="0" /> 

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

       <FrameLayout 
        android:id="@+id/tab_home" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 

       <FrameLayout 
        android:id="@+id/tab_video" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 

       <FrameLayout 
        android:id="@+id/tab_audio" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
       </FrameLayout> 

       <FrameLayout 
        android:id="@+id/tab_blog" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
       </FrameLayout> 

       <FrameLayout 
        android:id="@+id/tab_gal" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
       </FrameLayout> 

       <FrameLayout 
        android:id="@+id/tab_more" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
       </FrameLayout> 

       </FrameLayout> 

       <com.sit.gems.util.AppPromoPager 
       android:id="@+id/pager" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="0" /> 

     </LinearLayout> 

    </TabHost> 

</LinearLayout> 

AppPromoPager.java:

Dies ist die AppPromopager.Here ich die ViewPager verwende.

package com.sit.gems.util; 

import java.net.URLDecoder; 
import java.util.List; 
import android.content.Context; 
import android.os.Handler; 
import android.os.Parcelable; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.ImageView.ScaleType; 
import android.widget.RelativeLayout; 
import com.gems.android.R; 
import com.sit.gems.app.AppData; 
import com.sit.gems.util.ImageCache.ImageCacheParams; 

public class AppPromoPager extends RelativeLayout implements 
     ViewPager.OnPageChangeListener { 

    //private PromoPageIndicator indicator; 
    private PromoPagerListener listener; 
    private Handler trackerHandler = new Handler(); 
    private ImageFetcher mImageFetcher; 

    public AppPromoPager(Context context) { 
     super(context); 
    } 

    public AppPromoPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public AppPromoPager(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void init(FragmentActivity context, List<String> promotions) { 
     ImageCacheParams cacheParams = new ImageCacheParams(context, AppConstants.IMAGE_CACHE_DIR); 
     mImageFetcher = new ImageFetcher(context, AppData.getScreenWidth(),(AppData.getScreenWidth()/2)); 
     mImageFetcher.setLoadingImage(R.drawable.ic_launcher); 
     mImageFetcher.addImageCache(context.getSupportFragmentManager(), cacheParams); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     ViewPager pager = new ViewPager(context); 
     ViewPagerAdapter adapter = new ViewPagerAdapter(context, promotions); 
     pager.setAdapter(adapter); 
     pager.setCurrentItem(0); 
     pager.setOnPageChangeListener(this); 
     addView(pager, params); 
     //indicator = new PromoPageIndicator(context); 
     RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     params1.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
     params1.setMargins(0, 0, 0, 3); 
     //addView(indicator, params1); 
     //if (promotions.size() > 1) 
     // indicator.addCircleImage(promotions.size()); 
     /*pager.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 

       if (event.getAction() == MotionEvent.ACTION_MOVE 
         && scrollView != null) { 
        scrollView.requestDisallowInterceptTouchEvent(true); 
       } 
       return false; 
      } 
     });*/ 
    } 
    class ViewPagerAdapter extends PagerAdapter { 

     Context activity; 
     List<String> promotions; 

     public ViewPagerAdapter(Context context, List<String> promotions) { 
      this.promotions = promotions; 
      activity = context; 
     } 

     public int getCount() { 
      return promotions.size(); 
     } 

     public Object instantiateItem(View collection, final int position) { 
      final ImageView view = new ImageView(activity); 
      view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
        LayoutParams.MATCH_PARENT)); 
      view.setScaleType(ScaleType.CENTER); 

      String imageUrl = promotions.get(position); 


      if (imageUrl != null && imageUrl.length() > 0) { 
       setImage(view, imageUrl); 
      } 

      view.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        listener.selectedPromo(promotions.get(position),position); 
       } 
      }); 
      ((ViewPager) collection).addView(view, 0); 
      return view; 
     } 

     @Override 
     public void destroyItem(View arg0, int arg1, Object arg2) { 
      ((ViewPager) arg0).removeView((View) arg2); 
     } 

     @Override 
     public boolean isViewFromObject(View arg0, Object arg1) { 
      return arg0 == ((View) arg1); 
     } 

     @Override 
     public Parcelable saveState() { 
      return null; 
     } 
    } 

    @Override 
    public void onPageScrollStateChanged(int arg0) { 
     //listener.selectedPosition(arg0); 
    } 

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

    } 

    @Override 
    public void onPageSelected(int position) { 
     listener.selectedPosition(position); 
    } 

    public interface PromoPagerListener { 
     public void selectedPromo(String offer,int postion); 
     public void selectedPosition(int postion); 
    } 

    public void setPromoPagerListener(PromoPagerListener listener) { 
     this.listener = listener; 
    } 

    private void setImage(final ImageView view, final String imageUrl) { 
     trackerHandler.post(new Runnable() { 

      @Override 
      public void run() { 
       mImageFetcher.loadImage(URLDecoder.decode(imageUrl),view); 
      } 
     }); 
    } 

} 
  • Ich dachte, mit Hilfe von viewpager kann ich in der Lage eine Swipe Ansicht zu erhalten. ViewPager was added in AppPromoPager.

  • Ich habe einen AppPromoPager zu diesem layout_home.xml file.But bis jetzt habe ich nicht einen Schlag bekommen view.Anybody mich mit these.Thank Ihnen helfen können.

+0

Sie können meinen Code von http://stackoverflow.com/questions/18051271/android-sherlock-fragmenttab-clear-data ich hatte sherlockActionbar verwendet, aber Sie können Aktionsleiste verwenden und auf diese Weise können Sie Registerkarte wischen überprüfen Sie es – PankajAndroid

+0

@ PankajAndroid Ich habe bereits 6 Tabs erstellt. Ich denke, mit der Hilfe von Paketname.AppPromoPager in XML-Code kann ich das Problem lösen.Did Sie wissen, wie Sie diese tun? – Steve

+2

Warum haben Sie ein benutzerdefiniertes RelativeLayout erstellt, um einen ViewPager zu implementieren? Es ist möglicherweise besser, das ViewPager-Widget direkt in TabHost zu verwenden. Sie können [ein tolles Tutorial] (http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/) mit dieser Funktion finden. – Fllo

Antwort

0

Was ich getan habe, ist in jedem Tabcontent, den Swipe-Listener implementieren. Implementieren Sie im Tabhost eine Swipe-Methode.

tabcontent

layout.setOnTouchListener(new OnTouchListener() { 
private PointerCoords mDownPos = new PointerCoords(); 
private PointerCoords mUpPos = new PointerCoords(); 

@Override 
public boolean onTouch(View arg0, MotionEvent event) { 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: { 
      event.getPointerCoords(0, mDownPos); 
      return true; 
     } 
     case MotionEvent.ACTION_UP: { 
      event.getPointerCoords(0, mUpPos); 
      float dx = mDownPos.x - mUpPos.x; 
      // Check for horizontal wipe 
      if (Math.abs(dx) > SWIPE_MIN_DISTANCE) { 
       if (dx > 0) { 
        mParent.swipe(DIRECTION_RIGHT); 
       } else { 
        mParent.swipe(DIRECTION_LEFT); 
       } 
       return true; 
      } 
     } 
    } 
    return false; 
} 
}); 


protected void swipe(int direction) { 
    int current = mTabHost.getCurrentTab(); 
    if (direction == DIRECTION_LEFT) { 
    int newTab = --current % mTabHost.getTabWidget().getTabCount(); 
    mTabHost.setCurrentTab((newTab < 0) ? mTabHost.getTabWidget().getTabCount()-1 : newTab); 
} 
else { 
    mTabHost.setCurrentTab(++current % mTabHost.getTabWidget().getTabCount()); 
} 
} 
0

hey sollten Sie dies tun, indem ActionBar Tabs + viewpager hier ist ein Tutorial, das Sie machen lassen, was Sie wollen. http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

+0

Bereits habe ich diese Anleitung gesehen, es hat mir nicht geholfen, mein Problem zu lösen – Steve

+0

Wenn Sie Tabs wie Google Play möchten können Sie [PagerSlidingTabStrip] (https://github.com/astuetz/PagerSlidingTabStrip) dies auch ein Beispiel dafür verwenden ist so einfach zu bedienen Sie werden auf jeden Fall das lieben .. – Abhishek