Dialog+listview获取不到按键事件的解决办法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wrrryyyyy/article/details/82944262

自己想模仿AlertDialog.setMulitChoiceItems的风格写一个多选框

创建View和显示ListView都没问题 就是按键事件一直出不来

尝试删掉布局里的CheckBox 有按键事件

中间蠢蠢的在listview的配置文件里设置焦点 怎么设置怎么不行

最后解决方法是在adapterItem的CheckBox的xml配置里添加这几行 防止抢listview的焦点

        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"

然后在按键事件中设置CheckBox被选中

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            CheckBox cb = (CheckBox)view.findViewById(R.id.cb_wave_item);
                            boolean bool = cb.isSelected();
                            cb.setSelected(!bool);
                        }
                    });

 

展开阅读全文

Listview获取不到高度

04-28

我Listview 循环里面放ImageViewrnImageView的高度是随图片的宽度自由缩放rnListview 在动态设置高度的时候获取不到itemView里面的高度rn这个如何解决呢??rn[code=java]rn//设置单个ItemView数据rnpublic View getView(int position, View convertView, ViewGroup viewGroup) rn ProductIntroAdapter.ViewHolder holder;rn if (convertView == null) rn convertView = View.inflate(mContext, R.layout.product_item_intro, null);rn holder= new ProductIntroAdapter.ViewHolder();rn holder.iv_intro_img = convertView.findViewById(R.id.iv_intro_img);rn convertView.setTag(holder);rn rn else rn holder = (ProductIntroAdapter.ViewHolder) convertView.getTag();rn rn String imgurl = datas.get(position);rn //高度随宽度适应rn RequestOptions myOptions = new RequestOptions()rn .fitCenter();rn //loadimgrn Glide.with(mContext).load(imgurl).apply(myOptions).into(holder.iv_intro_img);rn return convertView;rn rnrn static class ViewHolder rn ImageView iv_intro_img;rn rn[/code]rnrn[code=java]rn//Json数据给Adapterrn private void processData(String jsonData) rn proData = JSON.parseObject(jsonData, ProductDetail.class);rn ProductIntroAdapter adapter = new ProductIntroAdapter(this, proData.getListIntros());rn lv_intro.setAdapter(adapter);rnrn adapter.notifyDataSetChanged();rn ListViewHight.setListViewHeightBasedOnChildren(lv_intro,40,0);rn rn[/code]rn[code=java]rnpublic static void setListViewHeightBasedOnChildren(ListView listView, int basicsHeight,int plusItem) rn //基础高度rn int totalHeight = basicsHeight;rn ListAdapter adapter = listView.getAdapter();rnrn for (int i = 0; i < adapter.getCount(); i++) rn View listItem = adapter.getView(i, null, listView);rn listItem.measure(0, 0);rn int itemlength=listItem.getMeasuredHeight();rn Log.e("ItemView"+i, "高度" + itemlength);//打印出来一直是0rn totalHeight += itemlength+plusItem;rn rn ViewGroup.LayoutParams params = listView.getLayoutParams();rn params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1))+2000;rn listView.setLayoutParams(params);rn rn[/code]rn请问我应该怎么设置ListView高度?? 论坛

没有更多推荐了,返回首页