Giter Club home page Giter Club logo

layoutswitch's People

Contributors

gjiazhe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

layoutswitch's Issues

Resolve layout at time of ViewHolder creation

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);
    }
}

NullPointerException after switch layout

先切换layout 然后滑动recyclerView 再点切换 再滑动报错
java.lang.NullPointerException: java.lang.NullPointerException
at com.gjiazhe.layoutswitch.ItemAdapter.onBindViewHolder(ItemAdapter.java:47)
at

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.