重新创建Listview并更改以前的视图

时间:2012-10-09 06:39:13

标签: android android-layout android-listview

我有一个ListView,其中我动态地扩展了4种不同的布局。在listview中的每个项目中,常见的东西是3个按钮。喜欢,不喜欢,评论。因此,当我们点击时,它会显示与图库相关的图片库。不喜欢和评论的方式相同。 问题出现了,当我点击它会显示正确的数据,但是当我向下滚动并且当该项目不在视图中时,当我向上滚动以查看该项目时,它会通过其他一些按钮点击更改其视图。 这发生在ListView中的每个项目中 急需帮助 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 个答案:

答案 0 :(得分:0)

我猜你没有使用Holder作为动态列表。 使用持有人模式以避免该问题。 希望此链接有所帮助。 http://www.vogella.com/articles/AndroidListView/article.html