0

我想举办活动。当我按下按钮 3 时,我想同时移动 ViewPager1(position 0->1) 和 ViewPager2(position 0->1)。这是我的代码

主要的.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) {
    }
}

从这里开始,没有错误。但我不知道如何添加代码....当按下按钮 3 时,viewpager2 从位置“0”移动到“1”,并且 viewpager1 从位置“0”移动到“1”。帮帮我~

4

1 回答 1

1

您可以使用

//change 1 to whatever page you want
viewPager.setCurrentItem(1, true);

更改为任意页面。第二个参数是smoothScroll,如果你设置它为true,分页器会平滑滚动到新的位置,如果设置为false,它只会从一个跳转到另一个。

请参阅 ViewPager 的文档以了解更多信息。

于 2013-03-21T13:43:37.837 回答