我的应用程序 ViewPager 和 ViewPagerIndicator 实际上有问题 ( http://viewpagerindicator.com/ ) 没有显示任何内容,我不明白为什么。我多次检查了所有内容(正确设置适配器,在 instantiateItem 中添加 View ,在 getCount() 中返回合适的大小,...),尝试了一些事情(更改 xml 中的布局参数,重新处理 MyPagerAdapter 类),甚至在谷歌和这里寻找类似的问题,但没有任何效果/不是我的问题。
我也有一个问题,不知道是否有链接,但我的 PagerAdapter 中的 instantiateItem 方法只被调用了 2 次,而 getCount() 很好地返回了 3 次。
我真的不知道那是从哪里来的,如果有人有想法我会很高兴接受它:
OnCreate(包含 ViewPager 和 ViewPagerIndicator 的 Activity):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_infos_vehicle);
title = (TextView) findViewById(R.id.title);
ll = (LinearLayout) findViewById(R.id.mygallery);
filenames = new ArrayList<String>();
[...]
MyPagerAdapter adapter = new MyPagerAdapter(getResources().getStringArray(R.array.infos_vehicle));
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.titles_pager);
indicator.setFooterIndicatorStyle(IndicatorStyle.Triangle);
indicator.setViewPager(pager);
[...]
}
MyPagerAdapter 类(第 3 种情况在这里进行更明显的测试,看看是不是我的 fillGeneral 或 fillEquipments 方法搞砸了):
私有(private)类 MyPagerAdapter 扩展了 PagerAdapter {
private final String[] TITLES;
public MyPagerAdapter(String[] titles) {
TITLES = titles;
}
@Override
public int getCount() {
return (TITLES.length);
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = null;
switch (position) {
case (0):
v = inflater.inflate(R.layout.linear_layout, null);
fillGeneral((ViewGroup) v.findViewById(R.id.layout));
break;
case (1):
v = inflater.inflate(R.layout.linear_layout, null);
fillEquipments((ViewGroup) v.findViewById(R.id.layout));
break;
case (2):
v = new ImageView(getApplicationContext());
((ImageView) v).setImageDrawable(getApplicationContext().getResources().getDrawable(android.R.drawable.alert_dark_frame));
break;
}
((ViewPager) collection).addView(v);
return (v);
}
@Override
public CharSequence getPageTitle(int position) {
return (TITLES[position]);
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0.equals(arg1));
}
@Override
public Parcelable saveState() {
return null;
}
和 Activity xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/first_color" />
<Button
android:id="@+id/try_vehicle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:onClick="tryingAlert"
android:text="@string/try_vehicle" />
<Button
android:id="@+id/ask_informations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/try_vehicle"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:onClick="informationsAlert"
android:text="@string/ask_informations" />
<Button
android:id="@+id/remove_selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="removeFromSelection"
android:layout_below="@+id/ask_informations"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/remove_selection" />
<HorizontalScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/remove_selection"
android:layout_marginTop="18dp"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/titles_pager"
android:layout_below="@id/scroll"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/titles_pager" />
</RelativeLayout>
</ScrollView>
它给了我什么( Activity 到此结束,如图所示,ViewPagerIndicator 不显示任何内容): http://i.imgur.com/vRa1O.jpg (外链,发不了图片抱歉!)
最佳答案
好的所以最后没有任何问题,只是我做错了。
如前所述,ViewPagerIndicator 只是白底白字,所以我看不到它,现在好了。
对于 ViewPager,它就在我的 ScrollView 的最末端,这意味着没有可用的“真实”屏幕空间,因此 android:layout_height="match_parent"
无效。这似乎是 ViewPager 的常见问题,我会看看我能做些什么来适应它。
无论如何感谢您的帮助,我希望这篇文章对某人有用!
关于android - ViewPager 和 ViewPagerIndicator 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12509332/