Android网易评论盖楼效果实现

本文介绍了一种使用递归方法动态加载楼层布局的技术,通过LinearLayout实现楼层效果,并展示了具体的实现代码,包括递归方法和Adapter的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


下面是一个主要的方法:

    /** 
         * 递归加载楼层的方法 
         *  
         * @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;  
        }  
    }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值