0

我正在尝试使用圆形页面指示器来允许用户通过翻转屏幕来查看多个页面。

每个页面都有用于视图的单独 XML 文件,每个页面都有一个按钮,该按钮绑定到 Activity 中的 java 方法。我想知道如何为多个页面初始化 Activity 中的所有按钮。因为目前,我只能为视图的第一页初始化按钮。我无法初始化第二页和第三页的按钮。有谁知道如何实现这一目标。我已将所有要为所有按钮完成的工作放在一个活动中。

我目前正在使用这个指标:http: //viewpagerindicator.com/

这是我的圆形页面指示器适配器:

 @Override
public Object instantiateItem(View collection, int position) {

    inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resid = 0;
    //View v = null;// inflater.inflate( R.layout.gaugescreen, (ViewPager)collection, false );
    switch( position )
    {
        case 0:
            resid = R.layout.gaugescreen;
            break;
        case 1:
            resid= R.layout.liveworkoutstatisticsscreen;
            break;
         case 2:
             resid = R.layout.mapscreen;
             break;
        default:
            resid = R.layout.gaugescreen;
            break;


    }


        View view = inflater.inflate(resid, null);

        ((ViewPager) collection).addView(view,0);

        return view;
}

有谁知道如何实现这一目标?

感谢您提前提供任何帮助

4

2 回答 2

1

使用setOffscreenPageLimit调整屏幕外页面限制,以便您的其他页面也被实例化。

于 2012-10-27T22:59:06.517 回答
1

CirclepageIndicator 类和 Adapter 类与 PageAdapter 一起使用。这里我使用 CirclePageIndicator 在活动底部显示圆圈并添加两个片段类(可以添加任意数量的片段)。

第 1 步:FragmentActivity 类(如主类)

 import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.Window;

    import com.viewpagerindicator.CirclePageIndicator;
    import com.viewpagerindicator.PageIndicator;

    public class ViewPager_activity extends FragmentActivity
    {
        private ViewPager _mViewPager;
        private ViewPagerAdapter _adapter;
        PageIndicator mIndicator;
        CirclePageIndicator circlePageIndicator;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.viewpager_main_activity);

            setUpView();
        }

       private void setUpView()
       {
            _adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
            _mViewPager = (ViewPager) findViewById(R.id.pager);

            _mViewPager.setAdapter(_adapter);
            circlePageIndicator=(CirclePageIndicator)findViewById(R.id.indicator);
            mIndicator=circlePageIndicator;
            circlePageIndicator.setViewPager(_mViewPager);
            final float density = getResources().getDisplayMetrics().density;
            circlePageIndicator.setBackgroundColor(0xFFCCCCCC);
            circlePageIndicator.setRadius(10 * density);
            circlePageIndicator.setPageColor(0xFF888888);
            circlePageIndicator.setFillColor(0x880000FF);
            circlePageIndicator.setStrokeColor(0xFF000000);
            circlePageIndicator.setStrokeWidth(2 * density);
            circlePageIndicator.setSnap(true);
           _mViewPager.setCurrentItem(0);

           setCirclePageIndicator();
           }
            public void setCirclePageIndicator()
            {
                circlePageIndicator.setOnPageChangeListener(new OnPageChangeListener() {

                    @Override
                    public void onPageSelected(int position) {
                        // TODO Auto-generated method stub
                        btnAction(position);
                    }

                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                        // TODO Auto-generated method stub
                        //btnAction(position);
                    }

                    @Override
                    public void onPageScrollStateChanged(int arg0) {
                        // TODO Auto-generated method stub

                    }
                });
            }

        private void btnAction(int action){
            switch(action){
              case 0: 

                  break;



              case 2: 


                 startActivity(new Intent(ViewPager_activity.this,HomePage_Activity.class));
                  break;
            }
        }
    }

第 2 步:FragmentPageAdapter.class

    public class ViewPagerAdapter extends FragmentPagerAdapter
    {


        private Context _context;
         public static int totalPage=3;

         public ViewPagerAdapter(Context context, FragmentManager fm) {
                super(fm);
                _context=context;

          }

    @Override
        public Fragment getItem(int position) {
            Fragment f = new Fragment();
            switch(position){
            case 0:
                f=ViewPager_first_fragment.newInstance(_context);
                break;
            case 1:
                f=ViewPager_second_fragment.newInstance(_context);
                break;


            }
            return f;
        }
        @Override
        public int getCount() {
            return totalPage;
        }


}

第三步:查看Pager_first_fragment.class

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ViewPager_first_fragment extends Fragment
{
    ImageView advertiseImage;

    public static Fragment newInstance(Context context) {

            ViewPager_first_fragment f = new ViewPager_first_fragment();

            return f;
        }
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
        {

            ViewGroup root = (ViewGroup) inflater.inflate(R.layout.viewpager_first_fragment, null);
            advertiseImage=(ImageView)root.findViewById(R.id.advertiseScreen1);
            advertiseImage.setImageResource(R.drawable.advertisescreen_1);
            return root;
        }
}

第 4 步:查看Pager_main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="10dip" />

</RelativeLayout>

第 5 步:

   <ImageView
       android:id="@+id/advertiseScreen1"
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"

       />

</LinearLayout>

第 6 步:style.xml 最后你必须更新你的 style.xml indise res/value 文件夹。

<style name="AppTheme" parent="AppBaseTheme">
        <item name="vpiCirclePageIndicatorStyle">@style/CirclePageIndicator</item>
    </style>

    <style name="Widget"></style>

    <style name="CirclePageIndicator" parent="Widget">
        <item name="android:textColor">#ffffff</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">italic</item>
        <item name="android:paddingLeft">16dp</item>
        <item name="android:paddingRight">16dp</item>
        <item name="android:fadingEdge">horizontal</item>
        <item name="android:fadingEdgeLength">8dp</item>
    </style>
于 2014-09-25T18:37:15.853 回答