6

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?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
TopsAndy
  • 202
  • 2
  • 11
  • For this you have to manipulate thread. Check here----> http://www.tutorialspoint.com/java/java_thread_control.htm http://stackoverflow.com/questions/10961714/how-to-properly-stop-the-thread-in-java – Chaudhary Amar Jan 04 '16 at 05:45
  • I achieve this through this one : https://github.com/Trinea/android-auto-scroll-view-pager – TopsAndy Jan 04 '16 at 06:15

2 Answers2

2

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'
}
TopsAndy
  • 202
  • 2
  • 11
-1

You can identify touch event of viewpager and on the basis of you can stop and start slider. Like below you will get touch event and then you can maintain your thread work.

viewPager.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    // put your code here to stop and start slide, means put your thread code here..
                    return false;
                }
            });
Ronak Joshi
  • 1,553
  • 2
  • 20
  • 43