RecyclerView滚动被弄乱了(在大位图的情况下)

时间:2018-08-06 06:40:00

标签: android android-recyclerview

GIF of what's happening to my RecyclerView

P.S。屏幕底部的大聊天气泡是图像(就像您通过WhatsApp和其他消息传递应用程序发送图像时一样)

我正在制作一个简单的聊天应用程序。问题是,每当我滚动到顶部时,视图就会回到最底部。这是我的OnBindViewHolder代码(由于太长而省略了一些部分):

@Override
public void onBindViewHolder(final MessageViewHolder viewHolder, final int i) {
    final ChatMessages c = messageList.get(i);
    final String from_user = c.getFrom();
    String message_type = c.getType();

    if (currentUser.equals(from_user)) {
        viewHolder.mydisplayName.setText(dummyright);
    } else {
        viewHolder.displayName.setText(dummyleft);
    }
            if (currentUser.equals(from_user)) {
        leftLayout.setVisibility(View.VISIBLE);
        rightLayout.setVisibility(View.GONE);
        person = "leftLayout";
    } else {
        leftLayout.setVisibility(View.GONE);
        rightLayout.setVisibility(View.VISIBLE);
        person = "rightLayout";
    }

    if (message_type.equals("text")) {
        //set visibility of TextViews and other elements according to which user is sending the message (left/right)
    } else if (message_type.equals("image")) {
        if (person.equals("leftLayout")) {
            viewHolder.mymessageText.setVisibility(View.GONE);
            viewHolder.myview_data.setVisibility(View.GONE);
            viewHolder.mymessageImage.setVisibility(View.VISIBLE);
            Cursor cursor = null;
            final String tempUri = c.getDownload_link();
            filename = null;

            byte[] decodedString = Base64.decode(c.getMessage(), Base64.DEFAULT);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inSampleSize=512;
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length,options);

            try {
                Glide.with(viewHolder.mymessageImage.getContext())
                        .asBitmap()
                        .load(decodedString)
                        .thumbnail(0.5f)
                        .into(viewHolder.mymessageImage);
                decodedString=null;
            } catch (Exception e) {
                Log.e("chat image right", e.getMessage());
            }

            File file = null;
            if (tempUri != null) {
                file = new File(tempUri);
                String path = file.getAbsolutePath();

                if (tempUri.startsWith("content://")) {
                    try {
                        cursor = context.getContentResolver().query(Uri.parse(c.getDownload_link()), null, null, null, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                        }
                    } finally {
                        cursor.close();
                    }
                } else if (tempUri.startsWith("file://")) {
                    filename = file.getName();
                }

                viewHolder.mymessageImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
                        } else {
                            builder = new AlertDialog.Builder(context);
                        }
                        builder.setTitle("Save Image")
                                .setMessage("Do you want to save this image?")
                                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // continue with delete
                                        startSaveImageToGallery();
                                    }
                                })
                                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // do nothing
                                    }
                                })
                                .setIcon(android.R.drawable.ic_dialog_alert)
                                .show();
                    }
                });
            } else {
            }

        } else if (person.equals("rightLayout")) {
           //same as above but for the right layout
        }
    } 
}

我已经阅读了很多有关此问题的问题,其中一些回答说这是由于在onBindViewHolder中设置可见性引起的。我不知道还要在哪里设置它们,因为布局的可见性取决于某些数据(在这种情况下,是谁发送的聊天记录)。

编辑:我刚刚尝试了我的应用几次,显然只有在发送图片时,它才会跳到底部,尤其是当它们的大小为600KB或更大时。我正在用Glide加载图像,它们存储在Base64中并转换为压缩的位图。这些图像会导致混乱的滚动吗?

1 个答案:

答案 0 :(得分:0)

1。如果ImageView是wrap_content,请为其设置一个固定的大小值。

2。如果OnBindViewHolder方法还有其他与UI相关的异步操作,请在setData之前与它们进行交易。

3。检查您的代码,是否有诸如scroll to

之类的方法