Giter Site home page Giter Site logo

asolis90 / android-recyclerview-withheaderandfooter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from u3breeze/android-recyclerview-withheaderandfooter

0.0 2.0 0.0 223 KB

Extension of RecyclerView.Adapter with header/footer. Go for list and grid. Easy to use.

Java 100.00%

android-recyclerview-withheaderandfooter's Introduction

android-RecyclerView-WithHeaderAndFooter

Extension of RecyclerView.Adapter. Can add header/footer to RecyclerView for LinearLayoutManager and GridLayoutManager.

Screenshot

Screenshot Screenshot

Usage

Add Header/Footer View to RecyclerView

Step 1

  • Create Adapter Class which extends HFRecyclerViewAdapter
   class HFAdapter extends HFRecyclerViewAdapter<String, HFAdapter.DataViewHolder>{

        public HFAdapter(Context context) {
            super(context);
        }

        @Override
        public DataViewHolder onCreateDataItemViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.data_item, parent, false);
            return new DataViewHolder(v);
        }

        @Override
        public void onBindDataItemViewHolder(DataViewHolder holder, int position) {
            holder.itemTv.setText(getData().get(position));
        }

        class DataViewHolder extends RecyclerView.ViewHolder{
            TextView itemTv;
            public DataViewHolder(View itemView) {
                super(itemView);
                itemTv = (TextView)itemView.findViewById(R.id.itemTv);
            }
        }
    }

Step 2

  • Use Adapter to add header/footer for RecyclerView
// set Adapter
final HFAdapter hfAdapter = new HFAdapter(this);
recyclerView.setAdapter(hfAdapter);

// add header
View headerView = LayoutInflater.from(this).inflate(R.layout.header, recyclerView, false);
hfAdapter.setHeaderView(headerView);
// add footer
View footerView = LayoutInflater.from(this).inflate(R.layout.footer, recyclerView, false);
hfAdapter.setFooterView(footerView);

// remove header
hfAdapter.removeHeader();
// remove footer
hfAdapter.removeFooter();

// set data
ArrayList<String> data = new ArrayList<>();
for (int i=0; i<8; i++){
    data.add(String.format("Item %d", i));
}
hfAdapter.setData(data);
  • For GridLayoutManager
final GridLayoutManager manager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(manager);
//Set header/footer's spanCount to match parent in horizontal.
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        if ( (hfAdapter.hasHeader() && hfAdapter.isHeader(position)) ||
                hfAdapter.hasFooter() && hfAdapter.isFooter(position) )
            return manager.getSpanCount();

        return 1;
    }
});

END

android-recyclerview-withheaderandfooter's People

Contributors

u3breeze avatar

Watchers

 avatar  avatar

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.