我已经实施了recyclerView。如何使用我现有的代码在recyclerView的底部添加页脚progressBar。我知道使用addFooterView(View v)方法可以在listview中实现相同的效果。如何在现有代码中实现相同的目标。我看过了在例如网络上的 - Android 5.0 - Add header/footer to a RecyclerView这样的例子中,但没有正确理解它们以及将它与我现有的代码一起使用的过程。我正在寻找一个更简单的解释。我希望在现有代码中实现此功能。请编辑它并告诉我需要进行哪些更改。
我的适配器
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostHandler> {
private LayoutInflater inflater;
Context context;
public PostAdapter(Context con) {
context = con;
inflater = LayoutInflater.from(context);
}
@Override
public PostHandler onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.post_row, parent, false);
PostHandler postHandler = new PostHandler(v);
return postHandler;
}
@Override
public void onBindViewHolder(final PostHandler holder, final int position) {
//The bind actions set text and others happen here...
}
@Override
public int getItemCount() {
return FreshPostContainer.getInstance().getPosts().size();
}
class PostHandler extends RecyclerView.ViewHolder {
SimpleDraweeView mprofilePic;
TextView mpostAuthor;
;
public PostHandler(View itemView) {
super(itemView);
mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);
}
}
}
MyEndless Scroller
public abstract class EndlessOnScrollRecycler extends RecyclerView.OnScrollListener {
boolean mloading;
private final int AUTO_LOAD_THRESHOLD = 5;
private LinearLayoutManager mlinearLayoutManager;
int firstVisibleItem, visibleItemCount, totalItemCount;
public EndlessOnScrollRecycler(LinearLayoutManager linearLayoutManager) {
this.mlinearLayoutManager = linearLayoutManager;
}
public void setLoading(boolean loading) {
this.mloading = loading;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mlinearLayoutManager.getItemCount();
firstVisibleItem = mlinearLayoutManager.findLastVisibleItemPosition();
if (!mloading) {
if (totalItemCount - AUTO_LOAD_THRESHOLD <= firstVisibleItem + visibleItemCount) {
loading();
setLoading(true);
}
}
}
protected abstract void loading();
}
答案 0 :(得分:0)
您可以覆盖getItemViewType():
@Override
public int getItemViewType(int position) {
// you can define your view type as your requirement
return super.getItemViewType(position);
}
@Override
public PostHandler onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.post_row, parent, false);
if(viewType == yourviewtype) // inflate your layout accordingly
PostHandler postHandler = new PostHandler(v);
return postHandler;
}
并更新你的posthander构造函数:
public PostHandler(View itemView, int viewType) {
super(itemView);
if(viewType == yourrequiredview) {
}else {
mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);
}
}
//final adapter class is as
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostHandler> {
private LayoutInflater inflater;
Context context;
public PostAdapter(Context con) {
context = con;
inflater = LayoutInflater.from(con);
}
@Override
public PostHandler onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if(viewType == 2){
v = inflater.inflate(R.layout.post_row, parent, false);
}else{
v = inflater.inflate(R.layout.progressbar_layout, parent, false);
}
PostHandler postHandler = new PostHandler(v, viewType);
return postHandler;
}
@Override
public void onBindViewHolder(final PostHandler holder, final int position)
{if (getItemViewType(position)==2)
{ //The bind actions set text and others happen here...
}
}
@Override
public int getItemCount() {
return FreshPostContainer.getInstance().getPosts().size();
}
class PostHandler extends RecyclerView.ViewHolder {
SimpleDraweeView mprofilePic;
TextView mpostAuthor;
ProgressBar mProgressBar;
;
public PostHandler(View itemView) {
super(itemView);
mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);
}
public PostHandler(View itemView, int viewType) {
super(itemView);
if(viewType == 2) {
mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);
}else {
mProgressBar = (ProgressBar) itemView.findViewById(R.id.progressbar);
}
}
}
@Override
public int getItemViewType(int position) {
if(position == (getItemCount()-1)){
return 1;
}else {
return 2;
}
}
}