Android网易评论盖楼效果实现
2013-01-08 16:44
495 查看
先来张效果图:
下面是一个主要的方法:
下面是add.xml文件:
下面附上Adapter的代码,Activity的布局和数据的添加自已想:
工程的压缩包可以到我的资源里面下载!
下面是一个主要的方法:
/** * 递归加载楼层的方法 * * @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; } }
工程的压缩包可以到我的资源里面下载!
相关文章推荐
- Android网易评论盖楼效果实现
- Android 使用ListView实现网易评论盖楼效果
- 【凯子哥带你做高仿】“煎蛋”Android版的高仿及优化(二)——大图显示模式、评论“盖楼”效果实现详解
- 【凯子哥带你做高仿】“煎蛋”Android版的高仿及优化(二)——大图显示模式、评论“盖楼”效果实现详解
- 网易评论盖楼效果前台实现。。。
- 【凯子哥带你做高仿】“煎蛋”Android版的高仿及优化(二)——大图显示模式、评论“盖楼”效果实现详解
- Android网易新闻评论盖楼效果的实现
- Android用TabLayout实现类似网易选项卡动态滑动效果
- php配合ajax实现仿网易文章评论效果
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。 2014-07-04 13:17 78人阅读 评论(0) 收藏
- Android用TabLayout实现类似网易选项卡动态滑动效果
- dedecms 评论盖楼实现楼层数,类似腾讯、网易的评论(5.5/5.6版)
- Android Design Support Library(一)用TabLayout实现类似网易选项卡动态滑动效果
- Android ScrollView滚动实现大众点评、网易云音乐评论悬停效果
- Android用TabLayout实现类似网易选项卡动态滑动效果
- Android评论工具类,实现QQ空间文字评论效果
- 仿网易163新闻评论盖楼效果代码
- Android广告条效果--使用Volley实现网络图片的自动轮播(仿淘宝、网易广告页轮播效果)
- Android Design Support Library(一)用TabLayout实现类似网易选项卡动态滑动效果
- Android Design Support Library(一)用TabLayout实现类似网易选项卡动态滑动效果