关于CheckBox和EditText在ListView里多布局的处理

由于时间有限,自己觉得这些知识点,以后自己会用到,做些笔记方便自己查询,也希望给遇到此类问题的一些人提供些帮助。

时间仓促,以后回头做解释,先把代码展示出来,需要的可以借鉴下。

1、注册清单里添加如下:

  android:windowSoftInputMode="adjustPan"

<activity
            android:name="com.pda.ui.repair.SeeCheckDetils_ActivityNew"
             android:windowSoftInputMode="adjustPan"
            android:screenOrientation="portrait"></activity>

临时插入一点小知识

  android:windowSoftInputMode

各值的含义:

  【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置

  【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示

  【C】stateHidden:用户选择activity时,软键盘总是被隐藏

  【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的

  【E】stateVisible:软键盘通常是可见的

  【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态

  【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示

  【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间

  【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

2、要展示的ListView布局如下:

android:descendantFocusability="afterDescendants"
<ListView 
        android:background="@color/subject_bg"
        android:descendantFocusability="afterDescendants"
        android:id="@+id/add_check_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

插入小知识点:

  beforeDescendants:viewgroup会优先其子类控件而获取到焦点
  afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
  blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

3、适配器如下(建议直接放在调用适配器的地方):(重点

    /**
     * @author MrFu适配器
     */
    private class SeeCheckRepairAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        final int TYPE_1 = 1;
        final int TYPE_2 = 2;
        final int TYPE_3 = 3;
        private int index = -1;

        public SeeCheckRepairAdapter(Context context) {
            super();
            this.mInflater = LayoutInflater.from(context);
        }

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

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

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

        // =====================
        @Override
        public int getItemViewType(int position) {
            return tmrDynamicArr.get(position).getOpertype();
        }

        @Override
        public int getViewTypeCount() {
            if (tmrDynamicArr.size() == 0)
                return 1;
            else
                return tmrDynamicArr.size();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder1 holder1 = null;
            ViewHolder2 holder2 = null;
            ViewHolder3 holder3 = null;
            int type = getItemViewType(position);
                switch (type) {
                case TYPE_1:
                    convertView = mInflater.inflate(R.layout.activity_check_a_style, parent, false);
                    holder1 = new ViewHolder1();
                    holder1.left_text = (TextView) convertView.findViewById(R.id.left_text1);
                    holder1.right_cbx_routing = (CheckBox) convertView.findViewById(R.id.right_cbx_routing1);
                    convertView.setTag(holder1);
                    break;
                case TYPE_2:
                    convertView = mInflater.inflate(R.layout.activity_check_b_style, parent, false);
                    holder2 = new ViewHolder2();
                    holder2.left_text = (TextView) convertView.findViewById(R.id.left_text2);
                    holder2.right_cbx_routing = (CheckBox) convertView.findViewById(R.id.right_cbx_routing2);
                    holder2.et_other = (EditText) convertView.findViewById(R.id.et_other2);
                    convertView.setTag(holder2);
                    break;
                case TYPE_3:
                    convertView = mInflater.inflate(R.layout.activity_check_c_style, parent, false);
                    holder3 = new ViewHolder3();
                    holder3.left_text = (TextView) convertView.findViewById(R.id.left_text3);
                    holder3.et_other = (EditText) convertView.findViewById(R.id.et_other3);
                    convertView.setTag(holder3);
                    break;
                }
                switch (type) {
                case TYPE_1:
                    holder1 = (ViewHolder1) convertView.getTag();
                    holder1.right_cbx_routing.setTag(position);
                    break;
                case TYPE_2:
                    holder2 = (ViewHolder2) convertView.getTag();
                    holder2.right_cbx_routing.setTag(position);
                    holder2.et_other.setTag(position); 
                    break;
                case TYPE_3:
                    holder3 = (ViewHolder3) convertView.getTag();
                    holder3.et_other.setTag(position);
                    break;
                }
            // 设置资源
            switch (type) {
            case TYPE_1:
                if (TextUtils.isEmpty(tmrDynamicArr.get(position).getShowvalue())) {
                    holder1.left_text.setText("");
                } else {
                    holder1.left_text.setText(tmrDynamicArr.get(position).getShowvalue());
                }
                if(tmrDynamicArr.get(position).getOperonoff().equals("Y")){
                    holder1.right_cbx_routing.setChecked(true);
                }else{
                    holder1.right_cbx_routing.setChecked(false);
                }
                holder1.right_cbx_routing.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            tmrDynamicArr.get(position).setOperonoff("Y");
                        }else{
                            tmrDynamicArr.get(position).setOperonoff("N");
                        }
                    }
                });
                break;
            case TYPE_2:
                if (TextUtils.isEmpty(tmrDynamicArr.get(position).getShowvalue())) {
                    holder2.left_text.setText("");
                } else {
                    holder2.left_text.setText(tmrDynamicArr.get(position).getShowvalue());
                }
                if(tmrDynamicArr.get(position).getOperonoff().equals("Y")){
                    holder2.right_cbx_routing.setChecked(true);
                }else{
                    holder2.right_cbx_routing.setChecked(false);
                }
                holder2.right_cbx_routing.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            tmrDynamicArr.get(position).setOperonoff("Y");
                        }else{
                            tmrDynamicArr.get(position).setOperonoff("N");
                        }
                    }
                });
                holder2.et_other.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            index = (Integer) v.getTag();
                        }
                        return false;
                    }
                });
                 if(tmrDynamicArr.get(position) != null){  
                        holder2.et_other.setText(tmrDynamicArr.get(position).getOpercontent());  
                    }  
                holder2.et_other.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
                    @Override
                    public void afterTextChanged(Editable s) {
//                        将editText中改变的值保存
                        tmrDynamicArr.get(position).setOpercontent(s.toString());
                    }
                });
                if (index == position) {
                    // 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
                    holder2.et_other.requestFocus();
                } else {
                    holder2.et_other.clearFocus();
                }
                break;
            case TYPE_3:
                if (TextUtils.isEmpty(tmrDynamicArr.get(position).getShowvalue())) {
                    holder3.left_text.setText("");
                } else {
                    holder3.left_text.setText(tmrDynamicArr.get(position).getShowvalue());
                }
                holder3.et_other.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            index = (Integer) v.getTag();
                        }
                        return false;
                    }
                });
                
                 if(tmrDynamicArr.get(position) != null){  
                     holder3.et_other.setText(tmrDynamicArr.get(position).getOpercontent());  
                    }  
                 holder3.et_other.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
                    @Override
                    public void afterTextChanged(Editable s) {
//                        将editText中改变的值保存
                        tmrDynamicArr.get(position).setOpercontent(s.toString());
                    }
                });
                if (index == position) {
                    // 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
                    holder3.et_other.requestFocus();
                } else {
                    holder3.et_other.clearFocus();
                }
                break;
            }
            return convertView;
        }

        public class ViewHolder1 {
            TextView left_text;
            CheckBox right_cbx_routing;
        }

        public class ViewHolder2 {
            TextView left_text;
            CheckBox right_cbx_routing;
            EditText et_other;
        }

        public class ViewHolder3 {
            TextView left_text;
            EditText et_other;
        }

    }

主要通过存储到操作数据保存状态以便以后面存储到数据库里。

里面用了三个布局,方便以后使用在这里添加布局:

R.layout.activity_check_a_style

<?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="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/left_text1"
            style="@style/comm_blue_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:text="这里是测试文本aaaaaaa"
             />

        <CheckBox
            android:id="@+id/right_cbx_routing1"
            style="@style/comm_checkbox_repair" />
    </RelativeLayout>

    <include layout="@layout/comm_divider" />

</LinearLayout>

R.layout.activity_check_b_style

<?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="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/left_text2"
            style="@style/comm_blue_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:text="这里是测试文本bbbbbbb" />

        <CheckBox
            android:id="@+id/right_cbx_routing2"
            style="@style/comm_checkbox_repair" />
    </RelativeLayout>

    <include layout="@layout/comm_divider" />

    <EditText
        android:id="@+id/et_other2"
        style="@style/check_right_text"
        android:textCursorDrawable="@drawable/color_cursor" />
    <include layout="@layout/comm_divider" />

</LinearLayout>

R.layout.activity_check_c_style

<?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="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/left_text3"
            style="@style/comm_blue_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:text="这里是测试文本ccccccccc" />
    </RelativeLayout>

    <include layout="@layout/comm_divider" />

    <EditText
        android:id="@+id/et_other3"
        style="@style/check_right_text"
        android:textCursorDrawable="@drawable/color_cursor" />
    <include layout="@layout/comm_divider" />
</LinearLayout>

调用方式:

SeeCheckRepairAdapter adapter = new SeeCheckRepairAdapter(SeeCheckDetils_ActivityNew.this);
        
        add_check_list = (ListView) findViewById(R.id.add_check_list);
        add_check_list.setAdapter(adapter);

最后把更改和操作的数据存储到数据库里

/**
                     * 这是保存操作数据
                     */
                    if(tmrDynamicArr!=null&&tmrDynamicArr.size()>0){
                        for(int i = 0;i<tmrDynamicArr.size();i++){
                            try {
                                dynamic_dao.update(tmrDynamicArr.get(i));
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }

 

下面这是我从数据库里读取数据和创建数据(ormlite方式,阅读者不用看)

Map<String, Object> where_dynamic=new HashMap<String, Object>();
         where_dynamic.put("taskid", atm_bean.getTaskid()+"");
         where_dynamic.put("atmid", atm_bean.getAtmid()+"");
         where_dynamic.put("pdatype", 1);//0为加钞组,1为维修组
         where_dynamic.put("pagetype", "data1");//页面类型,data1为页面1(巡检),data2为机具操作结束的页面
         List<TmrDynamicCheckVo> dynamics = dynamic_dao.quaryForNeed(where_dynamic);
         if(dynamics!=null&&dynamics.size()>0){
             for(int i = 0;i<dynamics.size();i++){
                 tmrDynamicArr.add(dynamics.get(i));
             }
         }else{
             TmrDynamicCheckVo testDataVo1 = new TmrDynamicCheckVo(atm_bean.getTaskid()+"", atm_bean.getAtmid()+"", 2, "1004", "流水打印机", "Y", "");// 流水打印机
             TmrDynamicCheckVo testDataVo2 = new TmrDynamicCheckVo(atm_bean.getTaskid()+"", atm_bean.getAtmid()+"", 2, "1005", "凭条打印机", "Y", "");// 凭条打印机
             TmrDynamicCheckVo testDataVo3 = new TmrDynamicCheckVo(atm_bean.getTaskid()+"", atm_bean.getAtmid()+"", 2, "1006", "通讯线路", "Y", "");
             TmrDynamicCheckVo testDataVo4 = new TmrDynamicCheckVo(atm_bean.getTaskid()+"", atm_bean.getAtmid()+"", 2, "1007", "温度", "Y", "");
             TmrDynamicCheckVo testDataVo5 = new TmrDynamicCheckVo(atm_bean.getTaskid()+"", atm_bean.getAtmid()+"", 2, "1008", "UPS", "Y", "");
             TmrDynamicCheckVo testDataVo6 = new TmrDynamicCheckVo(atm_bean.getTaskid()+"", atm_bean.getAtmid()+"", 2, "1009", "废钞", "Y", "");
             
            
             dynamic_dao.create(testDataVo1);
              dynamic_dao.create(testDataVo2);
             dynamic_dao.create(testDataVo3);
              dynamic_dao.create(testDataVo4);
             dynamic_dao.create(testDataVo5);
             dynamic_dao.create(testDataVo6);
             List<TmrDynamicCheckVo> dynamics1 = dynamic_dao.quaryForDetail(where_dynamic);
             if(dynamics1!=null&&dynamics1.size()>0){
                 for(int i = 0;i<dynamics1.size();i++){
                     tmrDynamicArr.add(dynamics1.get(i));
                 }
             }

 

 

 

 

 

 

以后慢慢作解释。


 

posted @ 2015-07-29 16:49  F_YuXiang  阅读(340)  评论(0编辑  收藏  举报