Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a swipe view to these existing coding

  • I need to add a swipe view in tab activity.In these below xml coding I just created a six tabs namely home,blog,audio,video,more and gallery.

  • The Tabs are created perfectly.But I cannot able to know how to add a swipe for that six tabs.I need to add a right to left swipe and left to right swipe.

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:

These is the AppPromopager.Here I am using the ViewPager.

 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);
            }
        });
    }

}
  • I thought with the help of viewpager I can able to get a Swipe view. ViewPager was added in AppPromoPager.

  • I added an AppPromoPager to that layout_home.xml file.But till now I didn't get a swipe view.Anybody can help me with these.Thank you.

like image 391
Steve Avatar asked Nov 10 '22 08:11

Steve


1 Answers

Better to use the Scrollable Tabs which uses a combination of Swipe view pager with the action bar tabs.

like image 94
Kailas Avatar answered Nov 15 '22 00:11

Kailas