0

イベントを作りたい。ボタン3を押すと、ViewPager1(位置0-> 1)とViewPager2(位置0-> 1)を同時に移動したい。ここに私のコードがあります

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) {
    }
}

ここから、エラーはありません。しかし、コードを追加する方法がわかりません.... button3 を押すと、viewpager2 が位置 '0' から '1' に移動し、viewpager1 も位置 '0' から '1' に移動します。助けて〜

4

1 に答える 1

1

使用できます

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

任意のページに変更します。2 番目のパラメーターは SmoothScroll です。これを true に設定すると、ページャーは新しい位置にスムーズにスクロールし、false に設定すると、ある場所から別の場所にジャンプします。

詳細については、ViewPager のドキュメントを参照してください。

于 2013-03-21T13:43:37.837 に答える