码迷,mamicode.com
首页 > 其他好文 > 详细

4解决图片乱跳的问题,preferenceactivity.

时间:2015-10-07 16:06:34      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

设置界面

listview中图片异步加载时,图片乱跳错位的现象
原因:这是由于在代码中复用了convertview,同时也复用了convertview中的bitmap,
解决:
①不用convertview缓存,但程序的效率打折扣
②可以用convertview缓存,在xml中不配置ImageView,在代码中手动添加
③自定义一个ImageView,同时具有异步加载的功能
技术分享
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/category_selector"
    android:gravity="center_vertical"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
        <LinearLayout
            android:id="@+id/ll_book_image"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <RatingBar
                android:id="@+id/ratingbar"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_width="65.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:isIndicator="true"
                android:numStars="5"
                android:paddingTop="3dip"
                android:stepSize="0.5" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingLeft="15dip" >
            <TextView
                android:id="@+id/book_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxHeight="50dip"
                android:textColor="@android:color/black"
                android:textSize="20dp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/book_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxHeight="60dip"
                android:textColor="@android:color/black"
                android:textSize="15dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>  
考虑如果不设置ratingbar样式有影响吗?

        public View getView(int position, View convertView, ViewGroup parent) {
            NewBook newbook = newbooks.get(position);
            View view;
            if (convertView == null) {
                view = View.inflate(NewBookActivity.this,
                        R.layout.new_book_itemnull);
            } else {
                view = convertView;
            }
            final ImageView iv = new ImageView(NewBookActivity.this);
            // RatingBar rb = new RatingBar(NewBookActivity.this);
            RatingBar rb = new RatingBar(NewBookActivity.thisnull,
                    android.R.attr.ratingBarStyleSmall);
            rb.setMax(5);
            rb.setProgress(4);
            LinearLayout ll = (LinearLayout) view
                    .findViewById(R.id.ll_book_image);
            // 清空ll的里面的view对象
            ll.removeAllViews();
            ll.addView(iv, new LayoutParams(60, 60));
            ll.addView(rb, new LayoutParams(60, LayoutParams.WRAP_CONTENT));
            TextView tv_title = (TextView) view.findViewById(R.id.book_title);
            TextView tv_description = (TextView) view
                    .findViewById(R.id.book_description);
            rb.setRating(4.0f);
            tv_title.setText(newbook.getName());
            tv_description.setText(newbook.getDescription());
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            boolean canloadicon = sp.getBoolean("canloadicon"false);
            if (canloadicon) {
                LoadImageAsynTask task = new LoadImageAsynTask(
                        new LoadImageAsynTaskCallback() {
                            public void beforeLoadImage() {
                                iv.setImageResource(R.drawable.book);
                            }
                            public void afterLoadImage(Bitmap bitmap) {
                                if (bitmap != null) {
                                    iv.setImageBitmap(bitmap);
                                } else {
                                    iv.setImageResource(R.drawable.book);
                                }
                            }
                        });
                task.execute(newbook.getIconpath());
            } else {
                iv.setImageResource(R.drawable.book);
            }
            return view;
        }
    }  

SharedpreferenceActivity
设置界面







4解决图片乱跳的问题,preferenceactivity.

标签:

原文地址:http://www.cnblogs.com/linmbbest/p/4858685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!