在Recyclerview中,如何将初始滚动位置更改为第一项的顶部?

时间:2019-11-24 16:40:53

标签: java android android-recyclerview

我将ItemView放在了RecyclerView中。 ItemView看起来像这样:

enter image description here

ItemView的大小大于屏幕高度的大小。

所以我认为,起初,RecyclerView将显示ItemView的上部:

enter image description here

但是RecyclerView显示了下部:

enter image description here

如何将初始滚动位置更改为ItemView的顶部?

代码如下:

public class TimelineRecyclerViewAdapter extends RecyclerView.Adapter<TimelineRecyclerViewAdapter.ViewHolder> {
    private Post post;

    private ArrayList<Integer> timelineRecyclerViewData = null;
    private ArrayList<Integer> viewPagerData = null;
    private ArrayList<Integer> replyRecyclerViewData = null;
    private Context context;
    private ActivityMain activityMain;

    TimelineRecyclerViewAdapter(ArrayList<Integer> timelineRecyclerViewList,
                                ArrayList<Integer> viewPagerList,
                                ArrayList<Integer> replyRecyclerViewList,
                                Context context,
                                ActivityMain activityMain) {
        timelineRecyclerViewData = timelineRecyclerViewList;
        viewPagerData = viewPagerList;
        replyRecyclerViewData = replyRecyclerViewList;
        this.context = context;
        this.activityMain = activityMain;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ViewPager viewPager;
        RecyclerView replyRecyclerView;
        ViewHolder(View itemView) {
            super(itemView);
            viewPager = itemView.findViewById(R.id.viewPager);
            replyRecyclerView = itemView.findViewById(R.id.replyRecyclerView);
        }
    }

    @Override
    @NonNull
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.post, viewGroup, false);
        post = new Post(view);
        return new ViewHolder(view);
    }

    HashMap<Integer, Integer> viewPagerState = new HashMap<>();

    @Override
    public void onViewRecycled(ViewHolder viewHolder) {
        viewPagerState.put(viewHolder.getAdapterPosition(), viewHolder.viewPager.getCurrentItem());
        super.onViewRecycled(viewHolder);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.setIsRecyclable(false);

        PostViewPagerAdapter postViewPagerAdapter = new PostViewPagerAdapter(viewPagerData);
        viewHolder.viewPager.setAdapter(postViewPagerAdapter);

        post.getMenu().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                PopupMenu popupMenu = new PopupMenu(context, post.getMenu());
                popupMenu.inflate(R.menu.post_menu);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch(item.getItemId()) {
                            case R.id.postMenuEdit:
                                activityMain.switchActivity(ActivityEdit.class);
                            case R.id.postMenuDelete:
                                return true;
                            default:
                                return false;
                        }
                    }
                });
                popupMenu.show();
            }
        });

        post.getProfilePicture().setImageResource(R.drawable.temp_profile);
        post.getId().setText("ID" + position);
        post.getTime().setText("time" + position);
        post.getContent().setText("content" + position);

        if(viewPagerState.containsKey(viewHolder.getAdapterPosition())) {
            viewHolder.viewPager.setCurrentItem(viewPagerState.get(position));
        }

        viewHolder.replyRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        viewHolder.replyRecyclerView.setAdapter(new ReplyRecyclerViewAdapter(replyRecyclerViewData));       // THIS IS THE PROBLEM
    }

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

我发现此代码是问题所在。当我删除它时,问题得以解决:

viewHolder.replyRecyclerView.setAdapter(new ReplyRecyclerViewAdapter(replyRecyclerViewData));

为什么会出现此问题?

1 个答案:

答案 0 :(得分:1)

可能由于以下几个原因而发生:

1)有时recyclerview会自动从第一个位置滚动一点。

解决方案:将此以下属性添加到父级布局。

  android:descendantFocusability="blocksDescendants"

2)其他所有内容

解决方案:mRecyclerView.layoutManager.smoothScrollToPosition(mRecyclerView, null, 0);