FooterView 到 recyclerView
FooterView to recyclerView
我已经实现了一个 recyclerView 。如何使用现有代码在我的 recyclerView 底部添加页脚进度栏。我知道可以在列表视图中使用 addFooterView(View v) method.How 实现相同的功能我可以在此现有代码中实现相同的功能吗?我在网络上看过类似 Android 5.0 - Add header/footer to a RecyclerView 的示例,但没有正确理解它们以及将它与我现有的 code.I 一起使用的过程,我正在寻找更简单的解释。我想在我现有的 code.Please 中实现此功能编辑它并告诉我需要进行哪些更改。
我的适配器
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);
}
}
}
我的无尽卷轴
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();
}
您可以覆盖 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;
}
}
}
我已经实现了一个 recyclerView 。如何使用现有代码在我的 recyclerView 底部添加页脚进度栏。我知道可以在列表视图中使用 addFooterView(View v) method.How 实现相同的功能我可以在此现有代码中实现相同的功能吗?我在网络上看过类似 Android 5.0 - Add header/footer to a RecyclerView 的示例,但没有正确理解它们以及将它与我现有的 code.I 一起使用的过程,我正在寻找更简单的解释。我想在我现有的 code.Please 中实现此功能编辑它并告诉我需要进行哪些更改。
我的适配器
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);
}
}
}
我的无尽卷轴
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();
}
您可以覆盖 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;
}
}
}