我正在使用this library为viewPager创建指标(在Android中)。我只想向用户显示他可以更改视图。出于某种奇怪的原因,即使我创建视图并进行设置,也不会显示。这是代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = (ViewPager) findViewById(R.id.instructionViewPager);
adapter = new InstructionPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
viewPager.addOnPageChangeListener(listener);
ViewPagerIndicator viewPagerIndicator = (ViewPagerIndicator)
findViewById(R.id.view_pager_indicator);
viewPagerIndicator.setupWithViewPager(viewPager);
viewPagerIndicator.addOnPageChangeListener(listener);
我在.xml中有它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical">
<TextView
android:id="@+id/biometricTextLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="84dp"
android:layout_marginRight="84dp"
android:layout_marginTop="38dp"
android:text="Biometric"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp" />
<android.support.v4.view.ViewPager
android:id="@+id/instructionViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/view_pager_indicator"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/biometricTextLabel"
android:layout_marginBottom="0dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/instructionbContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/instructionbBack"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="12dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:text="Continue"
android:textColor="@android:color/background_light" />
<com.github.vivchar.viewpagerindicator.ViewPagerIndicator
android:id="@+id/view_pager_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:delimiterSize="8dp"
app:itemSize="8dp"
android:layout_above="@id/instructionbContinue"
android:layout_marginBottom="30dp"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/instructionbBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:background="@android:color/transparent"
android:text="Go back"
android:textColor="@android:color/white" />
</RelativeLayout>
我做错了什么?
编辑:我已经添加了布局的所有xml。
编辑2:我添加了pageAdapter,很简单:
public class InstructionPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
public InstructionPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
}
public void addFragment(Fragment fragment) {
this.fragments.add(fragment);
}
@Override
public Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
@Override
public int getItemPosition(Object object) {
return POSITION_UNCHANGED;
}
@Override
public int getCount() {
return this.fragments.size();
}
}
答案 0 :(得分:0)
更改布局,如下面的模板,这对我有用,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
答案 1 :(得分:0)
似乎您还没有向适配器添加任何片段。如果在将适配器分配给viewpager后将片段添加到适配器,则尝试通知适配器。