RecyclerView从不关注最后一个元素

问题描述 投票:0回答:1

我正在尝试将recyclerview的最后一个元素设置为在拍摄照片时对用户完全可见。

所以我使用了recyclerview.smoothScrollToPosition(recycler.getAdapter()。getItemCount()-1);

但是我总是看到倒数第二张照片。即使我将“-1”更改为“ +1”或“ + 4”,甚至是“ + 15”

有人可以帮忙吗?我想念什么吗?

if (success) {
                File mFile = new File(mDir, new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(new Date()) + ".jpg");

                mImageCapture.takePicture(mFile,
                        new ImageCapture.OnImageSavedListener() {
                            @Override
                            public void onImageSaved(@NonNull File file) {
                                mListOfPhotos.add(file.getAbsolutePath());

                                mAdapter.setmListOfPhotos(mListOfPhotos);
                                mRecyclerView.setAdapter(mAdapter);

                                actualNumberOfPhoto();

                                mRecyclerView.smoothScrollToPosition(mRecyclerView.getAdapter().getItemCount() - 1);
                                mAdapter.notifyDataSetChanged();
                            }

                            @Override
                            public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
                                String mMessage = "Photo capture failed: " + message;
                                Toast.makeText(CameraActivity.this, mMessage, Toast.LENGTH_SHORT).show();

                                assert cause != null;
                                cause.printStackTrace();
                            }
                        });
            }

编辑:

转入actualNumberOfPhoto()的代码

    private void actualNumberOfPhoto(RecyclerView recyclerView) {
        mNumberOfPhotoTV.setText(getResources().getString(R.string.minPhotos, mListOfPhotos.size()));

        mIDDemande = mSharedPreferences.getInt(ConstantsClass.EXTRA_ID_APPLICATION, 0);

        if (mIDDemande != 0) {
            if (mListOfPhotos.size() > 0) {
                mSendPhotoFAB.setVisibility(View.VISIBLE);
            } else if (mListOfPhotos.size() < 1) {
                mSendPhotoFAB.setVisibility(View.GONE);
            }
        } else if (mListOfPhotos.size() >= 6) {
            mSendPhotoFAB.setVisibility(View.VISIBLE);
        } else if (mListOfPhotos.size() < 6) {
            mSendPhotoFAB.setVisibility(View.GONE);
        }

    }

android android-recyclerview recycler-adapter
1个回答
0
投票

您的代码应为:

@Override
public void onImageSaved(@NonNull File file) {
    mListOfPhotos.add(file.getAbsolutePath());

    mAdapter.setmListOfPhotos(mListOfPhotos);

    mRecyclerView.setAdapter(mAdapter);

    mRecyclerView.smoothScrollToPosition(mListOfPhotos.size());

    //mAdapter.notifyDataSetChanged();
}
© www.soinside.com 2019 - 2024. All rights reserved.