Reputation: 1
I wanna make the event. when I push the button 3, I want to move ViewPager1(position 0->1) also ViewPager2(position 0->1) at the same time. here is my code
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_a"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_b"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
viewpager_a1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager1(Position 0)" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button1" />
</LinearLayout>
viewpager_a2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager1(Position 1)" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button2" />
</LinearLayout>
viewpager_b1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager2(Position 0)" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button3" />
</LinearLayout>
viewpager_b2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager2(Position 1)" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button4" />
</LinearLayout>
main.java
public class MainActivity extends Activity {
private ViewPager viewpagerA;
private ViewPager viewpagerB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpagerA =(ViewPager)findViewById(R.id.viewpager_a);
viewpagerA.setAdapter(new AdpaterA(getApplicationContext(),viewpagerA));
viewpagerB =(ViewPager)findViewById(R.id.viewpager_b);
viewpagerB.setAdapter(new AdpaterB(getApplicationContext(),viewpagerB));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ViewPagerApdaterA.java
public class AdpaterA extends PagerAdapter {
public LayoutInflater mInflater;
public Context mContext;
public ViewPager mViewPager;
public AdpaterA(Context c, ViewPager pager) {
super();
mContext = c;
mInflater = LayoutInflater.from(c);
mViewPager=pager;
mViewPager.setAdapter(this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public boolean isViewFromObject(View pager, Object obj) {
return pager == obj;
}
@Override
public Object instantiateItem(View pager, int position) {
View v = null;
if (position == 0) {
v = mInflater.inflate(R.layout.viewpager_a1, null);
Button button1 = (Button) v.findViewById(R.id.button1);
button1.setOnClickListener(mPagerClickListener);
} else if (position == 1) {
v = mInflater.inflate(R.layout.viewpager_a2, null);
Button button2 = (Button) v.findViewById(R.id.button2);
button2.setOnClickListener(mPagerClickListener);
}
((ViewPager) pager).addView(v, 0);
return v;
}
private View.OnClickListener mPagerClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
changeviewpager(1);
Toast.makeText(mContext, "Button 1", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
changeviewpager(2);
Toast.makeText(mContext, "Button 2", Toast.LENGTH_SHORT).show();
break;
}
}
};
public void changeviewpager(int type) {
if(type==1){
mViewPager.setCurrentItem(1);
}else if(type==2){
mViewPager.setCurrentItem(0);
}
}
@Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((View) view);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
@Override
public void finishUpdate(View arg0) {
}
}
ViewPagerAdpaterB.java
public class AdpaterB extends PagerAdapter {
public LayoutInflater mInflater;
public Context mContext;
public ViewPager mViewPager;
public AdpaterB(Context c, ViewPager pager) {
super();
mContext = c;
mInflater = LayoutInflater.from(c);
mViewPager=pager;
mViewPager.setAdapter(this);
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View pager, Object obj) {
return pager == obj;
}
@Override
public Object instantiateItem(View pager, int position) {
View v = null;
if (position == 0) {
v = mInflater.inflate(R.layout.viewpager_b1, null);
Button button3 = (Button) v.findViewById(R.id.button3);
button3.setOnClickListener(mPagerClickListener);
} else if (position == 1) {
v = mInflater.inflate(R.layout.viewpager_b2, null);
Button button4 = (Button) v.findViewById(R.id.button4);
button4.setOnClickListener(mPagerClickListener);
}
((ViewPager) pager).addView(v, 0);
return v;
}
private View.OnClickListener mPagerClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button3:
changeviewpager(1);
Toast.makeText(mContext, "Button 3", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
changeviewpager(2);
Toast.makeText(mContext, "Button 4", Toast.LENGTH_SHORT).show();
break;
}
}
};
public void changeviewpager(int type) {
if(type==1){
mViewPager.setCurrentItem(1);
}else if(type==2){
mViewPager.setCurrentItem(0);
}
}
@Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((View) view);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
@Override
public void finishUpdate(View arg0) {
}
}
from here, there is no error. but i don't know how to add the code.... when push the button3, viewpager2 is moved from position '0' to '1' and also viewpager1 is moved from position '0' to '1'. help me~
Upvotes: 0
Views: 1040
Reputation: 46856
You can use
//change 1 to whatever page you want
viewPager.setCurrentItem(1, true);
to change to any arbitrary page. The second parameter is smoothScroll, if you set it to true the pager will smoothly scroll to the new position if set to false it will just jump from one to the other.
See the docs for ViewPager to learn more.
Upvotes: 1