RecyclerView从不关注最后一个元素

时间:2019-09-27 12:57:46

标签: android android-recyclerview recycler-adapter

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

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

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

我的recyclerView首先滚动到最后一个元素,然后将新元素添加到我的数组中。因此,这样就永远不会真正到达最后一个元素。应该相反。

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

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);
        }

    }

1 个答案:

答案 0 :(得分:-1)

您的代码应如下所示:

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

    mAdapter.setmListOfPhotos(mListOfPhotos);

    mRecyclerView.setAdapter(mAdapter);

    mRecyclerView.smoothScrollToPosition(mListOfPhotos.size());

    //mAdapter.notifyDataSetChanged();
}