注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

含头的CheckBox列表的使用实例  

2010-05-19 10:28:00|  分类: 数据适配器 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.sec.android.touchwiz.samples.R;

public class ListLayoutSample2Xml extends Activity {
    
    private static final String TAG = "ListLayoutSample2Xml";
    
    private ListView mList;
    private CheckBox mHeaderAllCheckBox;
    
    private final int AGENDA_NONE = 0;
    private final int AGENDA_CYAN = 1;
    private final int AGENDA_BLUE = 2;
    private final int AGENDA_GREEN = 3;
    private final int AGENDA_PURPLE = 4;
    private final int AGENDA_YELLOW = 5;        
    private final int AGENDA_COBALT = 6;
    
    private final int AGENDA_TAG_ICON_NONE = 100;
    private final int AGENDA_TAG_ICON_GOOGLE = 101;
    private final int AGENDA_TAG_ICON_FACEBOOK = 102;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlayoutsample2xml_main);      

        mList = (ListView)findViewById(R.id.agendaAllList);    
        mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        View allitem = getLayoutInflater().inflate(R.layout.listlayoutsample2xml_allitem, null);
        mHeaderAllCheckBox = (CheckBox)allitem.findViewById(R.id.allitemcheck);
        
        // [09.10.23] newly added!!! allitem clickListener to set check status of all items.
        allitem.setOnClickListener(new OnClickListener() {            
            
            public void onClick(View view) {
                Log.d("all item header", "onClick()");
                
mHeaderAllCheckBox.performClick();
            }
        });
        mHeaderAllCheckBox.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Log.d("all item header checkbox", "onClick()");
                CheckBox checkBox = (CheckBox)view;
                boolean checked = checkBox.isChecked();
                
                setAllItemChecked(checked);

            }
        });
        
        mList.addHeaderView(allitem);    

        mList.post(new Runnable() {
            public void run() {
                updateAllCheckState();
            }
        });
        
        
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View allitem = li.inflate(R.layout.touchwiz3list2_allitem, null);
        
        LinearLayout allitem = (LinearLayout)findViewById(R.id.allitem);
        allitem = new LinearLayout(this);
        if(allitem == null)
            Log.i("TouchWiz3List2","allitemview is null");
        else
        {
            TextView allitemtext = (TextView)findViewById(R.id.allitemtext);
            allitemtext = new TextView(this);
            CheckBox allitemcheck = (CheckBox)findViewById(R.id.allitemcheck);
            allitemcheck = new CheckBox(this);
            
            allitemtext.setText("All calendar");
            allitemtext.setTextSize(36);
            allitemtext.setTextColor(Color.WHITE);
            allitemtext.setPadding(20, allitemtext.getPaddingTop(), allitemtext.getPaddingRight(), allitemtext.getPaddingBottom());      
            
            allitem.setMinimumHeight(90);
            allitem.setGravity(Gravity.CENTER_VERTICAL);
            allitem.setBackgroundDrawable(getResources().getDrawable(R.drawable.winset_selectall_bg));

            allitem.addView(allitemtext);
            allitem.addView(allitemcheck);
            agendaList.addHeaderView(allitem);
        } 
        */           
        
        // AgendaItem type? item? ?? ArrayList? ??
        ArrayList<AgendaItem> list = new ArrayList<AgendaItem>();
        
        // mAgenda ???? for?? ?? ArrayList? item add
        for(int i=0;i<mAgenda.length;i++){
            AgendaItem contact = new AgendaItem(mAgendaColor, mAgendaTagIcon, mAgenda, mTag);
            list.add(contact);
        }      
        
        // AgendaAdapter ??
        AgendaAdapter agendaAdapter = new AgendaAdapter(this, R.layout.listlayoutsample2xml_row, list);
        // ListView? AgendaAdapter ??
        mList.setAdapter(agendaAdapter);
        
        mList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Log.d(TAG,"onItemClick("+position+")");

                updateAllCheckState();
            }
        });
        
    }
    
    /*
     * AgendaItem? mAgendarcolor? ?? display?? drawable
     */
    private Integer[] mAgendaColorDrawable = {
            R.drawable.listlayoutsample2xml_agenda_color01, 
            R.drawable.listlayoutsample2xml_agenda_color02, 
            R.drawable.listlayoutsample2xml_agenda_color03, 
            R.drawable.listlayoutsample2xml_agenda_color04,
            R.drawable.listlayoutsample2xml_agenda_color05, 
            R.drawable.listlayoutsample2xml_agenda_color06};
    
    /*
     * AgendaItem? mAgendatabicon? ?? display?? drawable
     */
    private Integer[] mAgendaTagDrawable = {
            -1, -1, R.drawable.listlayoutsample2xml_icon_facebook};  
    
    /* 
     * ??? ?? AgendaItem? ??? ??? data
     */
    /*
     * AgendaItem? mAgendarcolor? ??? data
     */
    private int[] mAgendaColor = 
    {
            AGENDA_CYAN,
            AGENDA_BLUE,
            AGENDA_GREEN,
            AGENDA_PURPLE,
            AGENDA_YELLOW,        
            AGENDA_COBALT
    };
    
    /*
     * AgendaItem? mAgendatabicon? ??? data
     */
    private int[] mAgendaTagIcon =
    {
            AGENDA_TAG_ICON_NONE,
            AGENDA_TAG_ICON_NONE,
            AGENDA_TAG_ICON_NONE,
            AGENDA_TAG_ICON_NONE,
            AGENDA_TAG_ICON_NONE,
            AGENDA_TAG_ICON_FACEBOOK
    };
    
    /*
     * AgendaItem? mAgenda? ??? data
     */
    private String[] mAgenda = 
    {
            "Personal",
            "Work",
            "UX Concil",
            "Holiday",
            "Event",
            "Facebook"
    };
    
    /*
     * AgendaItem? mTag? ??? data
     */
    private String[] mTag =
    {
            "",
            "Google",
            "Google",
            "",
            "Google",
            "Facebook"        
    };
    
    /*
     * ListView? ???? Adapter. ArrayAdapter? ???? ??.
     * AgendaItem type? data?? ArrayList? ???.
     */
    private class AgendaAdapter extends ArrayAdapter<AgendaItem> {

        private ArrayList<AgendaItem> items;

        public AgendaAdapter(Context context, int textViewResourceId, ArrayList<AgendaItem> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listlayoutsample2xml_row, null);
                }
                AgendaItem ti = items.get(position);
                if (ti != null) {
                    /*
                     * List ? item? ???? ??? View? data? ????.
                     * AgendaItem class? getter method? ???? data? ?? ?? ??.
                     */
                    ImageView ivAgendaColor = (ImageView) v.findViewById(R.id.icon1);
                    ImageView ivAgendaTag = (ImageView) v.findViewById(R.id.icon2);
                    TextView tvAgendaText = (TextView) v.findViewById(R.id.text1);
                    TextView tvTag = (TextView) v.findViewById(R.id.text2);
                    final CheckBox check = (CheckBox) v.findViewById(R.id.checkbox1);
                    
                    // AgendaColor
                    if (ivAgendaColor != null){
                        int agendaIconType;
                        
                        // getAgendaColorType? AgendaColorType?  get??
                        // ?? ???? drawable? imageView BackgroundResource? ???.
                        int colortype = ti.getAgendaColorType();
                        agendaIconType = getAgendaIconType(colortype);
                        //ivAgendaColor.setImageResource(mAgendaColorDrawable[agendaIconType]);                            
                        ivAgendaColor.setBackgroundResource(mAgendaColorDrawable[agendaIconType]);
                    }
                    
                    // AgendaIcon
                    if( ivAgendaTag != null){
                        // getAgendaTagIconTyp? AgendaTagIconType?  get??
                        // ?? ???? drawable? imageView Resource? ???.
                        int icontype = ti.getAgendaTagIconType();
                        int visibility = getAgendaTagVisibility(icontype);
                        ivAgendaTag.setVisibility(visibility);
                        
                        Log.i(TAG, "getView::icontype is "+icontype);
                        if(icontype != -1 && mAgendaTagDrawable[icontype-AGENDA_TAG_ICON_NONE] != -1)
                            ivAgendaTag.setImageResource(mAgendaTagDrawable[icontype-AGENDA_TAG_ICON_NONE]);                               
                    }
                    
                    // AgendaText
                    if (tvAgendaText != null){
                        tvAgendaText.setText(ti.getAgendaText());                            
                    }
                    
                    // AgendaTag
                    if (tvTag != null){
                        // AgendaTag Text? null? ???? ? TextView? display?? ?? ?? 
                        // Visibility? GONE?? ????.
                        // INVISIBLE? ??? GONE? ??? TabText? ?? ?? AgendaText? CenterAlign??? ?? ??.
                        String tagstring = ti.getAgendaTag();
                        if(tagstring.length() == 0)
                        {
                            Log.i("TouchWiz3List2", "getView::item("+position+")'s TagText length is 0");
                            tvTag.setVisibility(View.GONE);
                        }
                        else{
                            tvTag.setText(ti.getAgendaTag());
                            // [09.10.23] converView? ?? ??? ???? ? setting? reset?? ?? ??.
                            tvTag.setVisibility(View.VISIBLE);
                        }
                    }
                    if(check != null) {
                        final int myPosition = position;
                        check.setOnClickListener(new OnClickListener() {
                            
                            public void onClick(View v) {
                                
                                // ??? onBtnCheckChanged() ??? ?? ???? ??? ???
                                // ??? ????? ??? ????,  onBtnCheckChanged() ?
                                // post ??.
                                // [E] To avoid sceen flicker, post onBtnCheckChanged() instead of direct calling.
                                
                                check.post(new Runnable() {
                                    public void run() {
                                        boolean fchecked = check.isChecked();
                                        onBtnCheckChanged(myPosition, fchecked);        
                                    }
                                });
                                
                            }
                        });
                    }
                
                }
                return v;
        }
    }
    
    private void onBtnCheckChanged(int position, boolean check) {
        Log.d(TAG,"onBtnCheckChanged("+position+","+check+")");
        
        // ListView.setItemChecked() ??? headerView ??? ????? ??????.
        mList.setItemChecked(position+mList.getHeaderViewsCount(), check);
        
           updateAllCheckState();
    }
        
    private void updateAllCheckState() {
        Log.d("updateAllCheckState","called");
        
        boolean everyBodyIsOn = true;

        // dataCount? ?? ????? + ??? ??
        int dataCount = mList.getCount();
        
        for(int i=mList.getHeaderViewsCount(); i<dataCount && everyBodyIsOn; ++i) {
            // ListView.isItemChecked() ??? headerView ??? ????? ??????.
            boolean checked = mList.isItemChecked(i);
            Log.d("updateAllCheckState","checked["+i+"] : "+checked);
            if(!checked) {
                everyBodyIsOn = false;
            }
        }
        
        mHeaderAllCheckBox.setChecked(everyBodyIsOn);
    }
    
    
    /**
     * @param agendacolor
     * @return agendacolor? ???? drawable? type
     */
    public int getAgendaIconType(int agendacolor){
        int type = -1;
        switch(agendacolor)
        {
        case AGENDA_CYAN:
            type = 0;
            break;
        case AGENDA_BLUE:
            type = 1;
            break;
        case AGENDA_GREEN:
            type = 2;
            break;
        case AGENDA_PURPLE:
            type = 3;
            break;
        case AGENDA_YELLOW:
            type = 4;
            break;
        case AGENDA_COBALT:
            type = 5;
            break;
        case AGENDA_NONE:
        default:
            type = -1;
            break;
        }
        return type;
    }
    
    // [09.10.23] newly added! to set status of all items.
    public void setAllItemChecked(boolean bCheckStatus){        
        int dataCount = mList.getCount();
        for(int i=0; i<dataCount; ++i) {
            // ListView.setItemChecked() ??? ???? ListView ? ?????
            // requestLayout()? ???? ? ??? ?? item row ? ???
            // getView() ? ?? ?? ? ???.
            mList.setItemChecked(i, bCheckStatus);
        }
    }
    
    public int getAgendaTagVisibility(int agendaicontype)
    {
        int visibility = View.VISIBLE;
        
        switch(agendaicontype)
        {
        case AGENDA_TAG_ICON_FACEBOOK:
            visibility = View.VISIBLE;
            break;
        case AGENDA_TAG_ICON_GOOGLE:
            visibility = View.INVISIBLE;
            break;
        case AGENDA_TAG_ICON_NONE:
        default:
            visibility = View.GONE;
            break;
        }
        
        return visibility;        
    }
    
    /**
     * @author cooldawn.kim
     * 
     * Agenda Item 
     */

    class AgendaItem{
        private int mAgendarcolor;
        private int mAgendatabicon;
        private String mAgenda;
        private String mTag;
        
        public AgendaItem( int color, int tagicon, String agenda, String tag )
        {
            this.mAgendarcolor = color;
            this.mAgendatabicon = tagicon;
            this.mAgenda = agenda;
            this.mTag = tag;
        }
        
        public int getAgendaColorType(){
            return mAgendarcolor;
        }
        
        public int getAgendaTagIconType(){
            return mAgendatabicon;
        }
        
        public String getAgendaText(){
            return mAgenda;
        }
        
        public String getAgendaTag(){
            return mTag;
        }
        
    } // class AgendaItem

    
    /**
     * [09.10.23]
     * @author rhoon
     * 
     * public class
     * Checkable linear layout. ?? Checkable ?? ??? ??? ??? ??
     * ????? forwarding ??? ?????.
     */

    public static class ItemView extends LinearLayout implements Checkable {

        public ItemView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public boolean isChecked() {
            CheckBox check = findCheckBox();
            if(check != null) {
                return check.isChecked();
            }                
            return false;
        }

        public void setChecked(boolean checked) {
            Log.d("ItemView", "setChecked("+checked+")");
            CheckBox check = findCheckBox();
            if(check != null) {
                check.setChecked(checked);
            }                
        }

        public void toggle() {
            CheckBox check = findCheckBox();
            if(check != null) {
                check.toggle();
            }
                
        }
        
        private CheckBox findCheckBox() {
            View check = findViewById(R.id.checkbox1);
            if(check != null && check instanceof CheckBox) {
                return (CheckBox)check;
            }
            return null;
        }
        
    }
}
  评论这张
 
阅读(770)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017