我有一个包含 20 行的列表视图,我想将视图寻呼机设置为列表视图中的每一行。由于列表视图行中的项目可能一个或多个,我想使用视图寻呼机显示列表视图行项目。
为此,我使用以下代码:
将在列表视图行中显示的自定义布局(作为分页器项目)
<?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" />
具有 View Pager 的主列表视图自定义行项目
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
android:id="@+id/mypager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MyPagerAdapter 类
public class MyPagerAdapter extends PagerAdapter {
private Context context;
public MyPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 4;
}
@Override
public Object instantiateItem(View container, int position) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = inflater.inflate(R.layout.inner_layout_file, null);
((ViewPager) container).addView(view, position);
return view;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
}
列表视图适配器获取视图方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);
MyPagerAdapter adapter = new MyPagerAdapter(context);
ViewPager myPager = (ViewPager) convertView.findViewById(R.id.mypager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
return convertView;
}
我没有收到任何错误,运行后呈现的视图始终为空。