use*_*182 2 android android-layout android-gridview android-fragments android-viewpager
所以,我想要在一个片段中添加ViewPager视图(而不是片段)作为gridview的标题,但是当我将它设置为我的标题视图时,我的ViewPager不会出现.我目前正在使用Etsy的StaggeredGridView来允许我的gridview有一个标题,我使用PagerAdapter而不是FragmentStatePagerAdapter来滚动视图而不是片段.我打算只使用片段,但我不确定这是否会好,因为我认为,这将被认为是嵌套片段,只支持4.2+并通过支持库:我的应用程序支持4.0以上,此刻,我正在使用原生片段.
我也试过以编程方式设置视图,但仍然没有显示.
我在ViewPager中设置了一个toast,看看是否有任何实际创建,并且toast按预期显示,但仍然没有标题.我试图膨胀的观点不应该是问题,因为如果我将它作为标题单独使用它就会显示出来.这是我到目前为止所拥有的:
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflate fragment which contains the gridview. Have a pointer to the gridview.
View v = inflater.inflate(R.layout.fragment_menu, container, false);
mGridView = (StaggeredGridView) v.findViewById(R.id.dishesGridView);
View header = inflater.inflate(R.layout.view_pager_header, container, false);
ViewPager mPager = (ViewPager) header.findViewById(R.id.pager);
//list of views to test header
final List<View> views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.menu_header, null));
mPager.setAdapter(new PagerAdapter() {
public View getView(int position) {
return views.get(position);
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
View v = getView(position);
((ViewPager)collection).addView(v);
Toast.makeText(getActivity(),
"Hello ViewPager",
Toast.LENGTH_SHORT).show();
return v;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == (View) arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return views.size();
}
});
mGridView.addHeaderView(mPager);
return v;
}
Run Code Online (Sandbox Code Playgroud)
这是view_pager_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
这是menu_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/resHeader"
android:layout_width="wrap_content"
android:layout_height="100dp" >
<com.platey.IonPlatey.RectangleImageView
android:id="@+id/restaurant_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<ImageView
android:src="@drawable/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<TextView
android:id="@+id/resType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="15dp"
android:text="ResType Here"
android:textColor="#d82727"
android:textStyle="bold" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
基本上这个问题是由ViewPager android:layout_height="match_parent".
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
这match_parent肯定是错误的,因为你不希望标题填满整个父级.但不管怎样 - match_parent不会在这里工作StaggeredGridView.
这与标准ListView物品等相同- match_parent不允许,只有wrap_content在执行布局时才会被替换.不幸的wrap_content是不支持ViewPager因为ViewPager无法包装它的内容(它不知道孩子的大小).
您有两种方法可以正确显示ViewPager那里:
50dp)ViewPager如果您不想要静态高度,请扩展并执行自己的测量.这里也是截图match_parent.您可以看到ViewPager也未显示(高度为零):

并且具有静态高度:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="50dp" />
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
900 次 |
| 最近记录: |