Listview gets recreated and changes the previous views

余生颓废 提交于 2020-01-04 07:27:12

问题


I have a ListView in which I inflate 4 different layouts dynamically. In every Item in listview the common things are 3 buttons. Like ,dislike, comment. So when we click on like it shows up with gallery with profile pictures related to like. And same way for dislike and comment. The problem arises that when I click on like it will show up proper data but when I scroll down and when that Item is out of view then when i scroll back up to view that Item it changes its view with some other button click. This happens foe every Item in ListView Need a urgent help Code of getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        mInflater= LayoutInflater.from(mContext);
            pos=position;
        if(convertView==null){
            Log.e("--------Inside---------", "convertView==null");
            holder= new Holder();
            convertView = mInflater.inflate(R.layout.custom_stream_list, null);
            holder.childView= (LinearLayout) convertView.findViewById(R.id.view_to_inflate);
            holder.myGallery= (Gallery) convertView.findViewById(R.id.myGallery_stream);
            holder.like= (ImageView) convertView.findViewById(R.id.like_stream);
            holder.dislike =(ImageView) convertView.findViewById(R.id.dislike_stream);
            holder.comment = (ImageView) convertView.findViewById(R.id.comment_stream);
            holder.commentLayout = (RelativeLayout) convertView.findViewById(R.id.comment_view_to_inflate);
            holder.comment_name = (TextView) convertView.findViewById(R.id.comment_name);
            holder.comment_description = (TextView) convertView.findViewById(R.id.comment_description);
            holder.comment_time = (TextView) convertView.findViewById(R.id.comment_time);

            convertView.setTag(holder);
        }else {
            Log.e("--------Inside---------", "else Statement");
            holder= (Holder) convertView.getTag();
        }
        holder.myGallery.setVisibility(View.INVISIBLE);

        holder.like.setTag(holder);
        holder.dislike.setTag(holder);
        holder.comment.setTag(holder);



        holder.myGallery.setSpacing(5);


        holder.like.setOnClickListener(this);
        holder.dislike.setOnClickListener(this);
        holder.comment.setOnClickListener(this);


        if((position%4)==0){
            Log.e("--------Inside---------", "position==0");
            followLayoutView = mInflater.inflate(R.layout.abc, null);
            holder.news_titile=(TextView) followLayoutView.findViewById(R.id.news_article_titile);
            holder.news_description= (TextView) followLayoutView.findViewById(R.id.news_article_description);
            holder.childView.removeAllViews();
            holder.childView.addView(followLayoutView);

        } if((position%4)==1){
            Log.e("--------Inside---------", "position==1");
            followLayoutView1 = mInflater.inflate(R.layout.xyz, null);
            holder.match_event_title= (TextView) followLayoutView1.findViewById(R.id.match_events_stream_titile);
            holder.match_event_time= (TextView) followLayoutView1.findViewById(R.id.following_time);
            holder.match_event_description= (TextView) followLayoutView1.findViewById(R.id.match_events_stream_description);
            holder.childView.removeAllViews();
            holder.childView.addView(followLayoutView1);


        } if((position%4)== 2){
            Log.e("--------Inside---------", "position==2");
            followLayoutView2 = mInflater.inflate(R.layout.pqr, null);
            holder.match_event_title= (TextView) followLayoutView2.findViewById(R.id.match_events_stream_titile);
            holder.match_event_time= (TextView) followLayoutView2.findViewById(R.id.following_time);
            holder.match_event_description= (TextView) followLayoutView2.findViewById(R.id.match_events_stream_description);
            holder.childView.removeAllViews();
            holder.childView.addView(followLayoutView2);

        } if((position%4)==3){
            Log.e("--------Inside---------", "position==3");
            followLayoutView3 = mInflater.inflate(R.layout.mno, null);
            holder.video_background= (ImageView) followLayoutView3.findViewById(R.id.video_background);
            holder.play = (ImageView) followLayoutView3.findViewById(R.id.video_play);
            holder.childView.removeAllViews();
            holder.childView.addView(followLayoutView3);


        }

        return convertView;
    }

回答1:


I guess you are not using Holder for your dynamic list. Use Holder Pattern in order to avoid that issue. Hope this link helps. http://www.vogella.com/articles/AndroidListView/article.html



来源:https://stackoverflow.com/questions/12794182/listview-gets-recreated-and-changes-the-previous-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!