Searchview在android中无休止的滚动

时间:2017-04-16 13:59:23

标签: android android-recyclerview searchview infinite-scroll endlessscroll

我的适配器

public class AdapterItem extends RecyclerView.Adapter<RecyclerView.ViewHolder>  { 
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;

private ArrayList<Item> itemList;

private OnLoadMoreListener onLoadMoreListener;
private LinearLayoutManager mLinearLayoutManager;

private boolean isMoreLoading = false;
private int visibleThreshold = 1;
int firstVisibleItem, visibleItemCount, totalItemCount;

public interface OnLoadMoreListener{
    void onLoadMore();
}

public AdapterItem(OnLoadMoreListener onLoadMoreListener) {
    this.onLoadMoreListener=onLoadMoreListener;
    itemList =new ArrayList<>();
}

public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager){
    this.mLinearLayoutManager=linearLayoutManager;
}

public void setRecyclerView(RecyclerView mView){
    mView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            visibleItemCount = recyclerView.getChildCount();
            totalItemCount = mLinearLayoutManager.getItemCount();
            firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
            if (!isMoreLoading && (totalItemCount - visibleItemCount)<= (firstVisibleItem + visibleThreshold)) {
                if (onLoadMoreListener != null) {
                    onLoadMoreListener.onLoadMore();
                }
                isMoreLoading = true;
            }
        }
    });
}

@Override
public int getItemViewType(int position) {
    return itemList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
    if (viewType == VIEW_ITEM) {
        return new StudentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false));
    } else {
        return new ProgressViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false));
    }

}

public void addAll(List<Item> lst){
    itemList.clear();
    itemList.addAll(lst);
    notifyDataSetChanged();
}

public void addItemMore(List<Item> lst){
    itemList.addAll(lst);
    notifyItemRangeChanged(0,itemList.size());
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof StudentViewHolder) {
        Item singleItem = (Item) itemList.get(position);
        ((StudentViewHolder) holder).tvItem.setText(singleItem.getItem());
    }
}

public void setMoreLoading(boolean isMoreLoading) {
    this.isMoreLoading=isMoreLoading;
}

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

public void setProgressMore(final boolean isProgress) {
    if (isProgress) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                itemList.add(null);
                notifyItemInserted(itemList.size() - 1);
                }
            });
    } else {
        itemList.remove(itemList.size() - 1);
        notifyItemRemoved(itemList.size());
    }
}

static class StudentViewHolder extends RecyclerView.ViewHolder {
    public TextView tvItem;

    public StudentViewHolder(View v) {
        super(v);
        tvItem = (TextView) v.findViewById(R.id.tvItem);
    }
}

static class ProgressViewHolder extends RecyclerView.ViewHolder {
    public ProgressBar pBar;
    public ProgressViewHolder(View v) {
        super(v);
        pBar = (ProgressBar) v.findViewById(R.id.pBar);
    }
}}

我的活动

public class MainActivity extends AppCompatActivity implements AdapterItem.OnLoadMoreListener
            ,SwipeRefreshLayout.OnRefreshListener{
/EndlessRecyclerView

private AdapterItem mAdapter;
private ArrayList<Item> itemList;
private SwipeRefreshLayout swipeRefresh;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    itemList = new ArrayList<Item>();
    swipeRefresh=(SwipeRefreshLayout)findViewById(R.id.swipeRefresh);
    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.rvList);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new AdapterItem(this);
    mAdapter.setLinearLayoutManager(mLayoutManager);
    mAdapter.setRecyclerView(mRecyclerView);
    mRecyclerView.setAdapter(mAdapter);
    swipeRefresh.setOnRefreshListener(this);

    loadData();
    final EditText EditTxtFinancialCode = (EditText) findViewById(R.id.search);


    EditTxtFinancialCode.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            s = s.toString().toLowerCase();
            final ArrayList<Item> filteredList = new ArrayList<>();

            for (int i = 0; i < itemList.size(); i++) {

                final String text = itemList.get(i).getItem().toLowerCase();
                if (text.contains(s)) {
                    filteredList.add(itemList.get(i));
                }
            }

            mAdapter.setMoreLoading(false);
            mAdapter.setProgressMore(false);
            mAdapter.addAll(filteredList);
            mAdapter.notifyDataSetChanged();


        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });


}

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

}

@Override
public void onRefresh() {
    Log.d("MainActivity_","onRefresh");
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            swipeRefresh.setRefreshing(false);
            loadData();

        }
    },2000);
}

@Override
public void onLoadMore() {
    Log.d("MainActivity_","onLoadMore");
    mAdapter.setProgressMore(true);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mAdapter.setProgressMore(false);
            int start = mAdapter.getItemCount();
            int end = start + 15;
            if(end<70) {
                for (int i = start + 1; i <= end; i++) {
                    itemList.add(new Item("Item " + i));
                }
                mAdapter.addAll(itemList);
            }
            mAdapter.setMoreLoading(false);
        }
    },1000);
}

private void loadData() {
    itemList.clear();
    for (int i = 1; i <= 20; i++) {
        itemList.add(new Item("Item " + i));
    }
    mAdapter.addAll(itemList);
}}

我想问一下如何在无休止的滚动中创建搜索视图?

我尝试过但失败了,在运行搜索视图时,总是会出现加载更多数据和数据加载的问题,因此我创建的代码是否不正确? 如果它可以帮助我克服这个问题

由于

1 个答案:

答案 0 :(得分:0)

建议 - 不要这样做,这只会令人困惑

但要做到这一点,你需要: 1.公开int getItemCount() {返回无穷大,或至少为no1滚动到它的数字 2.在public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {上点“如果位置多于物品数,那么从位置移除物品计数并进行到位置少于物品数

如果需要,this example包含可搜索的recyclerview,没有无限滚动

修改

onbind:

while(items.size() < position){
   position -= items.size();
}
textView.setText(items.get(position));

应该给无限滚动,没有测试过它....但我仍然认为无限滚动是愚蠢的想法