1

对于我的应用程序,我有一个基本的主要活动,其中包含一个页面查看器。在活动课上,我有:

MyPagerAdapter adapter = new MyPagerAdapter(this);
viewPager.setAdapter(adapter);

适配器类如下所示:

private class MyPagerAdapter extends PagerAdapter {

private ArrayList<LinearLayout> views;

public MyPagerAdapter(Context context) {
    views = new ArrayList<LinearLayout>();
    views.add(new questionListView(context));
}

    @Override
    public void destroyItem(View view, int arg1, Object object) {
        ((ViewPager) view).removeView((LinearLayout) object);
    }

    @Override
    public void finishUpdate(View arg0) {

    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public Object instantiateItem(View view, int position) {
        View myView = views.get(position);
        ((ViewPager) view).addView(myView);
        return myView;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {

    }

    }
    }
}

questionListView 类看起来像:

public class questionListView extends LinearLayout {

public questionListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public questionListView(Context context) {
    super(context);
    init();
}

private void init() {

    LayoutInflater factory = LayoutInflater.from(getContext());
    View myView = factory.inflate(R.layout.view1,null);

    addView(myView);

}

}

主要活动布局:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:alpha="4"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#000000" />

</android.support.v4.view.ViewPager>

view1 是一个布局视图,一个带有其他布局的线性布局。

当我运行该应用程序时,会显示主要活动,但没有显示带有 view1-layout 的视图。

为什么不,我在这里做错了什么?我没有收到任何错误。

rg,埃里克

4

1 回答 1

0

Luksprog 帮助解决了这个问题。将 PagerTitleStrip 设置为 wrap_content 解决了它。

于 2012-11-07T21:27:11.873 回答