when I try to add viewgroup as the listview's item.  the listview will
not visiable.
follow is the code:

package com.test.mylistview;

import java.util.ArrayList;

import android.app.Activity;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class TestListViewActivity extends Activity {
        private static final String TAG = "TestCWActivity";
        private final static int VIEW_MARGIN = 2;
        private int lastPosition = -1 ;

        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                setTvList();
                ListView ltv = (ListView) findViewById(R.id.listview);
                ltv.setAdapter(new ListAdapt());
        }

        class ListAdapt extends BaseAdapter{

                @Override
                public int getCount() {
                        // TODO Auto-generated method stub
                        return tvlst.size();
                }

                @Override
                public Object getItem(int position) {
                        // TODO Auto-generated method stub
                        return null;
                }

                @Override
                public long getItemId(int position) {
                        // TODO Auto-generated method stub
                        return 0;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup
parent) {
                        // TODO Auto-generated method stub
                        SelectionExam selectionExam;
            if (convertView == null){
                selectionExam = new SelectionExam(TestCWActivity.this);
                if((lastPosition == -1 || lastPosition != position) &&
(lastPosition < 9)){
                        ImageView img = new ImageView(TestCWActivity.this);
                        img.setBackgroundColor(0xffeeff);
                        selectionExam.addView(img,new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams
                                                .WRAP_CONTENT));
                        selectionExam.addView(tvlst.get(position),new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams
                                                .WRAP_CONTENT));
                        Log.i(TAG, "add textview--->"+tvlst.get(position));
                        lastPosition = position;
                }
            }else{
                selectionExam = (SelectionExam) convertView;
                if((lastPosition == -1 || lastPosition != position) &&
(lastPosition < 9)){
                        ImageView img = new ImageView(TestCWActivity.this);
                        img.setBackgroundColor(0xffeeff);
                        selectionExam.addView(img,new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams
                                                .WRAP_CONTENT));
                        selectionExam.addView(tvlst.get(position),new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams
                                                .WRAP_CONTENT));
                        Log.i(TAG, "add textview--->"+tvlst.get(position));
                        lastPosition = position;
                }
            }

                        return selectionExam;
                }

        }

        ArrayList<TextView> tvlst = new ArrayList<TextView>();
        private void setTvList(){
                for(int i=10; i<20; i++){
                        TextView tv = new TextView(this);
                        tvlst.add(tv);
                }
        }

        class SelectionExam extends ViewGroup {

                public SelectionExam(Context context) {
                        super(context);
                }

                protected void onMeasure(int widthMeasureSpec, int
heightMeasureSpec) {
                        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//                      Log.d(TAG, "widthMeasureSpec = " + widthMeasureSpec
//                                      + " heightMeasureSpec = " + 
heightMeasureSpec);
                        for (int index = 0; index < getChildCount(); index++) {
                                final View child = getChildAt(index);
                                child.measure(MeasureSpec.UNSPECIFIED, 
MeasureSpec.UNSPECIFIED);
                        }
                }

                protected void onLayout(boolean changed, int left, int top, int
right,
                                int bottom) {
//                      Log.d(TAG, "changed = " + changed + " left = " + left + 
" top = "
//                                      + top + " right = " + right + " botom = 
" + bottom);
                        final int count = getChildCount();
                        int row = 0;
                        int lengthX = left;
                        int lengthY = top;
                        for (int i = 0; i < count; i++) {
                                final View child = this.getChildAt(i);
                                int width = child.getMeasuredWidth();
                                int height = child.getMeasuredHeight();
                                lengthX += width + VIEW_MARGIN;
                                lengthY = row * (height + VIEW_MARGIN) + 
VIEW_MARGIN + height
                                                + top;
                                if (lengthX > right) {
                                        lengthX = width + VIEW_MARGIN + left;
                                        row++;
                                        lengthY = row * (height + VIEW_MARGIN) 
+ VIEW_MARGIN
                                                        + height + top;
                                }
                                child.layout(lengthX - width, lengthY - height, 
lengthX,
                                                lengthY);
                        }
                }

        }

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to