适用于android中的圆形页面指示器

时间:2012-10-27 22:48:50

标签: android

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

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

我目前正在使用此指标: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;
}

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

感谢您提前提供任何帮助

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

与PageAdapter一起使用的CirclepageIndicator类和Adapter类。这里我使用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;
        }


}

第3步:ViewPager_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步:ViewPager_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>