将canvas添加到expandablelistview中

时间:2012-02-28 06:24:43

标签: android android-layout android-canvas

我正在尝试将画布添加到我的可扩展列表视图中,但我无法在无尽的搜索之后更接近它。这是我的可扩展列表代码。很抱歉让它变得混乱,但我无法改善它。现在我的脑袋真是油炸了

package com.android.nestedexplist;

ExpandableListAdapter mAdapter,cAdapter;
Canvas canvas;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // Set up our adapter

    mAdapter = new MyExpandableListAdapter();
    setListAdapter(mAdapter);
    //setListAdapter(cAdapter);
    registerForContextMenu(getExpandableListView());
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Sample menu");
    menu.add(0, 0, 0, R.string.expandable_list_sample_action);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();

    String title = ((TextView) info.targetView).getText().toString();

    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
        int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
        Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
                Toast.LENGTH_SHORT).show();
        return true;
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
        Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
        return true;
    }

    return false;
}

/**
 * A simple adapter which maintains an ArrayList of photo resource Ids. 
 * Each photo is displayed as an image. This adapter supports clearing the
 * list of photos and adding a new photo.
 *
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };



    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,256);

        TextView textView = new TextView(NestedexplistActivity.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);



        return textView;
    }




    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
           View convertView, ViewGroup parent) {
       TextView textView = getGenericView();

       textView.setText(getChild(groupPosition, childPosition).toString());
       return textView;
    }



    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public TextView getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
    TextView textView =getCustomDemoView();
    return textView;  
    }

    public TextView getCustomDemoView()
    {
         AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                 ViewGroup.LayoutParams.MATCH_PARENT, 64);
    TextView textView= new TextView(NestedexplistActivity.this);    
    textView.setLayoutParams(lp);
    DemoView graph=new Demoview();

    return textView;    
    }



    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }



    public TextView getCustomView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(NestedexplistActivity.this);
        textView.setLayoutParams(lp);

        textView.setPadding(36, 0, 0, 0);
        return textView;

    }

}

这是我想在expandableListview

中显示的画布数据
public DemoView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

protected  void onDraw(Canvas canvas)
{   
    super.onDraw(canvas);
    int[] x= {10,20,30,40,50,60,70,80};

    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.FILL);
    paint.setAlpha(255);
    canvas.drawPaint(paint);
    paint.setStrokeWidth(20);
    paint.clearShadowLayer();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);

    int i =0;
        canvas.drawLine(100, (50*(i+1)), 100+(x[i]*10),(50*(i+1)), paint);
        String s=""+x[i]+"%"; 
        canvas.drawText(s,100+ x[i]*10+5,(50*(i+1))+5 , paint);


    paint.setColor(Color.BLACK);
    canvas.drawText("Total", 10, (50), paint);

    }

请帮助!!

0 个答案:

没有答案