当设备旋转时,我试图在recyclerview中滚动到相同的位置 - 虽然保存的位置无法正确显示

时间:2017-03-26 08:45:56

标签: java android scroll android-recyclerview restore

当用户旋转设备时,我正在尝试滚动到Recyclerview中的相同位置。我在这里保存职位:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mPosRev = savedInstanceState.getInt("Pos");
    Log.d(TAG, "onSaveInstanceState: "+mPosRev);
    llm.scrollToPositionWithOffset(mPosRev,10);
}

并在此处恢复:

private final Review mLock = new Review();

private List<Review> mObjects;
public ReviewAdapter( List<Review> objects) {
    mObjects = objects;
}


public void add(Review object) {
    synchronized (mLock) {
        mObjects.add(object);
    }
    notifyDataSetChanged();
}

public void add(ReviewDetail object) {
    synchronized (mLock) {
        try {
            Log.d(TAG, "add: "+object.getContent());
            mObjects.add(new Review(object));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    notifyDataSetChanged();
}

public void clear() {
    synchronized (mLock) {
        mObjects.clear();
    }
    notifyDataSetChanged();
}

@Override
public ReviewViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
    Context context=viewGroup.getContext();
    int layoutIdForListItem=R.layout.item_movie_review;
    LayoutInflater inflater=LayoutInflater.from(context);
    View view=inflater.inflate(layoutIdForListItem, viewGroup, false);
    ReviewViewHolder viewHolder= new ReviewViewHolder(view);
    return viewHolder;
}
@Override
public void onBindViewHolder(ReviewViewHolder holder, int position) {
    holder.bind(position);
}

@Override
public int getItemCount() {
    if (null == mObjects) return 0;
    return mObjects.size();
}

class ReviewViewHolder extends RecyclerView.ViewHolder {
    TextView Author;
    TextView Txt;

    public ReviewViewHolder(View itemView) {
        super(itemView);

        Author = (TextView) itemView.findViewById(R.id.author);
        Txt = (TextView) itemView.findViewById(R.id.txt);
    }

    void bind(int listIndex) {
        Author.setText(mObjects.get(listIndex).getAuthor());
        Txt.setText(mObjects.get(listIndex).getContent());
    }
}

我可以通过日志看到,Position已正确存储,但未滚动到。我已经尝试过smoothScrollToPosition以及scrollToPosition。

这是我的适配器

recList = (RecyclerView) findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);
    mReviewAdapter = new ReviewAdapter(new ArrayList<Review>());
    recList.setAdapter(mReviewAdapter);

}

在这里,我将处理我的Recyclerview:

ref.child("Driver").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                arrayLat = new ArrayList<String>();

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Driver d = postSnapshot.getValue(Driver.class);

                    System.out.println("data snapshot Drivers name is -------------------> "+d.getName());

                    arrayLat.add(Double.toString(d.getLat()));


                }

            }

            /************had to implement this method****************/
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());

            }
        }); 

关于什么可能导致这种行为的任何想法?

0 个答案:

没有答案