查看传呼机的列表视图行项目 [英] View Pager as List View Row Item
问题描述
我有一个列表视图与20行,我想设置一个视图寻呼机作为一个列表视图中的每一行。作为项目进来我的列表视图可能的一个或一个以上,的行及欲使用视图寻呼机来显示列表视图行项目
I have a list view with 20 rows and I want to setup a view pager as each row in a list view. As the items coming in my row of the list view may one or more than one, and I want to show the list view row items using a view pager.
有关这一点,我现在用的是下面的code:
For this I am using the following code:
自定义布局将被显示在列表视图行(如寻呼机项目)
<?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" />
有查看传呼机主列表视图自定义行项目
<?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;
}
我越来越没有错误,这是运行IT总是空后呈现的视图。任何人都可以请帮我什么,我做错了。
I AM GETTING NO ERROR AND THE VIEW WHICH IS RENDERED AFTER RUNNING IT ALWAYS EMPTY. cAN ANYONE PLEASE HELP ME WHAT I AM DOING WRONG.
谢谢
推荐答案
我也面临着同样的问题,由 getView()
方法。
I have faced the same issue and resolved it by providing id to the viewpager dynamically in the getView()
method.
@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.setId(position + getCount());**
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
return convertView;
}
希望这可以帮助你。
Hope this may help you.
这篇关于查看传呼机的列表视图行项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!