您的位置:首页 > 移动开发 > Android开发

Android网易评论盖楼效果实现

2013-01-08 16:44 495 查看
先来张效果图:



下面是一个主要的方法:

/**
* 递归加载楼层的方法
*
* @param context上下文的对像
* @param 递归的控制参数
*            ,同时也是取用户评论信息和背景色的下标,引参数的大小必须是从集合中获得的用户名数组或从集合中获得的评论内容数据的大小减一
* @param pad
*            楼层的间距
* @param strs
*            用户名的字符串数组
* @param strs1
*            用户相应评论内容的字符串数组
* @param color
*            背景色的数组,实际应用的时候这个参数可以不用,用一张背景图片代替就行
* @return 返回一个楼层的LinearLayout布局对象
*/
private LinearLayout add(Context context, int i, int pad, String[] strs,
String[] strs1, int[] color) {
// 加载一个布局
LinearLayout layout1 = (LinearLayout) LayoutInflater.from(context)
.inflate(R.layout.add, null);
// 获得显示用户名、楼层数、用户评论内容的TextView
TextView name = (TextView) layout1.findViewById(R.id.add_textView01);
TextView page = (TextView) layout1.findViewById(R.id.add_textView02);
TextView comment = (TextView) layout1.findViewById(R.id.add_textView03);
// 设置显示用户名、楼层数、用户评论内容TextView的内容
name.setText(strs[i]);
page.setText((i + 1) + "");
comment.setText(strs1[i]);
// 动态生成一个LinearLayout来装载获得的布局
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(color[i]);
layout.setPadding(pad, pad, pad, pad);
// 当i的值为零时,递归结束
if (i != 0) {
layout.addView(add(context, --i, pad, strs, strs1, color));
}
layout.addView(layout1);
return layout;
}


下面是add.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/add_textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:text="sd" />

<TextView
android:id="@+id/add_textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="1" />
</RelativeLayout>

<TextView
android:id="@+id/add_textView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="sfsd" />

</LinearLayout>

下面附上Adapter的代码,Activity的布局和数据的添加自已想:

public class CommentAdapter extends BaseAdapter {

private Context context;
private List<Map<String, Object>> mAryList;
private int[] color = new int[] { Color.CYAN, Color.RED, Color.BLUE,
Color.BLACK, Color.DKGRAY, Color.GREEN, Color.LTGRAY,
Color.MAGENTA, Color.WHITE, Color.YELLOW };
private int pad = 2;

public CommentAdapter(Context context, List<Map<String, Object>> strings) {
this.context = context;
this.mAryList = strings;
}

@Override
public int getCount() {
return mAryList != null ? mAryList.size() : 0;
}

@Override
public Object getItem(int position) {
return mAryList != null ? mAryList.get(position) : null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.main_item, null);
holder.layout = (LinearLayout) convertView
.findViewById(R.id.main_linearLayout);
holder.lastComment = (TextView) convertView
.findViewById(R.id.main_textView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
// 此处清除子View
holder.layout.removeAllViews();
}
holder.lastComment.setText(mAryList.get(position).get("lastComment")
.toString());
String[] strs = (String[]) mAryList.get(position).get("name");
String[] strs1 = (String[]) mAryList.get(position).get("comment");
holder.layout.addView(add(context, (strs.length - 1), pad, strs, strs1,
color));
convertView.setBackgroundColor(Color.TRANSPARENT);
return convertView;
}

/**
* 递归加载楼层的方法
*
* @param context上下文的对像
* @param 递归的控制参数
*            ,同时也是取用户评论信息和背景色的下标,引参数的大小必须是从集合中获得的用户名数组或从集合中获得的评论内容数据的大小减一
* @param pad
*            楼层的间距
* @param strs
*            用户名的字符串数组
* @param strs1
*            用户相应评论内容的字符串数组
* @param color
*            背景色的数组,实际应用的时候这个参数可以不用,用一张背景图片代替就行
* @return 返回一个楼层的LinearLayout布局对象
*/
private LinearLayout add(Context context, int i, int pad, String[] strs,
String[] strs1, int[] color) {
LinearLayout layout1 = (LinearLayout) LayoutInflater.from(context)
.inflate(R.layout.add, null);
TextView name = (TextView) layout1.findViewById(R.id.add_textView01);
TextView page = (TextView) layout1.findViewById(R.id.add_textView02);
TextView comment = (TextView) layout1.findViewById(R.id.add_textView03);
name.setText(strs[i]);
page.setText((i + 1) + "");
comment.setText(strs1[i]);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(color[i]);
layout.setPadding(pad, pad, pad, pad);
if (i != 0) {
layout.addView(add(context, --i, pad, strs, strs1, color));
}
layout.addView(layout1);
return layout;
}

private class ViewHolder {
LinearLayout layout;
TextView lastComment;
}
}


工程的压缩包可以到我的资源里面下载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐