我从外部数据库下载数据并在用户向下滚动时填充数组。我正在下载数据时显示页脚视图(loadmoreview)。目前我所处的位置是在用户到达列表末尾时删除此页脚视图,而且没有更多数据可供下载。
下载:
private void DownloadUsersClassic(final String start, final String finish) {
loadingMore = true;
Log.i("DownloadUsersClassic", "started");
StringRequest postReq = new StringRequest(Request.Method.POST, download_leaderboard_classic, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.length() > 10) {
//populate arrays from json
if (arr_userid.size() < 14) {
//set adapter the first time the user scrolls
mAdapter = new CustomAdapter(getActivity(), arr_userid, arr_username, arr_photo, arr_level, arr_date, true);
rv.setAdapter(mAdapter);
} else {
//call notifyDataSetChanged() at the other scrolls
mAdapter.notifyDataSetChanged();
}
else {
//there is no more data to download
mAdapter = new CustomAdapter(getActivity(), arr_userid, arr_username, arr_photo, arr_level, arr_date, false);
mAdapter.notifyDataSetChanged();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("VOLLEY_ERROR", error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("start", start);
params.put("finish", finish);
return params;
}
};
适配器:
public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<String> arr_userid = new ArrayList<String>();
ArrayList<String> arr_username = new ArrayList<String>();
ArrayList<String> arr_photo = new ArrayList<String>();
ArrayList<String> arr_level = new ArrayList<String>();
ArrayList<String> arr_date = new ArrayList<String>();
boolean show_footer;
public LayoutInflater inflater;
public static final int TYPE_ITEM = 1;
public static final int TYPE_FOOTER = 2;
Context context;
public CustomAdapter(Context context, ArrayList<String> arr_userid, ArrayList<String> arr_username, ArrayList<String> arr_photo, ArrayList<String> arr_level, ArrayList<String> arr_date, boolean show_footer) {
super();
this.arr_userid = arr_userid;
this.arr_username = arr_username;
this.arr_photo = arr_photo;
this.arr_level = arr_level;
this.arr_date = arr_date;
this.show_footer = show_footer;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class GenericViewHolder extends RecyclerView.ViewHolder {
public TextView txtName, txtPoints, txtDate;
public ImageView ivUser;
public LinearLayout llLoadmore;
public GenericViewHolder(View v) {
super(v);
txtName = (TextView) v.findViewById(R.id.txtName);
txtPoints = (TextView) v.findViewById(R.id.txtPoints);
txtDate = (TextView) v.findViewById(R.id.txtDate);
ivUser = (ImageView) v.findViewById(R.id.ivUser);
llLoadmore = (LinearLayout) v.findViewById(R.id.loadmore);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
TextView tvloadmore;
public FooterViewHolder (View itemView) {
super (itemView);
this.tvloadmore = (TextView) itemView.findViewById (R.id.tvloadmore);
}
}
public void add(int position, String item) {
arr_userid.add(position, item);
notifyItemInserted(position);
}
public void remove(String item) {
int position = arr_userid.indexOf(item);
arr_userid.remove(position);
notifyItemRemoved(position);
}
public CustomAdapter(ArrayList<String> myDataset) {
arr_userid = myDataset;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if(viewType == TYPE_FOOTER) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.loadmore, parent, false);
return new FooterViewHolder (v);
} else if(viewType == TYPE_ITEM) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.leaderboard_row, parent, false);
return new GenericViewHolder (v);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof FooterViewHolder) {
FooterViewHolder footerHolder = (FooterViewHolder) holder;
footerHolder.tvloadmore.setText("Footer");
footerHolder.tvloadmore.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
Toast.makeText (getActivity(), "Clicked Footer", Toast.LENGTH_SHORT).show ();
}
});
} else if(holder instanceof GenericViewHolder) {
GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
final String name = arr_username.get(position);
genericViewHolder.txtName.setText(arr_username.get(position));
genericViewHolder.txtPoints.setText(arr_level.get(position) + " Levels");
String year = arr_date.get(position).substring(0, 4);
String month = arr_date.get(position).substring(5, 7);
String day = arr_date.get(position).substring(8, 10);
genericViewHolder.txtDate.setText(month + "." + day + "." + year);
if (!arr_photo.get(position).equals("")) {
Picasso.with(getActivity())
.load(arr_photo.get(position))
.into(genericViewHolder.ivUser);
} else {
genericViewHolder.ivUser.setImageDrawable(null);
}
}
}
@Override
public int getItemCount() {
if (show_footer) {
return arr_userid.size() + 1; //+1 is for the footer as it's an extra item
} else {
return arr_userid.size();
}
}
@Override
public int getItemViewType (int position) {
if (show_footer) {
if (isPositionFooter(position)) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
} else {
return TYPE_ITEM;
}
}
private boolean isPositionFooter (int position) {
return position == arr_userid.size ();
}
}
onScrollListener :
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = rv.getChildCount();
totalItemCount = llm.getItemCount();
firstVisibleItem = llm.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached
//we need the -1 otherwise 1 item will be missed the first time we scroll. I don't know why only the first
//time but the point it's working this way
DownloadUsersClassic(String.valueOf(totalItemCount-1), String.valueOf(num_loadfinish));
loading = true;
}
}
});
请参阅适配器末尾的布尔变量?我试图用它告诉适配器我是否需要页脚或者我不需要。 我下载数据时确实如此:
mAdapter = new CustomAdapter(getActivity(), arr_userid, arr_username, arr_photo, arr_level, arr_date, true);
到达目的地时它是错误的:
mAdapter = new CustomAdapter(getActivity(), arr_userid, arr_username, arr_photo, arr_level, arr_date, false);
事情是,没有任何反应。页脚停留在那里。我无法打电话
mAdapter = new CustomAdapter(getActivity(), arr_userid, arr_username, arr_photo, arr_level, arr_date, false);
rv.setAdapter(mAdapter);
因为它会重新初始化适配器并跳转到顶部。
答案 0 :(得分:1)
适配器类的修改很少: -
@Override
public int getItemViewType(int position) {
return dataSet.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
在您调用loadMore
时,将null作为项目插入: -
dataSet.add(null);
mAdapter.notifyItemInserted(dataSet.size() - 1);
在loadMore完成后从recyclerView中删除项目: -
dataset.remove(dataSet.size() - 1);
mAdapter.notifyItemRemoved(dataSet.size());
这就是全部。 希望这有帮助
答案 1 :(得分:0)
mAdapter.hideFooter();
mAdapter.notifyDataSetChanged();
将此添加到适配器:
public void hideFooter() {
show_footer = false;
}
这告诉适配器在末尾隐藏页脚。
查看this tutorial以获取完整代码。