I'm using SectionedRecyclerViewAdapter and i'm pass to myAdapter two sections
first section has 16 elements , and second section has 18 elements
each element has CheckBox Item and i'm try calling method that set all items as checked , but when looping on it and get item number 17 it returns null
but my mRecyclerView actually has 34 items as a total
so how to move to other section and get element number 17.
sectionAdapter = new SectionedRecyclerViewAdapter();
ArrayList<Item> contacts = new ArrayList<>();
// ArrayList<String> dishNames = new ArrayList<>();
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
for(int d = 0 ; d<DishesIngredientsShoppingList.size() ;d++) {
contacts = new ArrayList<>();
for (int i = 0; i < DishesIngredientsShoppingList.get(d).getGroups().length; i++) {
for (int g = 0; g < DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients().length; g++) {
contacts.add(new Item(DishesIngredientsShoppingList.get(d).getGroups()[i].getIngredients()[g].getName()));
}
}
sectionAdapter.addSection(new ContactsSection(DishesIngredientsShoppingList.get(d).getName(),contacts));
// contacts.clear();
}
class ContactsSection extends StatelessSection {
String title;
List<Item> list;
ItemViewHolder row;
HeaderViewHolder MainRow;
public ContactsSection(String title, ArrayList<Item> list) {
super(R.layout.shopping_row_item_header, R.layout.shopping_row_item);
this.title = title;
this.list = list;
}
#Override
public int getContentItemsTotal() {
return list.size(); // list here has 16 elements !! it should 34 elements
}
#Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new ItemViewHolder(view);
}
#Override
public void onBindItemViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final ItemViewHolder itemHolder = (ItemViewHolder) holder;
final Item name = list.get(position);
itemHolder.tvItem.setText(name.name);
((ItemViewHolder) holder).IntegBOX.setChecked(name.checked);
chooseAllradioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i <sectionAdapter.getItemCount(); i++) {
final Item name = list.get(i);
name.checked = true;
sectionAdapter.notifyDataSetChanged();
}
}
});
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.tvTitle.setText(title);
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
private final TextView tvTitle;
private final CheckBox DishBOX;
public HeaderViewHolder(View view) {
super(view);
tvTitle = (TextView) view.findViewById(R.id.DishName);
DishBOX = (CheckBox) view.findViewById(R.id.chooseDish);
}
}
class ItemViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{
private final View rootView;
private final TextView tvItem;
private final CheckBox IntegBOX;
public ItemViewHolder(View view) {
super(view);
rootView = view;
tvItem = (TextView) view.findViewById(R.id.DishName);
IntegBOX = (CheckBox) view.findViewById(R.id.chooseDish);
IntegBOX.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = getAdapterPosition();
list.get(position).checked = isChecked;
}
}
}
class Item {
String name;
boolean checked;
public Item(String name) {
this.name = name;
checked = false;
}
}
Add a method in your ContactsSection class to check all items:
public void checkAllItems() {
for (Item item : list) {
item.checked = true;
}
}
Then change your onClick code to:
chooseAllradioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(Section section : sectionAdapter.getSectionsMap().values()) {
section.checkAllItems();
}
sectionAdapter.notifyDataSetChanged();
}
});
Related
I have an abstract class "ListItem" with two subclasses - "Header"(Data) and "Event". Depending on the situation, the array should be displayed either as "Header" - "Events of this data", or just events.
The created object of the "Event" class is passed to the main fragment from a separate activity. If I only need to display events, I just write it to an array and update the adapter data, it works. If, after creating a class object, I need to display it and existing events in the form "Header" - "Events", then the adapter for some reason is not updated and the new event is not displayed. If you close the application and reopen it, the event appears in its normal form with a header.
Here's my RV Adapter
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final String TAG = "myLogs";
private ArrayList<ListItem> list;
final SparseBooleanArray selectedItems = new SparseBooleanArray();
private int currentSelectedPos;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM", myDateFormatSymbols);
public EventAdapter(ArrayList<ListItem> items){
list = items;
}
public ArrayList<ListItem> getItems(){
return list;
}
private EventAdapterListener itemClickListener;
public interface EventAdapterListener {
void onItemClick(int position);
void onItemLongClick(int position);
}
public void setListener(EventAdapterListener listener){
itemClickListener = listener;
}
private OnItemCheckedListener OnItemChecked;
public interface OnItemCheckedListener {
void onItemChecked(int position, boolean isImportant);
}
public void setOnItemCheckedListener (OnItemCheckedListener listener){
OnItemChecked = listener;
}
public static class EventViewHolder extends RecyclerView.ViewHolder{
public TextView TVtitle;
public TextView TVcomment;
public CheckBox CBimportance;
public TextView TVdate;
//public EventViewHolder(#NonNull View itemView, final EventAdapterListener listener) {
public EventViewHolder(#NonNull View itemView) {
super(itemView);
}
}
public void removeAllObjectsOf(Class<? extends ListItem> cls) {
Iterator itr = list.iterator();
while (itr.hasNext()) {
if (itr.next().getClass().equals(cls)) {
itr.remove();
}
}
notifyDataSetChanged();
}
Comparator<ListItem> dateComparator = new Comparator<ListItem>() {
#Override
public int compare(ListItem event, ListItem t1) {
return ((Event)(event)).Date.compareTo(((Event)t1).Date);
}
};
Comparator<ListItem> importanceComparator = new Comparator<ListItem>() {
#Override
public int compare(ListItem event, ListItem t1) {
return ((Event)(event)).IsImportant.compareTo(((Event)t1).IsImportant);
}
};
Comparator<ListItem> nameComparator = new Comparator<ListItem>() {
#Override
public int compare(ListItem event, ListItem t1) {
return ((Event)(event)).Name.compareTo(((Event)t1).Name);
}
};
Comparator<ListItem> completenessComparator = new Comparator<ListItem>() {
#Override
public int compare(ListItem event, ListItem t1) {
return ((Event)(event)).IsComplete.compareTo(((Event)t1).IsComplete);
}
};
public void sortBy(Comparator<ListItem> comparator, ArrayList<ListItem> list) {
removeAllObjectsOf(Header.class);
Collections.sort(list, comparator);
}
public void groupByDate()
{
ArrayList<ListItem> result = new ArrayList<>();
removeAllObjectsOf(Header.class);
sortBy(dateComparator, list);
ArrayList<Long> dates = new ArrayList<>();
for (ListItem event: list)
{
if (!(dates.contains(((Event)event).getDate())))
{
dates.add(((Event)event).getDate());
ArrayList<ListItem> ei = getItemsbyDate(list, ((Event)event).getDate());
Header header = new Header();
header.setHeader(String.valueOf(((Event)event).getDate()));
result.add(header);
result.addAll(ei);
}
}
list = result;
notifyDataSetChanged();
}
public ArrayList<ListItem> getItemsbyDate(ArrayList<ListItem> items, Long date){
ArrayList<ListItem> result = new ArrayList<>();
for (ListItem ci : items){
if (((Event)ci).getDate().equals(date)){
result.add(ci);
}
}
return result;
}
public void removeItem(int position) {
list.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,list.size());
}
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return list.get(position) instanceof Header;
}
private static DateFormatSymbols myDateFormatSymbols = new DateFormatSymbols(){
#Override
public String[] getMonths() {
return new String[]{"января", "февраля", "марта", "апреля", "мая", "июня",
"июля", "августа", "сентября", "октября", "ноября", "декабря"};
}
};
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == TYPE_HEADER){
View v = inflater.inflate(R.layout.group_layout, parent, false);
return new VHHeader(v);
} else {
View v = inflater.inflate(R.layout.list_item_cardview, parent, false);
return new VHItem(v);
}
}
#Override
public void onBindViewHolder(#NonNull EventViewHolder holder, final int position)
{
if (holder instanceof VHHeader){
Header currentItem = (Header) list.get(position);
VHHeader VHheader = (VHHeader) holder;
VHheader.txtTitle.setText(simpleDateFormat.format(Long.parseLong(currentItem.getHeader())));
} else if (holder instanceof VHItem) {
final Event event = (Event) list.get(position);
VHItem VHitem = (VHItem) holder;
VHitem.txtName.setText(event.getName());
VHitem.txtDate.setText(simpleDateFormat.format(event.getDate()));
VHitem.TVcomment.setText(event.getComment());
VHitem.CBimportance.setOnCheckedChangeListener(null);
VHitem.CBimportance.setChecked(event.getImportant());
VHitem.CBimportance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
event.IsImportant = b;
OnItemChecked.onItemChecked(position, b);
}
});
VHitem.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (selectedItems.size() > 0 && itemClickListener != null){
itemClickListener.onItemClick(position);
}
}
});
VHitem.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
if (itemClickListener != null){
itemClickListener.onItemLongClick(position);
}
return true;
}
});
if (currentSelectedPos == position) currentSelectedPos = -1;
}
}
#Override
public int getItemCount() {
return (list == null) ? 0 : list.size();
}
void deleteEvents(){
List<ListItem> selectedEvents = new ArrayList<>();
for (ListItem item : list){
if (item instanceof Event){
if (((Event) item).getSelected()){
selectedEvents.add(item);
}
}
}
list.removeAll(selectedEvents);
//notifyDataSetChanged();
currentSelectedPos = -1;
deleteEmptyHeaders();
}
public int getCountofHeaderItems(Header header)
{
int k = 0;
for (ListItem item: list)
{
if (item instanceof Event){
if (((Event) item).Date.equals(Long.parseLong(header.getHeader()))){
k++;
}
}
}
return k;
}
void deleteEmptyHeaders(){
List<Header> headers = new ArrayList<>();
for (ListItem event : list){
if (event instanceof Header){
if (getCountofHeaderItems((Header)event) == 0){
headers.add((Header)event);
}
}
}
list.removeAll(headers);
}
void toggleSelection(int position) {
if (getItemViewType(position) == TYPE_ITEM){
currentSelectedPos = position;
if (selectedItems.get(position)) {
selectedItems.delete(position);
((Event)list.get(position)).setSelected(false);
} else {
selectedItems.put(position, true);
((Event)list.get(position)).setSelected(true);
}
notifyItemChanged(position);
}
}
class VHHeader extends EventAdapter.EventViewHolder {
TextView txtTitle;
public VHHeader(View itemView) {
super(itemView);
this.txtTitle = (TextView) itemView.findViewById(R.id.tvTitleGroup);
}
}
class VHItem extends EventAdapter.EventViewHolder {
TextView txtName;
TextView txtDate;
TextView TVcomment;
CheckBox CBimportance;
public VHItem(View itemView) {
super(itemView);
this.txtName = (TextView) itemView.findViewById(R.id.tvTitle);
this.txtDate = (TextView) itemView.findViewById(R.id.tvDate);
this.TVcomment = (TextView) itemView.findViewById(R.id.tvComment);
this.CBimportance = (CheckBox) itemView.findViewById(R.id.cbIconImportant);
}
}
}
Here's my MainFragment:
private final static String ONLY_EVENTS = "ONLY_EVENTS";
private final static String GROUP_EVENTS_BY_DATE = "GROUP_EVENTS_BY_DATE";
ArrayList<ListItem> events;
private String ShowMode = ONLY_EVENTS;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first_new, container, false);
events = read(this.getActivity(), PROCESSED_EVENTS);
if (events == null)
events = new ArrayList<ListItem>();
rvEvent = view.findViewById(R.id.recyclerview);
rvEvent.setHasFixedSize(true);
lmEvent = new LinearLayoutManager(this.getContext());
adapterEvent = new EventAdapter(events);
rvEvent.setLayoutManager(lmEvent);
rvEvent.setAdapter(adapterEvent);
ShowEvents(ShowMode);
initSwipe();
return view;
}
public void ShowEvents(String mode){
switch (mode){
case ONLY_EVENTS:
adapterEvent.removeAllObjectsOf(Header.class);
break;
case GROUP_EVENTS_BY_DATE:
adapterEvent.groupByDate();
break;
}
}
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
Event nd = (Event) data.getSerializableExtra("class");
events.add(nd);
write(this.getActivity(), events, PROCESSED_EVENTS);
if (ShowMode.equals(GROUP_EVENTS_BY_DATE))
adapterEvent.groupByDate();
adapterEvent.notifyDataSetChanged();
}
}
public static void write(Context context, ArrayList<ListItem> events, String Key) {
Gson gson = new Gson();
String jsonString = gson.toJson(events);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = pref.edit();
editor.putString(Key, jsonString);
editor.apply();
}
public static ArrayList<ListItem> read(Context context, String Key) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
String jsonString = pref.getString(Key, "");
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Event>>() {
}.getType();
ArrayList<ListItem> list = gson.fromJson(jsonString, type);
return list;
}
This is how the Event is sent from the activity:
Intent intent = new Intent(NewEvent.this, FirstFragment.class);
//intent.putExtra("name", (String) etName.getText().toString());
objectEvent = new Event(etName.getText().toString(), date, etComment.getText().toString(), etType.getText().toString(), etNotif.getText().toString(), swIsComplete.isChecked(), false);
intent.putExtra("class", objectEvent);
setResult(RESULT_OK, intent);
finish();
UPD. In OnActivityResult after executing the line adapterEvent.groupByDate(); nothing changes. Changes take place in the array list in the adapter, but for some reason they do not go into the array that is used in the fragment. In the ShowEvents procedure, the same line of code works, and after it the array is changed.
I'm making NLevel expandable list using listview. I've added checkbox only last level data in list view. I have stuck in below scenario.
If I check checkbox then when I expand listview means checkbox gets automatically unchecked.I don't want it to be like that. If I checked checkbox it should stay checked until I uncheck manually.
Please anyone help me!! It's been two days I stuck here.
Here goes my code:
MainActivity.java
public class MainActivity extends Activity {
List<NLevelItem> list;
ListView listView;
Context context;
Button checkButton;
ArrayList<String>tempList;
CheckBox selected = null; //Make only one selection at a time
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
list = new ArrayList<NLevelItem>();
context = this;
checkButton = (Button)findViewById(R.id.buttons);
tempList = new ArrayList<String>();
//here we create 5 grandparent (top level) NLevelItems
//then foreach grandparent create a random number of parent (second level) NLevelItems
//then foreach parent create a random number of children (third level) NLevelItems
//we pass in an anonymous instance of NLevelView to the NLevelItem, this NLevelView is
//what supplies the NLevelAdapter with a View for this NLevelItem
Random rng = new Random();
final LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < 5; i++) {
final NLevelItem grandParent = new NLevelItem(new SomeObject("GrandParent "+i),null, new NLevelView() {
#Override
public View getView(NLevelItem item) {
View view = inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
//tv.setBackgroundColor(Color.GREEN);
String name = (String) ((SomeObject) item.getWrappedObject()).getName();
tv.setText(name);
return view;
}
});
list.add(grandParent);
int numChildren = rng.nextInt(4) + 1;
for (int j = 0; j < numChildren; j++) {
NLevelItem parent = new NLevelItem(new SomeObject("Parent "+j),grandParent, new NLevelView() {
#Override
public View getView(NLevelItem item) {
View view = inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
//tv.setBackgroundColor(Color.YELLOW);
String name = (String) ((SomeObject) item.getWrappedObject()).getName();
tv.setText(name);
return view;
}
});
list.add(parent);
int children = rng.nextInt(3)+1;
for(int x=0; x<children;x++){
final NLevelItem childs = new NLevelItem(new SomeObject("Parent1 "+x),parent, new NLevelView() {
#Override
public View getView(NLevelItem item) {
View view = inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
//tv.setBackgroundColor(Color.BLUE);
String name = (String) ((SomeObject) item.getWrappedObject()).getName();
tv.setText(name);
return view;
}
});
list.add(childs);
int grandChildren = rng.nextInt(5)+1;
for( int k = 0; k < grandChildren; k++) {
NLevelItem child = new NLevelItem(new SomeObject("child "+k),childs, new NLevelView() {
#Override
public View getView(NLevelItem item) {
View view = inflater.inflate(R.layout.check_list, null);
TextView tv = (TextView) view.findViewById(R.id.checktextView);
final String name = (String) ((SomeObject) item.getWrappedObject()).getName();
final CheckBox checkBox = (CheckBox)view.findViewById(R.id.check);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(selected != null){ //Edit
selected.setChecked(false);
}
selected = checkBox; //Edit
if(checkBox.isChecked()){
tempList.add((String) ((SomeObject)childs.getWrappedObject()).getName()+"+"+name);
}
else {
tempList.remove((String) ((SomeObject)childs.getWrappedObject()).getName()+"+"+name);
}
}
});
//tv.setBackgroundColor(Color.GRAY);
tv.setText(name);
return view;
}
});
list.add(child);
}
}
}
}
NLevelAdapter adapter = new NLevelAdapter(list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
((NLevelAdapter)listView.getAdapter()).toggle(arg2);
((NLevelAdapter)listView.getAdapter()).getFilter().filter();
}
});
checkButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i=0;i<tempList.size();i++){
Toast.makeText(context,tempList.get(i),Toast.LENGTH_LONG).show();
}
}
});
}
class SomeObject {
public String name;
public SomeObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}
NLevelAdapter.java
public class NLevelAdapter extends BaseAdapter {
List<NLevelItem> list;
List<NLevelListItem> filtered;
public void setFiltered(ArrayList<NLevelListItem> filtered) {
this.filtered = filtered;
}
public NLevelAdapter(List<NLevelItem> list) {
this.list = list;
this.filtered = filterItems();
}
#Override
public int getCount() {
return filtered.size();
}
#Override
public NLevelListItem getItem(int arg0) {
return filtered.get(arg0);
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
return getItem(arg0).getView();
}
public NLevelFilter getFilter() {
return new NLevelFilter();
}
class NLevelFilter {
public void filter() {
new AsyncFilter().execute();
}
class AsyncFilter extends AsyncTask<Void, Void, ArrayList<NLevelListItem> > {
#Override
protected ArrayList<NLevelListItem> doInBackground(Void...arg0) {
return (ArrayList<NLevelListItem>)filterItems();
}
#Override
protected void onPostExecute(ArrayList<NLevelListItem> result) {
setFiltered(result);
NLevelAdapter.this.notifyDataSetChanged();
}
}
}
public List<NLevelListItem> filterItems() {
List<NLevelListItem> tempfiltered = new ArrayList<NLevelListItem>();
OUTER: for (NLevelListItem item : list) {
//add expanded items and top level items
//if parent is null then its a top level item
if(item.getParent() == null) {
tempfiltered.add(item);
} else {
//go through each ancestor to make sure they are all expanded
NLevelListItem parent = item;
while ((parent = parent.getParent())!= null) {
if (!parent.isExpanded()) {
//one parent was not expanded
//skip the rest and continue the OUTER for loop
continue OUTER;
}
}
tempfiltered.add(item);
}
}
return tempfiltered;
}
public void toggle(int arg2) {
filtered.get(arg2).toggle();
}
}
Thanks in advance!!
i think you need to store the checkbox state in a boolean (is checked), and reflect that on the view, when getView() is called.
1- Add boolean checked to NLevelItem :
private boolean checked = false;
//add setter: setChecked(boolean)
//add getter isChecked()
2- Use that boolean in getView() (last one where checkbox is added)
#Override
public View getView(final NLevelItem item) {
// .......
final CheckBox checkBox = (CheckBox)view.findViewById(R.id.check);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//store checkbox state, note that NLevelItem item might need to be defined with 'final'
item.setChecked(checkBox.isChecked());
if(checkBox.isChecked()){
tempList.add((String) ((SomeObject)childs.getWrappedObject()).getName()+"+"+name);
}
else {
tempList.remove((String) ((SomeObject)childs.getWrappedObject()).getName()+"+"+name);
}
}//onClick()
}//setOnClickListener()
//update checkbox state from the corresponding NLevelItem
checkBox.setChecked(item.isChecked());
//.......
}//getView()
-EDIT:
to select 1 item, you need to iterate all items, set checked = false, but 1
i am not sure if you have to do it on:
List<NLevelItem> list;
or
List<NLevelListItem> filtered;
in the adapter class
private void selectOnly(int position){
for(int a=0;a<list.size();a++){
if(a == position){
list.get(a).setChecked(true);
continue;
}
list.get(a).setChecked(false);
}//for loop
notifyDataSetChanged(); // to update views (checkbox state)
}
Usage: selectOnly(15);
Use ViewHolder class to set and get Tag like this:
public class ListAdapter extends BaseAdapter {
private Context con;
private List<String> dataLt;
private static LayoutInflater inflater = null;
public ListAdapter(Context context, List<String> dataList){
con = context;
dataLt = dataList;
inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return dataLt.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.list_item_search, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.textView = (TextView) vi.findViewById(R.id.textView);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
return vi;
}
public static class ViewHolder{
TextView textView;
}
}
Hope this may help.
I Just need to fix the value inside Spinner when i select some value and SetEnabled True/False when i checked the CheckBox.
For example when i check the chekcbox, the spinner will be enabled and do the opposite when i uncheck the checkbox.
However it will changing randomly when i just scrolling.
This my code :
CustomListView.java
class CustomListView extends BaseAdapter {
private ArrayList<ListItem2> listData;
private LayoutInflater layoutInflater;
private Context context;
public CustomListView adapter = this;
String jmlCmbSiap;
ArrayList<String> a = new ArrayList<>();
public CustomListView(Context context, ArrayList<ListItem2> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
newsItem = listData.get(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_detail_order, null);
holder = new ViewHolder();
newsItem.setAdapter(Integer.parseInt(newsItem.getJumlah()));
holder.txtBarang = (TextView) convertView.findViewById(R.id.txtBarang);
holder.txtJumlah = (TextView) convertView.findViewById(R.id.txtJumlah);
holder.txtBox = (TextView) convertView.findViewById(R.id.txtBoxes);
holder.cmbSiap = (Spinner) convertView.findViewById(R.id.cmbSiap);
holder.cmbSiap.setAdapter(newsItem.getAdapter());
holder.thumbImage = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.chkOk = (CheckBox) convertView.findViewById(R.id.chkOk);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Picasso
.with(context)
.load(newsItem.getImg())
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(holder.thumbImage);
holder.txtBarang.setText(newsItem.getNamaProduk().toUpperCase());
holder.txtJumlah.setText(newsItem.getJumlah().toUpperCase());
holder.cmbSiap.setSelection(Integer.parseInt(newsItem.getJumlah()));
holder.cmbSiap.setEnabled(newsItem.getcmbSiap());
holder.txtBox.setEnabled(newsItem.gettxtBox());
holder.cmbSiap.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
jmlCmbSiap = parent.getItemAtPosition(position).toString();
newsItem.setJumlah(jmlCmbSiap);
holder.cmbSiap.setSelection(Integer.parseInt(newsItem.getJumlah()));
System.out.println("JML : " + newsItem.getJumlah());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
holder.chkOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (newsItem.getchkOk() == false) {
newsItem.setcmbSiap(true);
newsItem.setchkOk(true);
newsItem.settxtBox(true);
holder.cmbSiap.setEnabled(true);
holder.chkOk.setChecked(true);
txtBox.setEnabled(true);
numberChecked = numberChecked + 1;
idChkOk.add(position);
System.out.println("Chk : " + idChkOk);
} else {
numberChecked = numberChecked - 1;
newsItem.setcmbSiap(false);
newsItem.setchkOk(false);
holder.cmbSiap.setEnabled(false);
holder.chkOk.setChecked(false);
if (numberChecked == 0) {
newsItem.settxtBox(false);
txtBox.setEnabled(false);
}
idChkOk.remove(position);
System.out.println("Chk : " + idChkOk);
}
}
});
cmdPak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a.clear();
for (int i = 0; i < listData.size(); i++)
{
newsItem = listData.get(i);
a.add(newsItem.getJumlah().toString());
}
System.out.println("Pak : " + a);
}
});
return convertView;
}
class ViewHolder {
TextView txtBarang,txtJumlah,txtBox;
Spinner cmbSiap;
ImageView thumbImage;
CheckBox chkOk;
}
}
ListItem2.java
class ListItem2 {
private String shipment;
private String truck;
private String transporter;
private String tanggal;
private String order;
private String nilai;
private String alamat;
private String status;
private String jumlah;
private String namaProduk;
private String img;
private String sopir;
private String jumlahTerima;
private String alasan;
private String orderTemp;
private Boolean chkOk;
private Boolean cmbSiap;
private Boolean txtBox;
private ArrayAdapter<CharSequence> adapter;
private ArrayList<String> array = new ArrayList<String>();
public void setAdapter(Integer jml)
{
for (int i = 0; i<= jml; i++) {
array.add(String.valueOf(i));
}
adapter = new ArrayAdapter(DetailOrder.this,android.R.layout.simple_spinner_item,array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter()
{
return adapter;
}
public void setTanggal(String tanggal) {
this.tanggal = tanggal;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getAlamat() {
return alamat;
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
public String getNilai() {
return nilai;
}
public void setNilai(String nilai) {
this.nilai = nilai;
}
public String getKodeProduk() {
return nilai;
}
public void setKodeProduk(String nilai) {
this.nilai = nilai;
}
public String getNamaProduk() {
return namaProduk;
}
public void setNamaProduk(String namaProduk) {
this.namaProduk = namaProduk;
}
public String getJumlah() {
return jumlah;
}
public void setJumlah(String jumlah) {
this.jumlah = jumlah;
}
public Boolean getchkOk() {
return chkOk;
}
public void setchkOk(Boolean chkOk) {
this.chkOk = chkOk;
}
public Boolean getcmbSiap() {
return cmbSiap;
}
public void setcmbSiap(Boolean cmbSiap) {
this.cmbSiap = cmbSiap;
}
public Boolean gettxtBox() {
return txtBox;
}
public void settxtBox(Boolean txtBox) {
this.txtBox = txtBox;
}
}
Could you lend me a hand ?
Update :
chkAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View views;
CheckBox chkOkx;
Spinner cmbSiap;
for (int i = 0; i < jArray; i++) {
final ListItem2 newsItem = listData.get(i);
views = listView.getChildAt(i);
if (views == null) {
return;
} else {
chkOkx = (CheckBox) views.findViewById(R.id.chkOk);
cmbSiap = (Spinner) views.findViewById(R.id.cmbSiap);
if (chkOkx.isChecked()) {
newsItem.setchkOk(true);
newsItem.setcmbSiap(true);
chkOkx.setChecked(newsItem.getchkOk());
cmbSiap.setEnabled(newsItem.getcmbSiap());
} else {
newsItem.setchkOk(false);
newsItem.setcmbSiap(false);
chkOkx.setChecked(newsItem.getchkOk());
cmbSiap.setEnabled(newsItem.getcmbSiap());
}
idChkOk.add(i);
}
}
if (chkAll.isChecked()) {
txtBox.setEnabled(true);
numberChecked = 0;
} else {
txtBox.setEnabled(false);
numberChecked = listviewChild;
}
listView.invalidateViews();
}
});
In ListView when you scroll, the views are destroyed and re-created. To overcome the issue you need to preserve both of the checkbox status and spinner item's value.
For the checkbox, save the checked status from setOnClickListener inside a boolean array,
if (((CheckBox) v).isChecked()) {
checkBoxState[position] = true;
} else {
checkBoxState[position] = false;
}
The boolean array should like this,
private boolean[] checkBoxState;
Later on getView() apply the check/uncheck status of each checkbox items based on the array
holder.checkBox.setChecked(checkBoxState[position]);
Similarly to save the spinner data, create a Hashmap
Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
and save the spinner data once it's selected from the setOnItemSelectedListener
myMap.put(position, pos);
Later restore the spinner selection position from the saved data,
if (myMap.containsKey(position)) {
viewHolder.spinner.setSelection(myMap.get(position));
}
Full code,
class CustomListView extends BaseAdapter {
private ArrayList<ListItem2> listData;
private LayoutInflater layoutInflater;
private Context context;
public CustomListView adapter = this;
String jmlCmbSiap;
ArrayList<String> a = new ArrayList<>();
private boolean[] checkBoxState;
private Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
public CustomListView(Context context, ArrayList<ListItem2> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
checkBoxState = new boolean[listData.size()];
this.context = context;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
newsItem = listData.get(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_detail_order, null);
holder = new ViewHolder();
newsItem.setAdapter(Integer.parseInt(newsItem.getJumlah()));
holder.txtBarang = (TextView) convertView.findViewById(R.id.txtBarang);
holder.txtJumlah = (TextView) convertView.findViewById(R.id.txtJumlah);
holder.txtBox = (TextView) convertView.findViewById(R.id.txtBoxes);
holder.cmbSiap = (Spinner) convertView.findViewById(R.id.cmbSiap);
holder.cmbSiap.setAdapter(newsItem.getAdapter());
holder.thumbImage = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.chkOk = (CheckBox) convertView.findViewById(R.id.chkOk);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setChecked(checkBoxState[position]);
if (checkBoxState[position])
{
holder.chkOk.setChecked(true);
newsItem.setcmbSiap(true);
newsItem.settxtBox(true);
holder.cmbSiap.setEnabled(true);
holder.cmbSiap.setClickable(true);
txtBox.setEnabled(true);
}
else
{
holder.chkOk.setChecked(false);
newsItem.setcmbSiap(false);
newsItem.settxtBox(false);
holder.cmbSiap.setEnabled(false);
holder.cmbSiap.setClickable(false);
txtBox.setEnabled(false);
}
Picasso
.with(context)
.load(newsItem.getImg())
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(holder.thumbImage);
holder.txtBarang.setText(newsItem.getNamaProduk().toUpperCase());
holder.txtJumlah.setText(newsItem.getJumlah().toUpperCase());
holder.cmbSiap.setSelection(Integer.parseInt(newsItem.getJumlah()));
holder.cmbSiap.setEnabled(newsItem.getcmbSiap());
holder.txtBox.setEnabled(newsItem.gettxtBox());
if (myMap.containsKey(position)) {
viewHolder.spinner.setSelection(myMap.get(position));
}
holder.cmbSiap.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
jmlCmbSiap = parent.getItemAtPosition(position).toString();
newsItem.setJumlah(jmlCmbSiap);
myMap.put(position, pos);
System.out.println("JML : " + newsItem.getJumlah());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
holder.chkOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//is chkOk checked?
if (((CheckBox) v).isChecked()) {
checkBoxState[position] = true;
viewHolder.spinner.setEnabled(true);
viewHolder.spinner.setClickable(true);
} else {
checkBoxState[position] = false;
viewHolder.spinner.setEnabled(false);
viewHolder.spinner.setClickable(false);
}
}
});
cmdPak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a.clear();
for (int i = 0; i < listData.size(); i++)
{
newsItem = listData.get(i);
a.add(newsItem.getJumlah().toString());
}
System.out.println("Pak : " + a);
}
});
return convertView;
}
class ViewHolder {
TextView txtBarang,txtJumlah,txtBox;
Spinner cmbSiap;
ImageView thumbImage;
CheckBox chkOk;
}
}
Update:
In order to make a select all/deselect all checkbox feature,
create a boolean flag at the top of adapter's class,
boolean isCheckAll = false;
then create a toggle method,
public void toggleCheckAll() {
isCheckAll = !isCheckAll;
for (int i = 0; i < listData.size(); i++) {
if (isCheckAll) {
checkBoxState[i] = true;
notifyDataSetInvalidated();
} else {
checkBoxState[i] = false;
notifyDataSetInvalidated();
}
}
}
Now call the method from your aactivity's check all button's click,
btnCheckAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
adapter.toggleCheckAll();
}
});
I have a RecyclerView with 5 items and drag & drop feature. I want to save items's positions into SharedPreferences in order to restore them when the App is launched. Currently I'm able to save their positions into SharedPreferences using Json but the App crashes when I restore positions. Any solution? Thanks.
public static AppCompatActivity mActivity;
public static SharedPreferences SP;
public static SharedPreferences.Editor mEditor;
public static RecyclerView mRecyclerView;
public static RecyclerViewDragDropManager mRecyclerViewDragDropManager;
public static List<Long> mSaveItemsOrder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mEditor = SP.edit();
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerViewDragDropManager = new RecyclerViewDragDropManager();
mRecyclerViewDragDropManager.setInitiateOnMove(false);
mRecyclerViewDragDropManager.setInitiateOnLongPress(true);
mRecyclerViewDragDropManager.setLongPressTimeout(300);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mRecyclerViewDragDropManager.createWrappedAdapter(new Adapter()));
mRecyclerViewDragDropManager.attachRecyclerView(mRecyclerView);
}
public static class Item {
public final long id;
public final String text;
public Item(long id, String text) {
this.id = id;
this.text = text;
}
}
public static class ViewHolder extends AbstractDraggableItemViewHolder {
Button mButton;
public ViewHolder(View itemView) {
super(itemView);
mButton = (Button) itemView.findViewById(R.id.button);
}
}
public static class Adapter extends RecyclerView.Adapter<ViewHolder> implements DraggableItemAdapter<ViewHolder> {
List<Item> mItems;
public Adapter() {
setHasStableIds(true);
mItems = new ArrayList<>();
if (SP.getString("items_order", null) == null) {
for (int mInt = 0; mInt < 5; mInt++) {
mItems.add(new Item(mInt, "Item " + mInt));
}
} else {
mItems = new Gson().fromJson(SP.getString("items_order", null), new TypeToken<Item>(){}.getType());
}
}
#Override
public long getItemId(int position) {
return mItems.get(position).id;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view, parent, false);
return new ViewHolder(mView);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Item mItem = mItems.get(position);
holder.mButton.setText(mItem.text);
}
#Override
public int getItemCount() {
return mItems.size();
}
#Override
public void onMoveItem(int fromPosition, int toPosition) {
Item mMovedItem = mItems.remove(fromPosition);
mItems.add(toPosition, mMovedItem);
notifyItemMoved(fromPosition, toPosition);
}
#Override
public boolean onCheckCanStartDrag(ViewHolder holder, int position, int x, int y) {
return true;
}
#Override
public ItemDraggableRange onGetItemDraggableRange(ViewHolder holder, int position) {
return null;
}
#Override
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}
}
#Override
protected void onPause() {
super.onPause();
mSaveItemsOrder = new ArrayList<>();
for (int mInt = 0; mInt < mRecyclerView.getAdapter().getItemCount(); mInt++) {
mSaveItemsOrder.add(mRecyclerView.getAdapter().getItemId(mInt));
}
mEditor.putString("items_order", new Gson().toJson(mSaveItemsOrder)).commit();
}
This is the error:
https://www.dropbox.com/s/j2tf4n08depyynt/Error.txt?dl=0
The problem is with your JSON.
You the object you are serializing to JSON is mSaveItemsOrder which is an ArrayList. But on deserializing you give GSON the Class new TypeToken<Item>(){}.getType().
So you store an ArrayList in your JSON but trying to deserialzie a different Class, that won't work.
This should work:
mItems = new Gson().fromJson(SP.getString("items_order", null), new TypeToken<ArrayList>(){}.getType(););
I'm Trying to use : SimpleSectionedRecyclerViewAdapter , i have arrayList that have 30 elements with strings , i'm going to group every 10 elements with header Title ,
first item should be "Item0" with header name "First 10 elements start"
but when doing that i get :
"First 10 elements start" // header titile
"Item1" <---! note : it should "Item0"
so where is index number 0 gone?
//adapter.addItem3(CategoriesList.get(0));
List<SimpleSectionedRecyclerViewAdapter.Section> sections =
new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>();
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "First 10 elements start"));
//Add your adapter to the sectionAdapter
SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()];
SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new
SimpleSectionedRecyclerViewAdapter(activity, R.layout.drawer_header_book, R.id.headerName, adapter);
mSectionedAdapter.setSections(sections.toArray(dummy));
mDrawerList.setLayoutManager(new LinearLayoutManager(activity));
mDrawerList.setAdapter(mSectionedAdapter);
I'm using exactly as adapter :
https://gist.github.com/gabrielemariotti/4c189fb1124df4556058
public class DrawerItemCustomAdapterForAllBooks extends RecyclerView.Adapter<DrawerItemCustomAdapterForAllBooks.SimpleViewHolder> {
private final Context context;
Typeface custom_font;
ArrayList<Categories> mData;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
int BookID;
public void add(Categories s,int position) {
position = position == -1 ? getItemCount() : position;
mData.add(position,s);
notifyItemInserted(position);
}
public void remove(int position){
if (position < getItemCount() ) {
mData.remove(position);
notifyItemRemoved(position);
}
}
public DrawerItemCustomAdapterForAllBooks(Context context, ArrayList<Categories> Categories2, int BookID) {
this.mData = Categories2;
this.context = context;
this.BookID = BookID;
custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/HelveticaNeueLTArabic-Light.ttf");
}
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(context).inflate(R.layout.listview_item_row, parent, false);
return new SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
if (holder != null) {
final Categories currentItem = getItem(holder.getAdapterPosition());
SimpleViewHolder genericViewHolder = (SimpleViewHolder) holder;
genericViewHolder.position = holder.getAdapterPosition();
genericViewHolder.CategoryName.setText(currentItem.getName());
genericViewHolder.CategoryName.setTypeface(custom_font);
genericViewHolder.CategoryName.setTag(holder.getAdapterPosition());
genericViewHolder.itemView.setTag(holder.getAdapterPosition());
if (currentItem.getBookID() == 1) {
genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_white));
genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_darkrose));
Picasso.with(context).load(R.drawable.list_icon_e02)
.error(R.drawable.no_internet)
.tag(context)
.placeholder(R.drawable.no_spic)
.into(genericViewHolder.CategoryImage);
} else if (currentItem.getBookID() == 2) {
genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_openrose));
genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_darkrose));
Picasso.with(context).load(R.drawable.list_icon_08)
.error(R.drawable.no_internet)
.tag(context)
.placeholder(R.drawable.no_spic)
.into(genericViewHolder.CategoryImage);
} else if (currentItem.getBookID() == 3) {
genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_darkrose));
genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_white));
Picasso.with(context).load(R.drawable.list_icon_08)
.error(R.drawable.no_internet)
.tag(context)
.placeholder(R.drawable.no_spic)
.into(genericViewHolder.CategoryImage);
}
}
}
#Override
public int getItemCount() {
return mData.size()-2;
}
public Categories getItem(int position) {
return mData.get(position-1);
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
protected TextView CategoryName;
protected ImageView CategoryImage;
protected View itemView;
int position = -1;
public SimpleViewHolder(View itemView) {
super(itemView);
this.CategoryName = (TextView) itemView.findViewById(R.id.CategoryName);
this.CategoryImage = (ImageView) itemView.findViewById(R.id.CategoryImage);
// CategoryName.setTypeface(custom_font);
this.itemView = itemView;
getAdapterPosition();
this.CategoryName = (TextView) itemView.findViewById(R.id.CategoryName);
this.CategoryImage = (ImageView) itemView.findViewById(R.id.CategoryImage); }
}
}
With the library SectionedRecyclerViewAdapter you can group your items in sections and add a header to each section without worrying about the "position" of the headers:
class MySection extends StatelessSection {
String title;
List<Categories> list;
public MySection(String title, List<Categories> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);
this.title = title;
this.list = list;
}
#Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}
#Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}
#Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
// bind your view here
itemHolder.tvItem.setText(list.get(position).getName());
}
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(title);
}
}
Then you set up the RecyclerView with your Sections:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with a sub list of data from mData
MySection data1Section = new MySection("First 10 elements start", categories1List);
MySection data2Section = new MySection("Elements from 10 to 20 start", categories2List);
MySection data3Section = new MySection("Elements from 20 to 30 start", categories3List);
// Add your Sections to the adapter
sectionAdapter.addSection(data1Section);
sectionAdapter.addSection(data2Section);
sectionAdapter.addSection(data3Section);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);