动态设置查看寻呼机高度

时间:2013-11-22 09:22:30

标签: android height android-viewpager

我将View Pager作为List Item行,我将数据设置到寻呼机的适配器以显示在列表视图中。这是我的代码:

Pager_Item Layout

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inner_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6.0dip"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:text="Example Value"
        android:textAppearance="?android:textAppearanceMedium" />

 </LinearLayout>

列出视图行项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

ListView GetView

 ViewHolder holder;
    if(rowView==null){
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.horizontal_list_item, parent,false);
        MyPagerAdapter adapter = new MyPagerAdapter(context);
        ViewPager myPager = (ViewPager) rowView.findViewById(R.id.mypager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(4, true);

        rowView.setTag(holder);
    }
    else{
        holder = (ViewHolder) rowView.getTag();
    }

MyPagerAdapter

@Override
 public Object instantiateItem(View container, final int position) {
LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(
        R.layout.inner_layout_file, null);

TextView tv_view = (TextView) layout.findViewById(R.id.text);
tv_view.setText("Example Item " + position);
((ViewPager) container).addView(layout);

});
return layout;

问题:我面临的问题是,当我在XML文件中修复View Pager的高度时,我的代码会显示数据,否则如果我将寻呼机的高度设置为WRAP_CONTENT或MATCH_PARENT,屏幕上代码的运行时没有显示任何视图。

为什么它表现得像这样。我不想硬编码View Pager的高度。

请帮助我,如果您有任何疑问,请与我联系?

2 个答案:

答案 0 :(得分:1)

在你的实例化项目中,使用容器作为你的View Pager,在其上设置布局参数

答案 1 :(得分:0)

当然,最好的解决方案是使用自定义的viewpager,例如https://stackoverflow.com/a/34524240/3232778

enter image description here

我希望它会对你有所帮助。

相关问题