Glide和RecyclerView闪烁

时间:2016-11-19 15:57:57

标签: android android-recyclerview android-glide

我创建小应用程序来显示我的问题。 你可以在https://github.com/Anton111111/ExampleGlideBlink看到它。

主要课程:https://github.com/Anton111111/ExampleGlideBlink/blob/master/app/src/main/java/com/example/testglide/MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Adapter adapter;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
        rv.setHasFixedSize(true);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        rv.setLayoutManager(gridLayoutManager);

        ArrayList<String> items = new ArrayList<>(Arrays.asList(new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"
        }));

        adapter = new Adapter(this, items);
        rv.setAdapter(adapter);
        handler = new Handler();
        handler.postDelayed(r, 500);
    }

    Runnable r = new Runnable() {
        @Override
        public void run() {
            Random rand = new Random(System.currentTimeMillis());
            handler.postDelayed(r, 500);
            adapter.notifyItemChanged(rand.nextInt(4));
        }
    };

    public class Adapter extends RecyclerView.Adapter {

        private final Context ctx;
        private ArrayList<String> items = new ArrayList<>();
        private int progress = 1;

        public Adapter(Context context, ArrayList<String> items) {
            this.items = items;
            ctx = context;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item, parent, false);
            Holder h = new Holder(v);
            return h;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((Holder) holder).getProgress().setText(progress+"");
            progress++;
            Glide.with(ctx)
                    .load(Uri.parse("file:///android_asset/" + (position + 1) + ".png"))
                    .into(((Holder) holder).getImage1());
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        public class Holder extends RecyclerView.ViewHolder {

            private final ImageView image1;
            private final TextView progress;

            public ImageView getImage1() {
                return image1;
            }

            public TextView getProgress() {
                return progress;
            }

            public Holder(View itemView) {
                super(itemView);
                image1 = (ImageView) itemView.findViewById(R.id.image1_view);
                progress = (TextView) itemView.findViewById(R.id.progress_text);
            }
        }
    }

}

主要布局: https://github.com/Anton111111/ExampleGlideBlink/blob/master/app/src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testglide.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

项目布局: https://github.com/Anton111111/ExampleGlideBlink/blob/master/app/src/main/res/layout/item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image1_view"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <TextView
        android:id="@+id/progress_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

当我在RecyclerView适配器上调用notifyItemChanged时,我需要仅更改文本(在我的示例中,它是带有ID为R.id.progress的EditText)并且图像显示没有更改。但是它眨了眨眼睛。

我是否能够在不闪烁的情况下更改文本?

1 个答案:

答案 0 :(得分:0)

使用方形布局(如

)封闭行布局文件
public class SquaredFrameLayout extends FrameLayout {
public SquaredFrameLayout(Context context) {
    super(context);
}

public SquaredFrameLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquaredFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SquaredFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}}

然后在你的xml中

<com.app_views.SquaredFrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_answer_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="@{group.onHashTagPhotoClicked}"
        android:paddingEnd="1dp"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingStart="1dp"
        android:paddingTop="1dp"
        app:galleryImage="@{group.boothPhoto}" />
</com.app_views.SquaredFrameLayout>