Android 在相对窗口中用按钮从片段中滑动视图

Android 在相对窗口中用按钮从片段中滑动视图,android,button,android-fragments,fragment,swipe,Android,Button,Android Fragments,Fragment,Swipe,我是Android编程新手。 我的布局如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" &

我是Android编程新手。 我的布局如下:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/fundo"
        android:background="@color/roxo"
        tools:context="com.fnsp.anedotaspiadas.Anedotas" />

    <LinearLayout
        android:id="@+id/fundo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/adView"
        android:layout_alignParentBottom="true"
        android:background="@color/roxo"
        android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/previousButton"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/previous" />

            <ImageButton
                android:id="@+id/voltarButton"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/voltar" />

            <TextView
                android:id="@+id/counter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:text="TextView" />

            <ImageButton
                android:id="@+id/nextButton"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/next" />

        </LinearLayout>
    </RelativeLayout>

Fragment Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.fnsp.anedotaspiadas.Anedotas$PlaceholderFragment" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true" >

            <TextView
            android:id="@+id/section_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:padding="@dimen/padding"
            android:textColor="@color/branco"
            android:textSize="20sp" />

    </ScrollView>

</RelativeLayout>
按钮在片段外部,但我希望按钮能够在片段之间切换,文本视图显示片段编号。我从textView开始,但当我尝试打印数字时,它打印的数字不正确,当我刷卡时,他有时会跳几个数字

    public class Anedotas extends ActionBarActivity {
        DBTools dbTools = new DBTools(this);
        static ArrayList<String> ar = new ArrayList<String>();
        private static TextView counter;
        private ImageButton previous;
        private ImageButton next;
        private ImageButton voltar;



    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anedotas);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras(); 
        int aux = Integer.parseInt(extras.getString("categoria"));  
        this.setTitle(dbTools.retornaNomeCategoria(aux));


        counter = (TextView) findViewById(R.id.counter);
        previous = (ImageButton) findViewById(R.id.previousButton);
        next = (ImageButton) findViewById(R.id.nextButton);
        previous = (ImageButton) findViewById(R.id.voltarButton);


        //buscar as anedotas...
        ar = dbTools.getAnedotasCategoria(aux); 

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        //alteraBarraInferior();

    }

    @Override
    public void onResume(){
        super.onResume();       
    }

    @Override
    public void onPause(){
        super.onPause();
        //this.finish();
    }

     /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return ar.size();
        }

    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private final static String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_anedotas, container, false);

            Bundle args = getArguments();
            int i = args.getInt(ARG_SECTION_NUMBER);
            String help = i+"/"+ar.size();
            counter.setText(help);
            ((TextView) rootView.findViewById(R.id.section_label)).setText(Html.fromHtml(ar.get(i-1)));
            return rootView;
        }
    }
}
有人知道问题出在哪里吗?即使现在,我如何创建ClickListener来调用上一个片段或下一个片段呢。
感谢您的帮助和帮助。

要打印当前的ViewPager位置,您应该使用ViewPager:

ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
        counter.setText(String.valueOf(position));
    }
});
要更改ViewPager的当前位置,应使用以下方法:

这里是你的碎片的位置。这应该从按钮的onClick侦听器调用。当然,您应该将该1更改为正确的值

为什么从onCreateView打印当前位置不正确

ViewPager由于其实现而一次初始化多个片段。例如,您的ViewPager中有5个片段,ViewPager将显示片段3。它将初始化片段3、片段2和片段4。所以这3个片段的onCreateView将被调用,这就是为什么您会得到错误的位置

pager.setCurrentItem(1)