Android网易评论盖楼效果实现
16lz
2022-01-03
先来张效果图:
下面是一个主要的方法:
/** * 递归加载楼层的方法 * * @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);// 获得显示用户名、楼层数、用户评论内容的TextViewTextView 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;}@Overridepublic int getCount() {return mAryList != null ? mAryList.size() : 0;}@Overridepublic Object getItem(int position) {return mAryList != null ? mAryList.get(position) : null;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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();// 此处清除子Viewholder.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平台上的JNI技术介绍
- Android(安卓)3D 游戏学习笔记(4)-光源
- Android中实现Broastcast接收短信
- Android(安卓)SparseArray与HashMap与ArrayMap的性能差别
- android传输视频到PC
- Android高效编程注意事项
- android 蓝牙、低功耗BLE开发问题总结
- Android(安卓)通过AudioRecord实时录音并转AAC
- android中调用requestFocus()的详细过程