gjiazhe / layoutswitch Goto Github PK
View Code? Open in Web Editor NEWA sample showing an easy way to make switch animation from list layout to grid layout with RecyclerView.
License: MIT License
A sample showing an easy way to make switch animation from list layout to grid layout with RecyclerView.
License: MIT License
The current implementation inflates both small and large layout and only uses one of those which is wasteful and an antipattern.
Which layout to use should be determined at the time of creating a ViewHolder not at the time of binding data to it.
Here's the updated version of the Adapter:
@Override
public int getItemViewType(int position) {
final int spanCount = mLayoutManager.getSpanCount();
if (spanCount > 1) {
return R.layout.item_small;
} else {
return R.layout.item_large;
}
}
@Override
public ItemViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
final Item item = mItems.get(position % 4);
holder.title.setText(item.getTitle());
holder.iv.setImageResource(item.getImgResId());
holder.info.setText(item.getLikes() + " likes · " + item.getComments() + " comments");
}
static class ItemViewHolder extends RecyclerView.ViewHolder {
final ImageView iv;
final TextView title;
final TextView info;
ItemViewHolder(View itemView) {
super(itemView);
iv = (ImageView) itemView.findViewById(R.id.image);
title = (TextView) itemView.findViewById(R.id.title);
info = (TextView) itemView.findViewById(R.id.tv_info);
}
}
先切换layout 然后滑动recyclerView 再点切换 再滑动报错
java.lang.NullPointerException: java.lang.NullPointerException
at com.gjiazhe.layoutswitch.ItemAdapter.onBindViewHolder(ItemAdapter.java:47)
at
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.