qq_33230445
静水流深—j
采纳率38.9%
2016-09-07 11:11 阅读 2.8k
已采纳

Android ExpandableListView点击列表项改变列表项内组件产生混乱的问题

2

//ExpandableListView的适配器
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupViewHolder gvholder;
if (convertView == null) {
gvholder = new GroupViewHolder();
convertView = LinearLayout.inflate(getBaseContext(),
R.layout.group_layout, null);
gvholder.groupcity = (TextView) convertView
.findViewById(R.id.group_title);
gvholder.groupsize = (TextView) convertView
.findViewById(R.id.groupcap);
gvholder.groupload = (ImageView) convertView
.findViewById(R.id.group_state);
convertView.setTag(gvholder);
} else {
gvholder = (GroupViewHolder) convertView.getTag();
}

// ExpandableListView 的OnGroupClick监听
allcitylists.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // TODO Auto-generated method stub
            group_checked[groupPosition] = group_checked[groupPosition] + 1;

            ((BaseExpandableListAdapter) expadapter).notifyDataSetChanged();
            if (groupPosition < 5
                    || groupPosition >= provincelist.size() - 3) {
                ArrayList<MKOLSearchRecord> records = mOffline
                        .searchCity(provincelist.get(groupPosition).get(0));

                if (records != null && records.size() == 1) {
                    cityid =

                    records.get(0).cityID;

                    mOffline.start(cityid);

                    updateView();

                //改变的组件
                    TextView loadtext = (TextView) v
                            .findViewById(R.id.group_loadtitle);
                    loadtext.setVisibility(View.VISIBLE);

                    changeloadtext(loadtext, cityid);

                }

            }
            return false;
        }
    });

    内容比较多,没贴完,但关键的地方都在上面,点击列表项能够把loadtext显示出来,但是上下滑动之后,显示的就不是我刚刚点击的那个列表项的TextView,而是其他列表项的Textview,应该是不能在这里用ViewHolder,但是我也想不出来怎么改变,请各位大神指教指教
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    ljlfcg111 haha我逗 2016-09-08 02:20

    回收机制,你上下滑动之后,最上面的item被回收了,你之前点击的那个group会被刷新,你得保存每个group被点击后的groupposition 和对应的cityid,在适配器刷新之后重新显示出来

    点赞 评论 复制链接分享
  • ljlfcg111 haha我逗 2016-09-28 03:14

    比如这是adapter的getView
    public View getView(int position, View convertView, ViewGroup parent)
    {
    ViewHolder viewHolder = null;
    try
    {
    if (convertView == null)
    {
    viewHolder = new ViewHolder();
    convertView = LayoutInflater.from(context).inflate(R.layout.item_obtain_order, null);
    viewHolder.tv1 = (TextView) convertView.findViewById(R.id.textview);//随便写的textview
    convertView.setTag(viewHolder);

        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        configViewData(viewHolder,position);
        //-----------------------看这里-----------------------------------
    
        //假设你有个数组String[] data = {1,2}里面有两个值.
        //当你点击之后,你这个适配器绑定的数据就得保存起来,例如点击了第二个值,数值改变成了3
        //如果你把绑定的数据data 变成{1,3},那当然没问题,怎么滑动都没有问题
        //如果只是单纯的点击后把textview改变显示的值,不保存到对应position的绑定数据的话,滑动后就会变回原来的
    
        viewHolder.tv1.setText(data[position]);
    
        //还有一个问题我以前经常犯,如果根据某个状态设值
        //如果只写了if,表面上加载listview之后是没问题的,当你一上下拉,这个textview就会乱变化了,你可以试一试
        /**
        *错误写法
        *if(state.equals("1")){
        *    viewHolder.tv1.setText("已选择");
        *}
        */
    
        //想上下拉动显示不会错乱的话,正确写法只要把else也写了就行
    
        if(state.equals("1")){
            viewHolder.tv1.setText("已选择");
        }else{
            viewHolder.tv1.setText("");
        }       
    
        //-----------------------看这里------------------------------------
        } catch (Exception e)
        {
            Log.e("error", e.toString());
        }
        return convertView;
    }
    
    点赞 1 评论 复制链接分享
  • devmiao devmiao 2016-09-07 15:02
  • qq_28026283 大白大白大白 2016-09-08 02:14

    需要loadtext的setTag试一下呢

    点赞 评论 复制链接分享

相关推荐