Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android , Stop auto slide in on touch

I developing one application , in that i having view pager to display different video image, there are n number of pager screen,

All page is auto slide like following .

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    ViewGroup v=(ViewGroup)inflater.inflate(R.layout.fragment_home, container, false);

    ButterKnife.bind(this, v);



    /*Snackbar.make(v, "Home Fragment", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();*/
    //Toast.makeText(getActivity(),"home Fragment",Toast.LENGTH_LONG).show();
    viewPager.setAdapter(new MyPagerAdapter());

    t=new Thread(){
        @Override
        public void run()
        {

            try {
                while(true) {
                    count %= size;
                    Log.w("slide", "" + count);
                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            viewPager.setCurrentItem(count);
                        }
                    });

                    count++;
                    Thread.currentThread().sleep(5000);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    //t.start();

    return v;
}

I want to stop auto slide pager when i touch to this particular pager , when i remove my finger from the pager then auto slide of pager started and stop on again touch, how to fulfill this functionality?

like image 551
TopsAndy Avatar asked Nov 09 '22 22:11

TopsAndy


1 Answers

I achieving my functionality by using https://github.com/Trinea/android-auto-scroll-view-pager

I used following custom viewpager

<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

and set interval for specific time for auto slide

setInterval(long)

Then used : startAutoScroll() method for auto slide

and : topAutoScroll() for stop animation on touch , like following ,

     layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch(event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        Log.w("touched","down");
                        stopScroll();
                        return true;
                        //break;

                    case MotionEvent.ACTION_UP:
                        Log.w("touched","up");
                        startScroll();
                        return true;
                        //break;
                }

                return false;
            }
        });

and use following gradle :

compile ('cn.trinea.android.view.autoscrollviewpager:android-auto-scroll-view-pager:1.1.2') 
{
   exclude module: 'support-v4'
}
like image 137
TopsAndy Avatar answered Nov 14 '22 22:11

TopsAndy