水平RecyclerView始终滚动到最后一项

时间:2018-03-29 19:23:03

标签: android firebase firebase-realtime-database android-recyclerview

我有一个RecyclerView,设置为horizontal并加载FirebaseRecyclerAdapter。在我设置OnClickListeners的所有项目上。问题是,RecyclerView总是滚动到最后一项。因此,我没有显示第一个项目,而是看到最后一个项目。当我没有设置OnClickListener时,RecyclerView按原样运行,不会滚动到最后一项。

我已尝试setFocusable(false)scrollToPosition(0)

我的代码:

为了使用适配器中的load more button I覆盖方法来使用多个布局,因为FirebaseAdapter不会在Horizo​​ntalScrollView中加载项目,但我认为这不应该是问题。

mStoriesAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(Post.class, R.layout.list_item_outputdetail_story, PostHolder.class, query) {
            @Override
            protected void populateViewHolder(PostHolder viewHolder, final Post model, int position) {

            if (model != null) {

                viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent = new Intent(getActivity(), StoryActivity.class);
                        intent.putExtra(Constants.PASS_POST_TO_STORY_ACTIVITY, model);
                        intent.putExtra(Constants.STORY_ACTIVITY_TYPE, Constants.STORY_ACTIVITY_ONE_STORY);
                        intent.putExtra(Constants.FIREBASE_KEY, firebaseKey);
                        startActivity(intent);

                    }
                });

                viewHolder.name.setText(model.getUser().getUserName());
                viewHolder.heartsNumber.setText(String.valueOf(model.getNumLikes()));
                viewHolder.title.setText(model.getTitleText());
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(model.getTimeCreated());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
                viewHolder.date.setText(simpleDateFormat.format(calendar.getTime()));

                Glide.with(getActivity()).load(model.getUser().getPictureUri()).into(viewHolder.profilPicture);

                FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
                StorageReference imageReference = firebaseStorage.getReference().child(model.getPostId());
                GlideApp.with(getActivity()).load(imageReference).into(viewHolder.imageView);

                FirebaseUtils.checkForAlreadyLiked(model.getPostId(), viewHolder.hearts, firebaseKey);

            } else if (position == 0) {

                viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), WriteStoryActivity.class);
                        intent.putExtra(Constants.FIREBASE_KEY, firebaseKey);
                        startActivity(intent);
                    }
                });

            } else {

                viewHolder.showAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), StoryActivity.class);
                        intent.putExtra(Constants.STORY_ACTIVITY_TYPE, Constants.STORY_ACTIVITY_ALL_STORIES);
                        intent.putExtra(Constants.FIREBASE_KEY, firebaseKey);
                        startActivity(intent);
                    }
                });

            }

        }

        @Override
        public Post getItem(int position) {
            if (position == 0) {
                return null;
            } else if (super.getItemCount() < 5 || position < getItemCount() - 2) {
                return super.getItem(position - 1);
            } else {
                return null;
            }

        }

        @Override
        public int getItemCount() {
            if (super.getItemCount() < 5) {
                return super.getItemCount() + 1;
            } else {
                return super.getItemCount() + 2;
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (super.getItemCount() < 5) {
                if (position == 0) {
                    return R.layout.list_item_outputdetail_story_write_your_own;
                } else {
                    return super.getItemViewType(position);
                }
            } else {
                if (position == 0) {
                    return R.layout.list_item_outputdetail_story_write_your_own;
                } else if (position == getItemCount() - 1) {
                    return R.layout.list_item_outputdetail_story_showall_fab;
                } else  {
                    return super.getItemViewType(position);
                }
            }
        }

    };
    LinearLayoutManager storiesManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    mStoriesRecyclerView.setLayoutManager(storiesManager);
    mStoriesRecyclerView.setAdapter(mStoriesAdapter);

谢谢!

1 个答案:

答案 0 :(得分:0)

任何仍然需要这个的人。答案是您无法在OnClickListener中设置RecyclerView.Adapter

最好的想法是像这篇文章一样做一个解决方法。 RecyclerView onClick