我有一个我用Volley创建的JSON List RecyclerView。我的recyclerview有大约1000个项目,但我只想显示50个项目,但仍然从JSON加载所有项目,因此当用户使用搜索工具时,它将搜索整个JSON数据。
我可以让我的recyclerview显示我想要的数量,50,但是当我使用搜索工具时我只能搜索50.ive寻找答案但似乎无法找到任何关于如何加载所有项目但只显示50.有人可以帮忙吗?
MainActivity 的
public class HomeFragment extends Fragment {
public static final String URL_DATA = "https://api.coinmarketcap.com/v1/ticker/";
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;
private RecyclerView recyclerView;
private EditText SearchText;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
listItems = new ArrayList<>();
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
SearchText = (EditText) v.findViewById(R.id.search_text);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
loadRecyclerViewData();
SearchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
filter(editable.toString());
}
});
return v;
}
private void filter(String text){
List<ListItem> filteredList = new ArrayList<>();
for(ListItem item :listItems ){
if (item.getHead().toLowerCase().contains(text.toLowerCase()) && item.getDesc().toLowerCase().contains(text.toLowerCase())){
filteredList.add(item);
}
}
adapter = new MyAdapter(filteredList, getContext());
recyclerView.setAdapter(adapter);
}
private void loadRecyclerViewData() {
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading Data...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONArray array = new JSONArray(response);
for(int i=0; i < array.length(); i++){
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("name"),
);
listItems.add(item);
}
adapter = new MyAdapter(listItems, getContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
}
MyAdapter 的
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems;
private Context context;
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
final ListItem listItem = listItems.get(position);
holder.textViewHead.setText(listItem.getHead());
holder.textViewDesc.setText(listItem.getDesc());
public void filterList(ArrayList<ListItem> filteredList){
this.listItems = filteredList;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewHead;
public TextView textViewDesc;
public ViewHolder(View itemView) {
super(itemView);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
}
}
}
答案 0 :(得分:0)
显示的项目数由RecyclerView.Adapter.getItemCount()