Tuesday, December 1, 2015

Sectioned RecyclerView in android


Sectioned RecyclerView in android


Created a sample Sectioned RecyclerView Application.

I used the following Library to easily create this one.

https://github.com/afollestad/sectioned-recyclerview
Credits:Aidan Follestad














Step: 1
======
Add a dependency to the build.gradle file [or] You can download the SectionedRecyclerViewAdapter.java file and put in it your source code.

compile('com.github.afollestad:sectioned-recyclerview:0.1.0') {
transitive = true
}

Step: 2
======
Created a Sample Activity with RecyclerView in the Xml Layout file. Populated with dummy data for sample.

package com.pratap.sectionrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.pratap.sectionrecyclerview.models.DataModel;
import com.pratap.sectionrecyclerview.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;


List<DataModel> allSampleData;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);

allSampleData = new ArrayList<DataModel>();
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setTitle("Section RecyclerView");

}


populateSampleData();


RecyclerView my_recycler_view = (RecyclerView) findViewById(R.id.my_recycler_view);
my_recycler_view.addItemDecoration(
new DividerItemDecoration(this, null));


RecyclerViewSectionAdapter adapter = new RecyclerViewSectionAdapter(allSampleData);


GridLayoutManager manager = new GridLayoutManager(this,
getResources().getInteger(R.integer.grid_span_1));


/* GridLayoutManager manager = new GridLayoutManager(this,
getResources().getInteger(R.integer.grid_span_2));


GridLayoutManager manager = new GridLayoutManager(this,
getResources().getInteger(R.integer.grid_span_3));*/

my_recycler_view.setLayoutManager(manager);
adapter.setLayoutManager(manager);
my_recycler_view.setAdapter(adapter);


}

private void populateSampleData() {

for (int i = 1; i <= 10; i++) {

DataModel dm = new DataModel();

dm.setHeaderTitle("Section " + i);

ArrayList<String> singleItem = new ArrayList<>();
for (int j = 1; j <= 4; j++) {

singleItem.add("Item " + j);
}

dm.setAllItemsInSection(singleItem);

allSampleData.add(dm);

}
}
}

Step: 3
======
Created a Adapter  which extends from SectionedRecyclerViewAdapter Class provided in the Library.

package com.pratap.sectionrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pratap.sectionrecyclerview.models.DataModel;
import com.pratap.sectionrecyclerview.utils.SectionedRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;


public class RecyclerViewSectionAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {


private List<DataModel> allData;


public RecyclerViewSectionAdapter(List<DataModel> data) {

this.allData = data;
}


@Override
public int getSectionCount() {
return allData.size();
}

@Override
public int getItemCount(int section) {

return allData.get(section).getAllItemsInSection().size();

}

@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {

String sectionName = allData.get(section).getHeaderTitle();
SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
sectionViewHolder.sectionTitle.setText(sectionName);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int section, int relativePosition, int absolutePosition) {

ArrayList<String> itemsInSection = allData.get(section).getAllItemsInSection();

String itemName = itemsInSection.get(relativePosition);

ItemViewHolder itemViewHolder = (ItemViewHolder) holder;

itemViewHolder.itemTitle.setText(itemName);

// Try to put a image . for sample i set background color in xml layout file
// itemViewHolder.itemImage.setBackgroundColor(Color.parseColor("#01579b"));
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
View v = null;
if (header)

{
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_section, parent, false);
return new SectionViewHolder(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ItemViewHolder(v);
}

}


// SectionViewHolder Class for Sections
public static class SectionViewHolder extends RecyclerView.ViewHolder {


final TextView sectionTitle;

public SectionViewHolder(View itemView) {
super(itemView);

sectionTitle = (TextView) itemView.findViewById(R.id.sectionTitle);


}
}

// ItemViewHolder Class for Items in each Section
public static class ItemViewHolder extends RecyclerView.ViewHolder {

final TextView itemTitle;

final ImageView itemImage;


public ItemViewHolder(View itemView) {
super(itemView);
itemTitle = (TextView) itemView.findViewById(R.id.itemTitle);
itemImage = (ImageView) itemView.findViewById(R.id.itemImage);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(v.getContext(), itemTitle.getText(), Toast.LENGTH_SHORT).show();

}
});
}
}
}

Step: 4
======
Create a Model class to provide for the Adapter


package com.pratap.sectionrecyclerview.models;

import java.util.ArrayList;

/**
* Created by pratap.kesaboyina on 30-11-2015.
*/
public class DataModel {



private String headerTitle;
private ArrayList<String> allItemsInSection;


public DataModel() {

}
public DataModel(String headerTitle, ArrayList<String> allItemsInSection) {
this.headerTitle = headerTitle;
this.allItemsInSection = allItemsInSection;
}



public String getHeaderTitle() {
return headerTitle;
}

public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
}

public ArrayList<String> getAllItemsInSection() {
return allItemsInSection;
}

public void setAllItemsInSection(ArrayList<String> allItemsInSection) {
this.allItemsInSection = allItemsInSection;
}


}

ScreenShots
=========
























Demo 
=====







Source Code
=========
DropBoxLink


No comments:

Post a Comment