使用按钮进行viewPager滑动

时间:2014-09-04 11:40:13

标签: android android-layout button android-viewpager

我有一个viewPager,它显示了一些使用AsyncTask从JSON中获取的图像和文本。我的viewPager工作正常。但现在我需要一个按钮来滑动页面。因此,当我点击该按钮时,它应该移动到下一页,就像我们滑动页面并移动到下一页一样。我基本上需要移动到下一页而不滑动但是单击按钮。我怎么能这样做。

注意:我已经可以滑动页面并进入下一页。

以下是我的具有viewPager

的片段类
public class HomeViewFragment extends Fragment {

    private Activity activity;
    private int i = 0;
    private ImageLoader imageLoader;
    private Home home;
    private Home now_showing;
    private CommonVariable commonVariable;
    private HorizontalListView horizontalListView;
    private LazyAdapter adapter;
    private ViewPager pager;
    private ViewPageAdapter adapter1;
    private ProgressDialog dialog;


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

        horizontalListView = (HorizontalListView) view.findViewById(R.id.horizontal_listview);

        pager = (ViewPager) view.findViewById(R.id.home_nowshowing); 

        dialog = new ProgressDialog(HomeViewFragment.this.getActivity());
        dialog.setMessage("Loading Home Page. Please Wait.");
        dialog.setCancelable(true);



        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        imageLoader = new ImageLoader(activity);


        new BackGround().execute();
        return view;
    }

        public class BackGround extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... params) {

                home = JSONServices.getHomeListContent();
                now_showing = JSONServices.getHomeListContent();  

                return null;
            } 

            @Override
            /* check again */
            protected void onPostExecute(Void result) {

                commonVariable.setHomeListContent(home);
                commonVariable.setHomeListContent(now_showing);

                adapter = new LazyAdapter(home.getHome(), activity,Element.HORIZONTAL_MENU.getType());
                adapter1 = new ViewPageAdapter(home.getNowShowing(),Element.NOW_SHOWING.getType(), activity);


                horizontalListView.setAdapter(adapter);
                pager.setAdapter(adapter1); 

                dialog.dismiss();

                super.onPostExecute(result);

            }
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub

                dialog.show();
                super.onPreExecute();

                }
    }
}

我宣布viewpager

的布局
<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="6"
        android:background="@drawable/frame_background"
        android:orientation="horizontal"
        android:padding="5dp" >

        <android.support.v4.view.ViewPager
            android:id="@+id/home_nowshowing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inAnimation="@anim/fade_in_slider"
            android:outAnimation="@anim/fade_out_slider" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="5"
        android:background="@drawable/frame_background"
        android:orientation="horizontal" >

        <com.fortuna.cinemalk.customwidgets.HorizontalListView
            android:id="@+id/horizontal_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>

使用viewpager布局的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@color/grey"
    android:padding="2dp">

    <RelativeLayout
        android:layout_width="380dp"
        android:layout_height="wrap_content"
        android:background="@color/grey" >

        <ImageView
            android:id="@+id/image2"
            android:layout_width="match_parent"
            android:layout_height="245dp"
            android:scaleType="fitXY"
            android:src="@drawable/app" />



        <Button
            android:id="@+id/nowplayingButton"
            android:layout_width="212dp"
            android:layout_height="58dp"
            android:layout_alignBottom="@+id/image2"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="18dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/nowshowing_button" />

         <TextView
            android:id="@+id/nowplay_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="250dp"
            android:textColor="#000000"
            android:textSize="18dp"
            android:textStyle="bold" />

    </RelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以在onClick of button

中使用以下方法之一
pager.setCurrentItem(pageNumber);

OR

pager.setCurrentItem(pageNumber, isSmoothScroll;)