过滤后的错误位置和在Recyclerview中单击

时间:2017-12-05 04:49:17

标签: java android android-recyclerview

我在我的应用程序中实现了SearchView。但我面临的问题是回收器视图在过滤后返回错误的位置。如何在过滤后获得准确的位置。

这是我的适配器代码: -

$('#phone').val('123-123-1234')
if ($('#phone').val().length > 0)
  $('#btnSubmit').removeAttr('disabled');
else
  $('#btnSubmit').attr('disabled', 'disabled');
$('#phone').keyup(function() {
  if ($(this).val() != '') {
    $('#btnSubmit').removeAttr('disabled');
  } else {
    $('#btnSubmit').attr('disabled', 'disabled');
  }
});

我调用getPosition()方法获取项目的位置,但它返回旧位置,而不是过滤位置。任何帮助都会得到赞赏。

2 个答案:

答案 0 :(得分:0)

使用此类在recyclerview到达结尾之前实现滚动侦听器。它会在剩下5个项目滚动时通知。请参阅下面的代码。

将它作为一个单独的类

放入您的项目中
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;


public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener
{
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 0;

private StaggeredGridLayoutManager mLinearLayoutManager;
private LinearLayoutManager mLinearLayoutManagerG;

public EndlessRecyclerOnScrollListener(StaggeredGridLayoutManager linearLayoutManager)
{
    this.mLinearLayoutManager = linearLayoutManager;
}


public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager)
{
    this.mLinearLayoutManagerG = linearLayoutManager;
    mLinearLayoutManager = null;
}


public void reset()
{
    current_page = 0;
    previousTotal = 0;
    loading = true;
}

public void setThresHold(int threshold)
{
    visibleThreshold = threshold;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
    super.onScrolled(recyclerView, dx, dy);

    if (mLinearLayoutManager != null)
    {
        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLinearLayoutManager.getItemCount();
        firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPositions(null)[0];

        if (loading)
        {
            if (totalItemCount > previousTotal)
            {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold))
        {
            // End has been reached

            // Do something
            current_page++;

            onLoadMore(current_page);

            loading = true;
        }
    }
    else
    {
        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLinearLayoutManagerG.getItemCount();
        firstVisibleItem = mLinearLayoutManagerG.findFirstVisibleItemPosition();

        if (loading)
        {
            if (totalItemCount > previousTotal)
            {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold))
        {
            // End has been reached

            // Do something
            current_page++;

            onLoadMore(current_page);

            loading = true;
        }

    }

    countChange(firstVisibleItem);
}

public abstract void onLoadMore(int current_page);

public abstract void countChange(int currentVisibleCount);
}

像这样使用

endlessRecyclerOnScrollListener = new EndlessRecyclerOnScrollListener(pass your layout manager here)
    {
        @Override
        public void onLoadMore(int current_page) {
            pageNumber++;
            // Hit your api for more records 
        }

        @Override
        public void countChange(int currentVisibleCount) {

        }
    };

    recyclerView.setOnScrollListener(endlessRecyclerOnScrollListener);

有一个名为current_page的变量,默认情况下它从0开始,您可以根据您的api进行更改。 visibleThreshold表示在调用onLoadMore方法的项目数之前。

示例:如果visibleThreshold为5且列表中的总数据为20,则当您的recyclerview向上滚动到第15个位置时,将调用onLoadMore

如果您有更多疑问,请与我们联系。

答案 1 :(得分:0)

在onClick方法中使用getAdapterPosition(),因为在点击时你想知道项目的位置。如果你在创建时检查它(大多数时候)将是不准确的