欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决ListView中getView()多次调用的问题

程序员文章站 2022-07-05 09:37:21
ListView中getView()多次调用原因https://blog.csdn.net/WannerWang/article/details/49097001?utm_source=app简单的解决方法就是android:layout_height="wrap_content"改为android:layout_height="match_parent"...

ListView中getView()多次调用原因
https://blog.csdn.net/WannerWang/article/details/49097001?utm_source=app

简单的解决方法就是

android:layout_height="wrap_content"
改为
android:layout_height="match_parent"

因为我要用在Dialog上,所以改为match_parent不合适

我打点发现
解决ListView中getView()多次调用的问题
6个条目调用了42次
3个条目调用了21次

所以是7次

我是在ListView中嵌套Switch开关,点击ListView条目来切换Switch的状态,用List来控制,所以要在getView()中加入,
这时候问题来了,调用了这么多次,到底要在哪一次加呢?

一个一个分析发现是第四次。
所以:
6组数据
1=0
2=25 (7-3)×6+1
3=26 (7-3)×6+2
4=27 (7-3)×6+3
5=28 (7-3)×6+4
6=29 (7-3)×6+5

解决方法

int List_Count = getCount();
            int aa = List_Count * 4 + 1;
            
            int_list_switch = new int[List_Count];

        
            for (int i = 0; i < List_Count; i++) {

                int_list_switch[i] = aa;
                aa++;
            }

getView()中

     //优化Switch控件
            if (Optimize_getView == 0) {
                list_switch.add(holder.sw_Clock);
            }
            if (Optimize_getView == int_list_switch[Optimize_getView_Assist]) {
                list_switch.add(holder.sw_Clock);
                Log.d("添加switch", "" + Optimize_getView);
                if (list_switch.size() < getCount())
                    Optimize_getView_Assist++;
            }

觉得还有什么更好的办法评论下方留意吧!
全部代码

    class TalkAdapter extends BaseAdapter implements OnClickListener {

        private Context mContext;
        private List<Course>  mData;
        private List<Switch> list_switch;


        private int[] int_list_switch;
        private int Optimize_getView = 0;
        private int Optimize_getView_Assist =0;
        public HashMap<Integer, Boolean> Switch_isSelected= new HashMap<Integer, Boolean>();


        public TalkAdapter(Context mContext, List<Course>  mData) {
            this.mData = mData;
            this.mContext = mContext;


            list_switch = new ArrayList<>();
            list_switch.clear();

            int List_Count = getCount();
            int aa = List_Count * 4 + 1;
            int bb = 1;
            int_list_switch = new int[List_Count];

        
            for (int i = 0; i < List_Count; i++) {

                int_list_switch[i] = aa;
                aa++;
            }

            // int_list_switch= new int[]{0,17,18,19,28,29};


            if (MMkvListDate.isDataCapacity(SwitchStatus_KEY)) {              
                Switch_isSelected.clear();
                Switch_isSelected = MMkvListDate.getIBHashMap(SwitchStatus_KEY);             
            } else {
                for (int i = 0; i < getCount(); i++) {
                    Switch_isSelected.put(i, false);

                }
            }
        }

        public Switch getaSwitch(int position) {
            // Toast.makeText(mContext,list_switch.size()+ "位置:"+position, Toast.LENGTH_SHORT).show();
            return list_switch.get(position);
        }

        @Override
        public int getCount() {          
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if (convertView == null) {

                convertView = LayoutInflater.from(mContext).inflate(R.layout.course_alarmclock_tips_list_item, parent, false);
                holder = new ViewHolder();

                holder.tv_className = (TextView) convertView.findViewById(R.id.coursealarmclocktipslistitemTextView1);
                holder.tv_classTime = (TextView) convertView.findViewById(R.id.coursealarmclocktipslistitemTextView2);

                holder.sw_Clock = (Switch)convertView.findViewById(R.id.coursealarmclocktipslistitemSwitch1);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            String CourseName =mData.get(position).getName();
            String jc = LitepalCourseUtil.getJc(mData.get(position).getTime());
            holder.tv_className.setText(CourseName);
            holder.tv_classTime.setText(jc);

            System.out.println("list_getView()" + position);

            //优化Switch控件
            if (Optimize_getView == 0) {
                list_switch.add(holder.sw_Clock);
            }
            if (Optimize_getView == int_list_switch[Optimize_getView_Assist]) {
                list_switch.add(holder.sw_Clock);
                Log.d("添加switch", "" + Optimize_getView);
                if (list_switch.size() < getCount())
                    Optimize_getView_Assist++;
            }



            Log.d("Log_test", "___" + Optimize_getView);

            Optimize_getView++;
            
            holder.sw_Clock.setChecked(Switch_isSelected.get(position));
           // holder.sw_Clock.setChecked(Switch_isSelected.get(position)==null?false:true);
            
            
            

            return convertView;
        }

        @Override
        public void onClick(View p1) {
        }

        private class ViewHolder {
            TextView  tv_className;
            TextView  tv_classTime;
            Switch sw_Clock;
        }
    }
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    Switch aSwitch=item_Adapter.getaSwitch(position);
                    if (aSwitch.isChecked()) {
                        aSwitch.setChecked(false);
                        //进行业务处理

                    } else {
                        aSwitch.setChecked(true);
                        //进行业务处理

                    }

                                        
                    Toast.makeText(getContext(), aSwitch.isChecked() + "  第" + position, Toast.LENGTH_SHORT).show();
                }
            });
    }

本文地址:https://blog.csdn.net/jiulang9/article/details/110286785

相关标签: 笔记