【发布时间】:2013-11-22 09:22:29
【问题描述】:
我将 View Pager 作为列表项行,我正在将数据设置到寻呼机的适配器以显示在列表视图中。这是我的代码:
Pager_Item 布局
<?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 获取视图
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 的高度。
请帮助我,如果您有任何疑问,请告诉我?
【问题讨论】:
-
你能做到吗?
-
是的,我已经成功了
-
您认为您可以发布解决方案吗?我认为这会很有用:)
-
其实我并没有使用这个概念来做我的工作,我现在也使用了固定高度。解决方案是我已经完成的另一个问题。所以和上面的onne没有关系
标签: android height android-viewpager