Android listview多视图嵌套多视图

首先给大家展示下效果图:

public class HomeEduMoreAdapter extends BaseAdapter {
private final String TAG = "HomeEduMoreAdapter";
private static final int TYPE_COMMON = ;
private static final int TYPE_BL = ;
private static final int TYPE_NONE = ;
private static final int TYPE_THREE = ;
private static final int TYPE_QUESTION = ;
private int TYPE_COUNT = ;
private LayoutInflater inflater;
private List<HomeEdus> list;
private Context context;
private DisplayImageOptions options;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
public HomeEduMoreAdapter(Context context, List<HomeEdus> objects) {
this.context = context;
// groupUserDao = new GroupUserDao(context);
inflater = LayoutInflater.from(context);
list = objects;
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.bitmapConfig(Bitmap.Config.RGB_).cacheOnDisk(true)
.showImageOnLoading(R.drawable.icon_default)// 设置图片在下载期间显示的图片
.showImageForEmptyUri(R.drawable.icon_default)// 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.icon_default)// 设置图片加载/解码过程中错误时候显示的图片
.considerExifParams(true).build();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return TYPE_COUNT;
}
@Override
public HomeEdus getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int postion) {
// TODO Auto-generated method stub
return postion;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
HomeEdus edus = getItem(position);
String type = edus.getType();// 菜单类型
int result = TYPE_COMMON;
switch (type) {
case "hx":
result = TYPE_COMMON;
break;
case "wd":
if (edus.getPic() == null || edus.getPic().equals("")) {
result = TYPE_QUESTION;
}else {
result = TYPE_COMMON;
}
break;
case "jt":
result = TYPE_COMMON;
break;
case "bl":
result = TYPE_BL;
break;
default:
String jx = edus.getType();// 精选内容类型
switch (jx) {
default:
Integer imgNum = Integer.parseInt(edus.getImgNum());
if (imgNum != null && imgNum > && imgNum < ) {
result = TYPE_COMMON;
} else if (imgNum != null && imgNum >= ) {
result = TYPE_THREE;
} else {
result = TYPE_NONE;
}
break;
}
break;
}
return result;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final HomeEdus model = getItem(position);
int type = getItemViewType(position);
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
int width = manager.getDefaultDisplay().getWidth();
int height = manager.getDefaultDisplay().getWidth() / ;
switch (type) {
case TYPE_COMMON:
convertView = inflater.inflate(R.layout.item_home_edus, null);
holder.tv_content = (QSTextView) convertView
.findViewById(R.id.tv_content);
holder.tv_content.setTextColor(Color.GRAY);
holder.tv_title = (QSTextView) convertView
.findViewById(R.id.tv_title);
holder.tv_name = (TextView) convertView
.findViewById(R.id.tv_name);
// holder.img_head = (CircleImageView) convertView
// .findViewById(R.id.img_head);
holder.img_content = (ImageView) convertView
.findViewById(R.id.img_content);
holder.tv_read = (TextView) convertView
.findViewById(R.id.tv_read);
holder.tv_comment = (TextView) convertView
.findViewById(R.id.tv_comment);
holder.tv_thumbupNum = (TextView) convertView
.findViewById(R.id.tv_thumbupNum);
break;
case TYPE_QUESTION:
convertView = inflater.inflate(R.layout.item_home_question, null);
holder.tv_content = (QSTextView) convertView
.findViewById(R.id.tv_content);
holder.tv_content.setTextColor(Color.GRAY);
holder.tv_title = (QSTextView) convertView
.findViewById(R.id.tv_title);
holder.tv_name = (TextView) convertView
.findViewById(R.id.tv_name);
holder.tv_read = (TextView) convertView
.findViewById(R.id.tv_read);
holder.tv_comment = (TextView) convertView
.findViewById(R.id.tv_comment);
holder.tv_thumbupNum = (TextView) convertView
.findViewById(R.id.tv_thumbupNum);
break;
case TYPE_BL:
case TYPE_NONE:
convertView = inflater.inflate(R.layout.item_home_case, null);
holder.tv_content = (QSTextView) convertView
.findViewById(R.id.tv_content);
holder.tv_content.setTextColor(Color.GRAY);
holder.tv_title = (QSTextView) convertView
.findViewById(R.id.tv_title);
holder.tv_name = (TextView) convertView
.findViewById(R.id.tv_name);
holder.img_content = (ImageView) convertView
.findViewById(R.id.img_content);
LayoutParams para = holder.img_content.getLayoutParams();
para.height = height;
para.width = width;
holder.img_content.setLayoutParams(para);
holder.tv_read = (TextView) convertView
.findViewById(R.id.tv_read);
holder.tv_comment = (TextView) convertView
.findViewById(R.id.tv_comment);
holder.tv_thumbupNum = (TextView) convertView
.findViewById(R.id.tv_thumbupNum);
break;
case TYPE_THREE:
convertView = inflater.inflate(R.layout.item_home_three, null);
holder.tv_content = (QSTextView) convertView
.findViewById(R.id.tv_content);
holder.tv_content.setTextColor(Color.GRAY);
holder.tv_title = (QSTextView) convertView
.findViewById(R.id.tv_title);
holder.tv_name = (TextView) convertView
.findViewById(R.id.tv_name);
holder.img_content = (ImageView) convertView
.findViewById(R.id.img_content);
holder.img_content = (ImageView) convertView
.findViewById(R.id.img_content);
holder.img_content = (ImageView) convertView
.findViewById(R.id.img_content);
int w = width / ;
LayoutParams p = holder.img_content.getLayoutParams();
p.height = (int)(w*.f);
p.width = w;
holder.img_content.setLayoutParams(p);
p = holder.img_content.getLayoutParams();
p.height = (int)(w*.f);
p.width = w;
holder.img_content.setLayoutParams(p);
p = holder.img_content.getLayoutParams();
p.height = (int)(w*.f);
p.width = w;
holder.img_content.setLayoutParams(p);
holder.tv_read = (TextView) convertView
.findViewById(R.id.tv_read);
holder.tv_comment = (TextView) convertView
.findViewById(R.id.tv_comment);
holder.tv_thumbupNum = (TextView) convertView
.findViewById(R.id.tv_thumbupNum);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (type) {
case TYPE_COMMON:
case TYPE_BL:
setCommon(holder, model);
break;
case TYPE_QUESTION:
setQuestion(holder,model);
break;
case TYPE_NONE:
setCase(holder, model);
break;
case TYPE_THREE:
setThree(holder, model);
break;
}
return convertView;
}
private void setCommon(ViewHolder holder, HomeEdus entity) {
String content = entity.getDesc();
int clength = content.length();
if (clength > ) {
content = content.substring(, ) + "...";
}
holder.tv_content.setMText(content);
String title = entity.getTitle();
int tlength = title.length();
if (tlength > ) {
title = title.substring(, ) + "...";
}
holder.tv_title.setMText(title);
// int height = holder.tv_title.getHeight();
holder.tv_thumbupNum.setText(entity.getThumbupNum());
holder.tv_read.setText(entity.getReadNum());
holder.tv_comment.setText(entity.getCommentNum());
holder.tv_name.setText(entity.getSourceName());
// if (entity.getHead_img() != null && !entity.getHead_img().equals(""))
// {
// ImageLoader.getInstance().displayImage(entity.getHead_img().trim(),
// holder.img_head, options, animateFirstListener);
// } else {
// holder.img_head.setImageResource(R.drawable.pic_head_none_login);
// }
if (entity.getPic() != null
&& !entity.getPic().equals("")) {
holder.img_content.setBackground(null);
ImageLoader.getInstance().displayImage(
entity.getPic().trim(), holder.img_content,
options, animateFirstListener);
}
}
private void setCase(ViewHolder holder, HomeEdus entity) {
String content = entity.getDesc();
int clength = content.length();
if (clength > ) {
content = content.substring(, ) + "...";
}
holder.tv_content.setMText(content);
String title = entity.getTitle();
int tlength = title.length();
if (tlength > ) {
title = title.substring(, ) + "...";
}
holder.tv_title.setMText(title);
// int height = holder.tv_title.getHeight();
holder.tv_thumbupNum.setText(entity.getThumbupNum());
holder.tv_read.setText(entity.getReadNum());
holder.tv_comment.setText(entity.getCommentNum());
holder.tv_name.setText(entity.getSourceName());
if (entity.getPic() != null
&& !entity.getPic().equals("")) {
holder.img_content.setBackground(null);
ImageLoader.getInstance().displayImage(
entity.getPic().trim(), holder.img_content,
options, animateFirstListener);
}
}
private void setThree(ViewHolder holder, HomeEdus entity) {
String content = entity.getDesc();
int clength = content.length();
if (clength > ) {
content = content.substring(, ) + "...";
}
holder.tv_content.setMText(content);
String title = entity.getTitle();
int tlength = title.length();
if (tlength > ) {
title = title.substring(, ) + "...";
}
holder.tv_title.setMText(title);
// int height = holder.tv_title.getHeight();
holder.tv_thumbupNum.setText(entity.getThumbupNum());
holder.tv_read.setText(entity.getReadNum());
holder.tv_comment.setText(entity.getCommentNum());
holder.tv_name.setText(entity.getSourceName());
holder.img_content.setImageResource(R.drawable.icon_default);
holder.img_content.setImageResource(R.drawable.icon_default);
holder.img_content.setImageResource(R.drawable.icon_default);
if (entity.getPic() != null
&& !entity.getPic().equals("")) {
holder.img_content.setBackground(null);
ImageLoader.getInstance().displayImage(
entity.getPic().trim(), holder.img_content,
options, animateFirstListener);
}
if (entity.getPic() != null
&& !entity.getPic().equals("")) {
holder.img_content.setBackground(null);
ImageLoader.getInstance().displayImage(
entity.getPic().trim(), holder.img_content,
options, animateFirstListener);
}
if (entity.getPic() != null
&& !entity.getPic().equals("")) {
holder.img_content.setBackground(null);
ImageLoader.getInstance().displayImage(
entity.getPic().trim(), holder.img_content,
options, animateFirstListener);
}
}
private void setQuestion(ViewHolder holder, HomeEdus entity) {
String content = entity.getDesc();
int clength = content.length();
if (clength > ) {
content = content.substring(, ) + "...";
}
holder.tv_content.setMText(content);
String title = entity.getTitle();
int tlength = title.length();
if (tlength > ) {
title = title.substring(, ) + "...";
}
holder.tv_title.setMText(title);
// int height = holder.tv_title.getHeight();
holder.tv_thumbupNum.setText(entity.getThumbupNum());
holder.tv_read.setText(entity.getReadNum());
holder.tv_comment.setText(entity.getCommentNum());
holder.tv_name.setText(entity.getSourceName());
}
public void setList(List<HomeEdus> list) {
this.list.clear();
this.list.addAll(list);
notifyDataSetChanged();
}
static class ViewHolder {
public QSTextView tv_title;
public QSTextView tv_content;
public ImageView img_content;
public TextView tv_name;
public TextView tv_read;
public TextView tv_comment;
public TextView tv_thumbupNum;
public ImageView img_content;
public ImageView img_content;
public ImageView img_content;
}
private static class AnimateFirstDisplayListener extends
SimpleImageLoadingListener {
static final List<String> displayedImages = Collections
.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, );
displayedImages.add(imageUri);
}
}
}
}
}
(0)

相关推荐

  • 探讨:如何在ScrollView中嵌套ListView

    一.分析 1.最简单的布局:只有一个ListView 如果整个页面只有一个ListView的话,那么由于ListView本身带有滚动效果,所以当加载的数据超过页面显示的范围时,可以通过上下滑动来查看所有的item.因此这种情况下,不需要添加ScrollView. 2.其它布局A+ListView 这种情况下,如果布局A定义在ListView的前面,那么当布局A所占的比例较大,或者ListView加载的数据较多时,都会导致ListView显示不完全.同样,由于ListView自身可以滚动,因此仍然

  • Android之ScrollView嵌套ListView和GridView冲突的解决方法

    那么里面的ScrollView高度计算就会出现问题.我们也就无法得到想要的效果.核心解决方案: 重写ListView或者GridView的OnMesure 方法. 复制代码 代码如下: public class MyListView extends ListView {        public MyListView(Context context) {                super(context);        }        public MyListView(Conte

  • ScrollView嵌套ListView滑动冲突的解决方法

    ScrollView和ListView这两个控件想必大家都不会陌生,但是这两者嵌套使用的时候就会出现麻烦.比如,我们如果想在ListView下面添加其他的布局或者控件,然后想让它们作为一个整体都可以滑动的话,最常想到的就是用一个ScrollView把它们包裹起来.想法似乎很美好,但是现实就有点残酷了.我们可以写一个小例子体验一下. 首先创建一个Activity,在它的布局文件上放置一个ListView: <?xml version="1.0" encoding="utf

  • Android中listview嵌套scrollveiw冲突的解决方法

    一.使用网上用的动态改变listview高度的方法 该方法只适用于item布局是LinearLayout布局的情况,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常.所以使用限制较大. public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { //获取ListView对应

  • 关于Android中ListView嵌套GridView的问题

    问题 在Android开发中,遇到一个问题,是ListView嵌套GridView,需要点击整个ListView的Item进行跳转.但是在点击GridView区域时无法进行页面的跳转.这是因为GridView获得了焦点.导致点击无法跳转. 解决方法就是: 1.在Item最外层加上 android:descendantFocusability="blocksDescendants" 2.在Adapter中添加 holder.mGridView.setClickable(false); h

  • Android滑动冲突的完美解决

    Android滑动在智能手机上是必备的操作,但是在开发的时候,你是否和我一样,经常会遇到滑动冲突的问题,比如最简单需要在ListView里面添加一个侧滑动作,这时候冲突时必然的,那我们该如何解决这个问题呢? 先来说一下滑动冲突都有那些,该怎么解决. 场景一:类似于ViewPager嵌套Fragmnet并且在Fragmnet中嵌套了一个ListView的效果,可以通过左右滑动来切换或者触发其他view的显示.但是在ViewPager内部已经处理了这个冲突,所以我们会发现ViewPager嵌套Fra

  • Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑

    使用了RecyclerView嵌套RecyclerView的方案. 购物车的第一个界面为RecyclerView,每个Item里面包含一个店铺.在Item中使用RecyclerView包含店铺和店铺的多个商品. 实现思路: 使用接口回调将第二个adapter的商品选择的监听事件回调给第一个adapter后再在第一个adapter中回调给MainActivity. 使用接口回调将第一个adapter的商品选择的监听事件回调给MainActivity. 在MainActivity中处理第一个adap

  • Android笔记之:在ScrollView中嵌套ListView的方法

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListView会显示不完全,它的高度始终有问题.上网查了下,发现别人也有遇到这样的问题,而大多数人都不推荐这样的设计,因为默认情况下Android是禁止在ScrollView中放入另外的ScrollView的,它的高度是无法计算的. 又搜索了一下,发现有StackOverflow上的牛人已经解决了这个问题,经过

  • Android ListView的item中嵌套ScrollView的解决办法

    前沿:有时候,listview 的item要显示的字段比较多,考虑到显示问题,item外面不得不嵌套ScrollView来实现,于是问题来了,当listview需要做点击事件时,由于ScrollView的嵌套使用,拦截了listvew点击事件:只好重写listview来实现了. /** * * @author 作者:易皇星 * * @da2016年10月24日 时间: * * @toTODO 类描述: 解决 ListView中嵌套ScrollView,ScrollView拦截ListView的I

  • ListView嵌套GridView使用详解及注意事项

    MainActivity如下: 复制代码 代码如下: package cn.testlistviewandgridview; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; /** * Demo描述: * ListView嵌套GridView使用详解 * 即List

随机推荐