'Searchview with endless scrolling in android
My Adapter
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);
}
}}
My Activity
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);
}}
I want to ask how to make a search view in endless scrolling ?
I've tried but failed , when running a search view always appears loading load more and data into a double , so whether the code that I created is not correct? if it could help me to overcome this problem
Thanks
Solution 1:[1]
Suggestion - dont do that, it would be just confusing
BUT to make it, you would need to:
1. make public int getItemCount() {
return infinity, or atleast number that huge that no1 would scroll to it
2. on public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
make something "if position is more then items count then from position remove items count and do it till position is less then items count
if needed, this example contains searchable recyclerview, tho without infinite scroll
edit:
onbind:
while(items.size() < position){
position -= items.size();
}
textView.setText(items.get(position));
that should give infinite scroll, havent tested it.... But i still think that infinite scroll is stupid idea
Solution 2:[2]
Add one line ! May be to help you
recyclerView.setNestedScrollingEnabled(false);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |