我希望在列表视图中插入额外的元素。 例如,我有无限的图像列表视图,在每20张图像后,我希望添加带有源页面链接的webview。
根据sugested
不幸在WebView sourse = (WebView) vi.findViewById(R.id.webViewSourse);
public class MediaItemAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public MediaItemAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
@Override
public int getViewTypeCount() {
return 2; // any number what you need.
}
@Override
public int getItemViewType(int position) {
// view type is managed as zero-based index.
if (position % 11 != 0)
return 0;
else
return 1;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
// View Recycling is managed separately based on its view type,
// so you don't need to worry about view corruptions.
int type = getItemViewType(position);
switch (type) {
case 0:
// inflate imageview here.
convertView = inflater.inflate(R.layout.item_composer, null);
case 1:
// inflate webview here.
convertView = inflater.inflate(R.layout.banner_row, null);
}
}
int type = getItemViewType(position);
switch (type) {
case 0:
// inflate imageview here.
ImageView thumb_image = (ImageView) vi.findViewById(R.id.imagePrev); // thumb
String value = add.get("first_photo_url");
if (value == null || value.trim().length() <= 0
|| value.equalsIgnoreCase("null")
|| value.trim().equalsIgnoreCase("")) {
thumb_image.setImageResource(R.drawable.stub);
} else {
imageLoader.DisplayImage(add.get("first_photo_url"),
thumb_image);
// // do nothing
}
case 1:
// inflate webview here.
WebView sourse = (WebView) vi.findViewById(R.id.webViewSourse);
WebSettings webSettings = sourse.getSettings();
webSettings.setJavaScriptEnabled(true);
// banner.loadUrl("http://www.google.com");
sourse.loadUrl(url);
}
return vi;
}
}
答案 0 :(得分:2)
ListView支持多个ViewType。你可以覆盖getViewTypeCount&amp; ListAdapter中的getItemViewType方法。在您的情况下,您可以准备两种视图类型,一种用于图像,另一种用于webvie,并根据项目的位置单独使用。例如......
private class MyCustomAdapter extends ArrayAdapter<Object> {
public MyCustomAdapter(Context context) {
super(context, 0);
}
@Override
public int getViewTypeCount() {
return 2; //any number what you need.
}
@Override
public int getItemViewType(int position) {
//view type is managed as zero-based index.
if(position % 20 != 0)
return 0;
else
return 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView == null ){
// View Recycling is managed separately based on its view type,
// so you don't need to worry about view corruptions.
LayoutInflater inflater = getLayoutInflater();
int type = getItemViewType(position);
switch (type){
case 0:
//inflate imageview here.
convertView = inflater.inflate(R.layout.my_imageivew);
break;
case 1:
//inflate webview here.
convertView = inflater.inflate(R.layout.my_webview);
}
//do some data binding job here...
}
return convertView;
}
}//end of inner class
我发现了一篇有关此内容的有用博文 - “handling-listviews-with-multiple-row-types”
答案 1 :(得分:0)
在getView方法的列表适配器中,您可以检查以下条件:
if(position%20==0){
//return your webview here instead of imageView or list row etc.
}