2

I have two questions about ViewPager.

I have in my activity 2 ViewPagers, one for each week (of date). For example, one for 14/9/14 - 20/9/14, and the the other one for the next week 21/9/14 - 27/9/14. When I slide the ViewPager to it's next one, it will change each on of the ViewPagers to the next week of itself (21/9/14 - 27/9/14, 28/9/14 - 4/10/14).

  1. When I change the data f the second ViewPager and then slide the first ViewPager to it's next one (and this is the same week like the second view pager data that I changed), it won't show the changes that I made, unless I slide two more times and then return to the changed week (because of the memory of the ViewPager). I tried mPager1.setOffscreenPageLimit(0); , but it didn't helped. What can I do in order to solve that?

  2. I want that when I slide one of my ViewPagers, so the other one will animate itself exactly like my finger is sliding the other ViewPager. Any ideas how to do that?

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Itay
  • 219
  • 5
  • 14

1 Answers1

3

I hope it helps:

it won't show the changes that I made

use adapter.notifyDataSetChanged(); after you have made changes.

I want that when I slide one of my ViewPagers, so the other one will animate itself exactly like my finger is sliding the other ViewPager. Any ideas how to do that?

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager1= (ViewPager) findViewById(R.id.pager1);
    viewPager2= (ViewPager) findViewById(R.id.pager2);

    viewPager1.setAdapter(new MyAdapter1(getSupportFragmentManager()));
    viewPager2.setAdapter(new MyAdapter2(getSupportFragmentManager()));

    viewPager1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            viewPager2.onTouchEvent(event);
            return false;
        }
    });

    viewPager2.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            viewPager1.onTouchEvent(event);
            return false;
        }
    });

    }

enter image description here

if you got

09-19 05:49:29.313: E/AndroidRuntime(1272): java.lang.IllegalArgumentException: pointerIndex out of range

try: in your layout file you must use <package name of HackyViewPager.HackyViewPager instead of <android.support.v4.view.ViewPager

public class HackyViewPager extends ViewPager {

    public HackyViewPager(Context context) {
        super(context);

    }
    public HackyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        }

   }
   @Override
   public boolean onTouchEvent(MotionEvent event) {
       try {
        return super.onTouchEvent(event);
     }catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
     }

   }

}

Reference:

Controlling two ViewPager Together

Community
  • 1
  • 1
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • Thank you very much! That really what I need!! Where to put the `adapter.notifyDataSetChanged();` ? How can I know from the activity if the fragment changed? – Itay Sep 19 '14 at 09:34
  • `Where to put the adapter.notifyDataSetChanged(); ?` after you changed the data of the fragment. `How can I know from the activity if the fragment changed?` use callback method or use `getActivity().ihaveChanged();` that means you call a method from activity if it is not clear please ask it on SO with full description of your situation. – mmlooloo Sep 19 '14 at 09:40
  • `09-19 05:49:29.313: E/AndroidRuntime(1272): java.lang.IllegalArgumentException: pointerIndex out of range` That what happening when I use the `onTouch` method – Itay Sep 19 '14 at 09:51
  • i do not know whats going on there but please ask it with full logcat, i and others are ready to help you.. – mmlooloo Sep 19 '14 at 09:53
  • look at these links and if still you have a problem ask it on SO:http://stackoverflow.com/questions/16459196/java-lang-illegalargumentexception-pointerindex-out-of-range-exception-dispat and http://stackoverflow.com/questions/6919292/pointerindex-out-of-range-android-multitouch – mmlooloo Sep 19 '14 at 15:07
  • Hi! Thanks for the help! I fixed my first problem with the data refresh! About the second one- in the first link it says that there is a bug, and to fix that it says that I need to "create a custom view that extends ViewPager, override onTouchEvent and wrap the call to super.onTouchEvent in a try/catch.". How can I do that? And this solution will work always? – Itay Sep 23 '14 at 07:56
  • 1
    in your layout file you must use ` – mmlooloo Sep 23 '14 at 15:52
  • Well, thanks to the `try` and `catch` it's not crashing now... The problem is that the 2 `HackyViewPager` are not moving together (probably because it always get the `catch`...) – Itay Sep 23 '14 at 16:15
  • I changed my sample code with that HackyViewPager and it is working sorry i do not know what is your problem. if you want to make sure tell me your email so you can see it. – mmlooloo Sep 23 '14 at 16:24