smoothScrollToPosition()在RecyclerView中间歇性地工作

时间:2018-05-31 12:22:04

标签: android android-recyclerview

我的一项活动中有评论部分。用户可以发表评论。评论根据需要位于RecyclerView(索引0)的顶部。我正在调用smoothScrollToPosition(0)上的RecyclerView。这只能间歇性地工作。它的工作原理是,recylerview已经在帖子被评论之前滚动了。然后它滚动到索引0 ...新评论。但是,如果在发布评论之前显示项index 0,则smoothScrollToPosition不起作用。当我手动滚动时,评论在索引0处是预期的。任何建议?

修改

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        ButterKnife.bind(this);

        mCommnetsAdapter = new CommentsAdapter(commentsQuery) {

            @Override
            public void onDataChanged() {

                if (getItemCount() == 0) {
                    mRecycler.setVisibility(View.GONE);
                    //mEmptyView.setVisibility(View.VISIBLE);
                } else {
                    mRecycler.setVisibility(View.VISIBLE);
                    //mEmptyView.setVisibility(View.GONE);
                }
            }
        };



        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(mCommnetsAdapter);





    }

    @Override
    protected void onStart() {
        super.onStart();
        mCommnetsAdapter.startListening();

        mMatchRegistration= mMatchRef.addSnapshotListener(this);

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mMatchRegistration !=null){

            mMatchRegistration.remove();
            mMatchRegistration=null;

        }
    }



    @Override
    public void onRating(Rating rating) {

        addRating(mMatchRef, rating)
                .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "Rating added");

                        // Hide keyboard and scroll to top
                        hideKeyboard();
                        mRecycler.smoothScrollToPosition(0);

        });


    }

我有一个基本适配器类,其中实现了notifyItemChange()。

public abstract class FirestoreAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH>
        implements EventListener<QuerySnapshot> {
.....

public void stopListening() {
        if (mRegistration != null) {
            mRegistration.remove();
            mRegistration = null;
        }

        mSnapshots.clear();
        notifyDataSetChanged();
    }


    public void setQuery(Query query) {
        // Stop listening
        stopListening();

        // Clear existinkodig data
        mSnapshots.clear();
        notifyDataSetChanged();

        // Listen to new query
        mQuery = query;
        startListening();
    }.....

My CommentsApapter扩展了这个类。

0 个答案:

没有答案