列表视图行的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/linearLayout_template3">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/template3_textView"/>
<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="200dp"
android:orientation ="horizontal" />
</LinearLayout>
ListView适配器:
这里我使用Holder类来保存convertView的Tag信息。 并且还为viewPager提供了一个唯一的ID。我基本上打算在ViewPager中显示图像,但是要测试我正在使用TextViews的代码。
public class ListViewAdapter extends ArrayAdapter<ParsedItems> {
LinearLayout mainLinnerLayout;
ViewPagerAdapter pagerAdapter;
Context context;
ArrayList<ParsedItems> mObject;
ViewPagerAdapter adapter;
private ProgressDialog progressDialog;
public ListViewAdapter(Context context,int resource, ArrayList<ParsedItems> objects) {
super(context,resource,objects);
this.context = context;
this.mObject = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=null;
ParsedItems items = mObject.get(position);
String template = items.getTemplate();
ParsedItemsImage mInnerItem;
ArrayList<ParsedItemsImage> mInnerItems = items.getItems();
LinearLayout innerLinearLayout=null;
if((Holder)convertView.getTag() == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout_template3, parent,false);
ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.pager);
holder = new Holder();
holder.viewPager = viewPager;
holder.viewPager.setId(NotificationID.getID());
holder.textView = (TextView)convertView.findViewById(R.id.template3_textView);
convertView.setTag(holder);}
else
{
holder = (Holder)convertView.getTag();
}
holder.textView.setText("oyi!");
pagerAdapter = new ViewPagerAdapter(mInnerItems,context,convertView);
holder.viewPager.setAdapter(pagerAdapter);
return convertView;
}
class Holder
{
TextView textView;
ViewPager viewPager;
}
}
ViewPagerAdapter类:
public class ViewPagerAdapter extends PagerAdapter {
ArrayList<ParsedItemsImage> mInnerItems;
Context context;
private final WeakReference<View> ref;
public ViewPagerAdapter(ArrayList<ParsedItemsImage> items,Context context, View view)
{
mInnerItems = new ArrayList<ParsedItemsImage>(items);
this.context = context;
ref = new WeakReference<View>(view);
}
@Override
public int getCount() {
//return mInnerItems.size();
return 6;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return false;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View collection;
ParsedItemsImage mParsedItemsImage = mInnerItems.get(position);
String url=mParsedItemsImage.getImage();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
collection = inflater.inflate(R.layout.template3_imageview,container,false);
TextView textView = (TextView)collection.findViewById(R.id.text_temp3);
ViewPager vp = (ViewPager)container;
textView.setText("test");
vp.addView(collection);
return textView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((TextView) object);
}
}
Template3_imageview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text_temp3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/imageView_template3"/>
</LinearLayout>
因此输出显示在ArrayAdapter的getView()方法中添加的textview:
holder.textView.setText("oyi!");
但未显示ViewPager适配器中的那个:
textView.setText("test");
请帮忙。
P.S。:我打算最后添加图片,但我正在测试是否在寻呼机中添加了文本视图。所以你会在帖子中看到一些额外的代码。
答案 0 :(得分:0)
错误发生在isViewFromObject()方法中。我改变了这个
@Override
public boolean isViewFromObject(View view, Object o) {
return false;
}
到
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
它有效!