ScrollToPosition()在带有图像的RecyclerView上运行不正常

时间:2015-11-24 10:32:29

标签: android android-recyclerview

我有一个像这样的RecyclerView页面:

 <android.support.v7.widget.RecyclerView
                    android:id="@+id/rv_chat"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

RecyclerView项目的xml是ImageView:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/iv_chat_item_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxHeight="@dimen/height_xl"
        android:maxWidth="@dimen/width_l"
        android:scaleType="centerInside"
        android:src="?attr/chat_image_loading" />

适配器:

public ChatAdapter(Context context, List<String> urls)
{
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

         //download image from internet
         downloadImage(url);
    }
}

MainActivity:

    //image urls
    List<String> urls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //initialize recyclerview
        chatListView = (RecyclerView) findViewById(R.id.rv_chat);
        chatListView.setHasFixedSize(true);
        chatListView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        chatListView.setItemAnimator(new DefaultItemAnimator());

        chatAdapter = new ChatAdapter(this,null);
        chatListView.setAdapter(chatAdapter);

        //add datas
        chatAdapter.addDatas(urls);

        //scroll to bottom
        chatListView.smoothScrollToPosition(chatListView.getBottom());
    }

我只是想这样做:下载图像后,让recyclerview滚动到底部。但由于异步图像加载,recyclerview无法正确滚动到底部 - 它只是滚动到中间。那我该如何才能让它运作良好呢?

2 个答案:

答案 0 :(得分:1)

下载前获取图像尺寸,并在视图中设置空白图像。

答案 1 :(得分:0)

  

RecyclerViewScroller:

public class RecyclerViewScroller {
RecyclerView recycler;
LinearLayoutManager manager;
private boolean isMove = false;
private int index = 0;

public RecyclerViewScroller(RecyclerView recycler, LinearLayoutManager manager){
    this.recycler = recycler;
    this.manager = manager;
    recycler.addOnScrollListener(myScrollListener);
}

public void scrollTo(int position){
    recycler.scrollToPosition(position);
    int firstItem = manager.findFirstVisibleItemPosition();
    int lastItem = manager.findLastVisibleItemPosition();
    if (position <= firstItem) {
        recycler.scrollToPosition(position);
    } else if (position <= lastItem) {
        int top = recycler.getChildAt(position - firstItem).getTop();
        recycler.scrollBy(0, top);
    } else {
        recycler.scrollToPosition(position);
        index = position;
        isMove = true;
    }
}

private RecyclerView.OnScrollListener myScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (isMove) {
            isMove = false;
            int n = index - manager.findFirstVisibleItemPosition();
            if (0 <= n && n < recycler.getChildCount()) {
                int top = recycler.getChildAt(n).getTop();
                recycler.scrollBy(0, top);
            }
        }
    }
};

}

  

的活动:

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    chatListView.setLayoutManager(linearLayoutManager);
    RecyclerViewScroller scroller = new RecyclerViewScroller(chatListView, linearLayoutManager);
    scroller.scrollTo(chatListView.getBottom());