如何使用按钮单击滑动片段

时间:2014-05-30 16:57:43

标签: java android android-fragments android-viewpager swipe

我有两个片段和一个MainActivity类。 一切正常,但我想用onClick按钮滑动片段。例如,如果我单击片段A上的按钮,它将滑动到片段B.我在哪里需要添加代码?在MainActivity或Fragments中?

A:

package com.example.mytesttabandsweeper;



import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class Fragment_A extends Fragment implements OnClickListener {
    TextView wynik;
    Button przycisk;
    EditText procent,cena;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v =  inflater.inflate(R.layout.fragment_a,  container, false);

        return v;
    }

}

B:

package com.example.mytesttabandsweeper;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;



public class Fragment_B extends Fragment implements OnClickListener {


    TextView slowo;
    EditText podane_slowo;
    private ImageView iv;
    private Bitmap bitmap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub\

        View v = inflater.inflate(R.layout.fragment_b,  container, false);

        return v;
    }

}

MainActivity:

package com.example.mytesttabandsweeper;



import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

    ViewPager viewPager=null;
    Button test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager=getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));



    }




}

class MyAdapter extends FragmentStatePagerAdapter{

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }


    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        if(i==0)
        {
            fragment = new Fragment_A();
        }

        if(i==1)
        {
            fragment = new Fragment_B();
        }


        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        if(position==0)
        {
            return "A";
        }
        if(position==1)
        {
            return "B";
        }

        return null;
    }
}

一切正常,但我想用onClick按钮滑动片段。例如,如果我单击片段A上的按钮,它将滑动到片段B.我在哪里需要添加代码?在MainActivity或Framgments中?

1 个答案:

答案 0 :(得分:7)

点击Button点击即可拨打

viewPager.setCurrentItem(position);

或者如果您想要平滑滚动,可以使用

viewPager.setCurrentItem(position,true);