如何在Viewpager中用点显示全屏

时间:2015-03-30 06:10:12

标签: android android-fragments android-viewpager

我已经使用点实现了viewpager,但在设备中它显示为view pager with dots  viewpager中的第二个屏幕在滚动前可见30%,如何修复此问题。在此处添加我的代码,我没有在代码中出错 这是我的代码

public class MainActivity extends Activity {
    // Declare Variables
    ViewPager viewPager;
    PagerAdapter adapter;
    LinearLayout llDots;
    TextView fbLogin, textViewText1, textView2, textView3, textView4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from viewpager_main.xml
        setContentView(R.layout.viewpager_main);
        Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Candara.ttf");
        llDots = (LinearLayout) findViewById(R.id.llDots);
        viewPager = (ViewPager) findViewById(R.id.pager);

        adapter = new ViewPagerAdapter(this);
        viewPager.setAdapter(adapter);
        fbLogin = (TextView) findViewById(R.id.textViewfacebook);
        textViewText1 = (TextView) findViewById(R.id.textViewfacebook);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView3 = (TextView) findViewById(R.id.textView3);
        textView4 = (TextView) findViewById(R.id.textView4);

        for (int i = 0; i < adapter.getCount(); i++) {
            ImageButton imgDot = new ImageButton(this);
            imgDot.setTag(i);
            imgDot.setImageResource(R.drawable.dot_selector);
            imgDot.setBackgroundResource(0);
            imgDot.setPadding(5, 5, 5, 5);

            LayoutParams params = new LayoutParams(30, 30);
            imgDot.setLayoutParams(params);
            if (i == 0)
                imgDot.setSelected(true);

            llDots.addView(imgDot);
        }

        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
                Log.e("", "Page Selected is ===> " + pos);
                for (int i = 0; i < adapter.getCount(); i++) {
                    if (i != pos) {
                        ((ImageView) llDots.findViewWithTag(i))
                                .setSelected(false);
                    }
                }
                ((ImageView) llDots.findViewWithTag(pos)).setSelected(true);
            }

            @Override
            public void onPageScrolled(int pos, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }

    public void facebookLogin(View v) {
        Intent fbIntent = new Intent(MainActivity.this, EditProfile.class);
        startActivity(fbIntent);
    }

}

1 个答案:

答案 0 :(得分:2)

请使用此布局作为您的XML&amp;在项目中导入VPI库。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

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

    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/mc_cpi"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:radius="4dp"
        android:padding="5dp"
        app:strokeColor="@color/primary_dark"
        app:strokeWidth="1px"
        app:snap="true"
        app:fillColor="@color/primary" />

</RelativeLayout>