在视图中发生更改时使用转换视图模式

时间:2015-02-24 05:57:42

标签: android listview android-arrayadapter convertview

我在自定义数组适配器类中使用转换视图模式以获得列表视图效率。然而,我面临的问题是,我有一个ImageView里面的行模板,其可见性决定了getView方法中的某些条件。当listview重用视图时,一切都变得混乱,我得到了imageView的可见性GONE,我希望它可见,反之亦然。解决这个问题的最佳方法是什么?我已经完成了所有this但没有任何有用的东西。我也试过使用两种不同的布局而没有运气。 这是我的getVIew方法

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

    Typeface tf_title = Typeface.createFromAsset(context.getAssets(),
            "Roboto-Regular.ttf");
    Typeface tf_content = Typeface.createFromAsset(context.getAssets(),
            "Roboto-Light.ttf");

    options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.default_user)
            .showImageForEmptyUri(R.drawable.default_user)
            .showImageOnFail(R.drawable.default_user).cacheOnDisc(true)
            .considerExifParams(true).build();

    ViewHolder viewHolder = null;

    if (convertView == null) {

        convertView = mLayoutInflater.inflate(R.layout.temp, parent, false);

        viewHolder = new ViewHolder();

        viewHolder.tv_username = (TextView) convertView
                .findViewById(R.id.tv_username);

        viewHolder.tv_likes = (TextView) convertView
                .findViewById(R.id.tv_likes);

        viewHolder.tv_comment = (TextView) convertView
                .findViewById(R.id.tv_comment);
        viewHolder.tv_captions = (TextView) convertView
                .findViewById(R.id.tv_captions);

        viewHolder.tv_time = (TextView) convertView
                .findViewById(R.id.tv_time);

        viewHolder.img_photo = (ImageView) convertView
                .findViewById(R.id.img_photo);

        viewHolder.image_user = (ImageView) convertView
                .findViewById(R.id.image_user);

        viewHolder.img_like = (ImageView) convertView
                .findViewById(R.id.img_like);

        viewHolder.img_play_pause = (ImageView) convertView
                .findViewById(R.id.img_play_pause);

        viewHolder.playing_progress_bar = (ProgressBar) convertView
                .findViewById(R.id.playing_progress_bar);

        viewHolder.download_progres_bar = (ProgressBar) convertView
                .findViewById(R.id.download_progres_bar);

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

    if (viewHolder.tv_time.getTag() == null
            || !viewHolder.tv_time.getTag().equals(
                    Integer.valueOf(position))) {

        viewHolder.tv_time.setTag(Integer.valueOf(position));
    } else {
        // do nothing
    }

    if (viewHolder.download_progres_bar.getTag() == null) {
        viewHolder.download_progres_bar
                .setTag(values.get(position).isProgressing);
        // Log.e("null", "null " + position);
    } else if (!viewHolder.download_progres_bar.getTag().equals(
            values.get(position).isProgressing)) {  
        // Log.e("changed", "changed " + position);
        viewHolder.download_progres_bar
                .setTag(values.get(position).isProgressing);
    } else {

    }

    if ((Boolean) viewHolder.download_progres_bar.getTag() == true) {
        viewHolder.download_progres_bar.setVisibility(View.VISIBLE); // protected
        viewHolder.playing_progress_bar.setVisibility(View.GONE);
    } else {
        viewHolder.download_progres_bar.setVisibility(View.GONE); // protected
        viewHolder.playing_progress_bar.setVisibility(View.VISIBLE);
    }

    if (viewHolder.playing_progress_bar.getTag() == null) {
        viewHolder.playing_progress_bar
                .setTag(values.get(position).isPlaying);
        // Log.e("null", "null " + position);
    } else if (!viewHolder.playing_progress_bar.getTag().equals(
            values.get(position).isPlaying)) {
        // Log.e("changed", "changed " + position);
        viewHolder.playing_progress_bar
                .setTag(values.get(position).isPlaying);
    } else {

    }
    if ((Boolean) viewHolder.playing_progress_bar.getTag() == true) {
        // viewHolder.download_progres_bar.setVisibility(View.GONE); //
        // protected
        // viewHolder.playing_progress_bar.setVisibility(View.VISIBLE);
        viewHolder.playing_progress_bar.setProgress(values
                .get((Integer) viewHolder.tv_time.getTag()).progress);
    } else {
        // Log.e("priniti", "not true");
        // viewHolder.download_progres_bar.setVisibility(View.VISIBLE); //
        // protected
        viewHolder.playing_progress_bar.setProgress(100);
    }

    // Log.e("Name", "" + values.get(position).UserName);
    viewHolder.tv_username.setText(values.get(position).UserName);
    viewHolder.image_user.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("pos",""+position);
            /*String s = (String) viewHolder.image_user.getTag();
            int pos = Integer.parseInt(s.substring((s.lastIndexOf("~") + 1)));
            newsFragment.notifyProgress(pos);*/
        }
    });

    /*if (viewHolder.image_user.getTag() == null
            || !viewHolder.image_user.getTag().equals(
                    values.get(position).UserIcon + "~" + position)) {
        ImageLoader.getInstance().displayImage(
                values.get(position).UserIcon, viewHolder.image_user,
                options, animateFirstListener);

        viewHolder.image_user.setTag(values.get(position).UserIcon + "~"
                + position);
    } else {

    }*/

    if (values.get((Integer) viewHolder.tv_time.getTag()).ObjectType
            .equalsIgnoreCase("2")) {
        //  Log.e(""+(Integer) viewHolder.tv_time.getTag(),"two");
        if (viewHolder.img_photo.getTag() == null
                || !viewHolder.img_photo.getTag().equals(
                        values.get(position).Data.replace("_t", "") + "~"
                                + position)) 
        {
            viewHolder.img_photo.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(
                    values.get(position).Data.replace("_t", ""),
                    viewHolder.img_photo, options, animateFirstListener);

            viewHolder.img_photo.setTag(values.get(position).Data.replace(
                    "_t", "") + "~" + position);
        }
    } else {
        //      Log.e(""+(Integer) viewHolder.tv_time.getTag(),"seven");
        viewHolder.img_photo.setVisibility(View.GONE);
    }

    if (values.get((Integer) viewHolder.tv_time.getTag()).VoiceStatus != null) {
        viewHolder.img_play_pause.setVisibility(View.VISIBLE);
    } else {
        viewHolder.img_play_pause.setVisibility(View.GONE);
    }

    long time = getTime(values.get(position).Time);
    viewHolder.tv_time.setText(calculateRelativeTime(time));

    if (values.get(position).LikeCount == 0) {
        viewHolder.tv_likes.setText("");
    } else {
        viewHolder.tv_likes.setText("" + values.get(position).LikeCount);
    }

    if (values.get(position).CommentCount == 0) {
        viewHolder.tv_comment.setText("");
    } else {
        viewHolder.tv_comment.setText(""
                + values.get(position).CommentCount);
    }

    if (values.get(position).SelfLike.equalsIgnoreCase("1")) {
        viewHolder.img_like.setImageResource(R.drawable.feed_like);
    } else {
        viewHolder.img_like.setImageResource(R.drawable.likes_gray);
    }

    viewHolder.tv_username.setTypeface(tf_title);
    viewHolder.tv_captions.setTypeface(tf_content);
    viewHolder.tv_comment.setTypeface(tf_content);
    viewHolder.tv_time.setTypeface(tf_content);
    viewHolder.tv_likes.setTypeface(tf_content);

    if (values.get(position).ObjectType.equalsIgnoreCase("2")) {

        viewHolder.tv_captions
                .setText(Html.fromHtml(values.get(position).AdditionalData));

        // Log.e("Feed adapter",""+position+" fg "+values.get(position).FeedID);

        // viewHolder.img_photo.setTag(Integer.valueOf(position));
        viewHolder.img_photo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                //String s = (String) viewHolder.image_user.getTag();
                int rowPosition =4;/*= Integer.parseInt(s.substring((s
                        .lastIndexOf("~") + 1)));*/

                Intent i = new Intent(context,
                        PhotoFeedDetailActivity.class);
                i.putExtra("imgUrl", values.get(rowPosition).Data);
                i.putExtra("photoid", values.get(rowPosition).ObjectID);
                i.putExtra("type", "2");
                i.putExtra("AdditionalData",
                        values.get(rowPosition).AdditionalData);
                i.putExtra("CommentCount",
                        values.get(rowPosition).CommentCount);
                i.putExtra("LikeCount", values.get(rowPosition).LikeCount);
                // i.putExtra("selfLike",
                // values.get(rowPosition).SelfLike);
                i.putExtra("FeedID", values.get(rowPosition).FeedID);
                i.putExtra("position", rowPosition);
                if (scope.equalsIgnoreCase("masterProfile")) {
                    i.putExtra("fromType", 3);
                    i.putExtra("listInactivity", 0);
                } else {
                    i.putExtra("fromType", 1);
                }
                context.startActivity(i);
            }
        });
    } else {
        // viewHolder.img_photo.setVisibility(View.GONE);

        viewHolder.tv_captions
                .setText(Html.fromHtml(values.get(position).Data));

        viewHolder.tv_captions.setTag(Integer.valueOf(position));
        viewHolder.tv_captions
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Integer rowPositionn = (Integer) v.getTag();
                        Intent i = new Intent(context,
                                PhotoFeedDetailActivity.class);
                        i.putExtra("imgUrl", values.get(rowPositionn).Data);
                        i.putExtra("photoid",
                                values.get(rowPositionn).ObjectID);
                        i.putExtra("type", "7");
                        i.putExtra("username",
                                values.get(rowPositionn).UserName);
                        i.putExtra("status", values.get(rowPositionn).Data);
                        i.putExtra("CommentCount",
                                values.get(rowPositionn).CommentCount);
                        i.putExtra("LikeCount",
                                values.get(rowPositionn).LikeCount);
                        i.putExtra("UserIcon",
                                values.get(rowPositionn).UserIcon);
                        i.putExtra("position", rowPositionn);
                        if (scope.equalsIgnoreCase("masterProfile")) {
                            i.putExtra("fromType", 3);
                            i.putExtra("listInactivity", 0);
                        } else {
                            i.putExtra("fromType", 1);
                        }

                        /*
                         * i.putExtra("selfLike",
                         * values.get(rowPositionn).SelfLike);
                         */
                        i.putExtra("FeedID",
                                values.get(rowPositionn).FeedID);
                        context.startActivity(i);
                    }
                });
    }

    viewHolder.tv_likes.setTag(Integer.valueOf(position));
    viewHolder.tv_likes.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (values.get(position).ObjectType.equalsIgnoreCase("7")) {
                if (values.get(position).LikeCount == 0) {

                } else {
                    Integer rowPosition = (Integer) v.getTag();
                    Intent i = new Intent(context, LikersList.class);
                    i.putExtra("id", values.get(rowPosition).ObjectID);
                    i.putExtra("type", "7");
                    context.startActivity(i);
                }
            } else {
                if (values.get(position).LikeCount == 0) {

                } else {
                    Integer rowPosition = (Integer) v.getTag();

                    /*viewHolder.tv_likes.setText(""
                            + values.get(position).LikeCount);*/
                    Intent i = new Intent(context, LikersList.class);
                    i.putExtra("id", values.get(rowPosition).ObjectID);
                    i.putExtra("type", "2");
                    context.startActivity(i);
                }
            }
        }
    });

    viewHolder.img_like.setTag(Integer.valueOf(position));
    viewHolder.img_like.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer rowPosition = (Integer) v.getTag();
            if (values.get(position).ObjectType.equalsIgnoreCase("7")) {
                // Toast.makeText(context, "clicked", Toast.).show();
                if (values.get(position).SelfLike.equalsIgnoreCase("1")) {

                } else {
                    if (selfClickCount == 0) {
                        if (scope.equalsIgnoreCase("masterProfile")) {
                            selfClickCount = 1;
                            UserProfileSectionn activity = (UserProfileSectionn) context;
                            activity.likePhoto(rowPosition);
                        } else {
                            selfClickCount = 1;
                            newsFragment.likePhoto(rowPosition);
                        }
                    } else {
                        Log.e("do", "nothing");
                    }
                }
            } else {
                // Toast.makeText(context, "clicked", Toast.).show();
                if (values.get(position).SelfLike.equalsIgnoreCase("1")) {

                } else {
                    if (selfClickCount == 0) {
                        if (scope.equalsIgnoreCase("masterProfile")) {
                            selfClickCount = 1;
                            UserProfileSectionn activity = (UserProfileSectionn) context;
                            activity.likeStatus(rowPosition);
                        } else {
                            selfClickCount = 1;
                            newsFragment.likeStatus(rowPosition);
                        }
                    } else {
                        Log.e("do", "nothing");
                    }
                }
            }
        }
    });

    viewHolder.tv_username.setTag(Integer.valueOf(position));
    viewHolder.tv_username.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer rowPosition = (Integer) v.getTag();
            if (values.get(position).ObjectType.equalsIgnoreCase("7")) {

                Intent i = new Intent(context,
                        UserProfileSectionn.class);
                i.putExtra("userId", values.get(rowPosition).UserID);
                i.putExtra("titleName",
                        values.get(rowPosition).UserName);
                // i.putExtra("isOther", true);
                // i.putExtra("titleName",values.get(rowPosition).UserName);
                context.startActivity(i);

                /*
                 * Intent i = new Intent(context,
                 * UserProfileSectionn.class); i.putExtra("userId",
                 * values.get(rowPosition).UserID); i.putExtra("titleName",
                 * values.get(rowPosition).UserName); //
                 * i.putExtra("isOther", true); // i.putExtra("titleName"
                 * ,values.get(rowPosition).UserName);
                 * context.startActivity(i);
                 */
            } else {
                if (scope.equalsIgnoreCase("masterProfile")) {

                } else {
                    Intent i = new Intent(context,
                            UserProfileSectionn.class);
                    i.putExtra("userId", values.get(rowPosition).UserID);
                    i.putExtra("titleName",
                            values.get(rowPosition).UserName);
                    // i.putExtra("isOther", true);
                    // i.putExtra("titleName",values.get(rowPosition).UserName);
                    context.startActivity(i);
                }
            }
        }
    });
    return convertView;
}

2 个答案:

答案 0 :(得分:0)

如果ListView的某些行在某些情况下会有所不同,则可以使用RecyclerView代替。 RecyclerView.Adapter有一个方法,允许您决定每行的视图类型。请参阅:getItemViewType()

实施例:

public class ExampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @Override
    public int getItemViewType(int position) {
        //If position is divisible by three means that is a PrettyRow :)
        if(position%3 == 0)
            return 1;
        else
            return 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1: return new PrettyViewHolder();
            case 2: return new UglyViewHolder();
        }
    }

    class PrettyViewHolder extends RecyclerView.ViewHolder {
        //TODO: set holder attributes, like a TextView or ImageView
    }

    class UglyViewHolder extends RecyclerView.ViewHolder {
        //TODO: set holder attributes, like a TextView or ImageView
    }
}

有关RecyclerListView的更多信息,请检查this example

答案 1 :(得分:0)

这是一个常见的错误。 getView()中的null检查所需要的只是:

if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.temp, parent, false);
}

原因是convertView may = null,即使行项在某一点可见,但它不再被回收(由于虚拟内存方案)。因此,尽管你已经看到了convertView = null。所以需要这样的代码。

} else {
            viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder = new ViewHolder();

viewHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);

我希望这很清楚。