I am unable to update a cardView in my app. I have a main activity which displays my card views which contain a textview, a seekbar, an imageView and a switch. I have an edit activity to edit cards and an add activity to add new cards. I have added an onClickListener on the Itemview in my ViewHolder class and inside it a startactivityforresult with intent for Transitioneditactivity. The transitioneditactivity is where the user can edit the values and press save to reflect the changes. However the changes are not reflected in the app. I was following the code from a book introduction to android application development and they have done the same thing yet theirs works and mine doesn't.
Heres the code
MainActivity
public class MainActivity extends AppCompatActivity {
public static final String TRANSITION_FAB = "fab_transition";
public static final String EXTRA_NAME = "name";
public static final String EXTRA_TYPE = "type";
public static final String EXTRA_SEEK = "seek";
public static final String EXTRA_UPDATE = "update";
public static final String EXTRA_DELETE = "delete";
private RecyclerView recyclerView;
private CardAdapter adapter;
private ArrayList<Card> cardsList=new ArrayList<>();
private String[] names;
private int[] type;
private String[] type_name;
private boolean[] switchstates;
private boolean[] seekstatus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
names=getResources().getStringArray(R.array.device);
type_name = getResources().getStringArray(R.array.appliances);
initcards();
if(adapter==null){
adapter=new CardAdapter(this,cardsList);
}
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FloatingActionButton fab=findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Pair<View,String> pair=Pair.create(view.findViewById(R.id.fab),TRANSITION_FAB);
ActivityOptions options;
Activity act= MainActivity.this;
options= ActivityOptions.makeSceneTransitionAnimation(act,pair);
Intent transitionIntent= new Intent(act,TransitionAddActivity.class);
act.startActivityForResult(transitionIntent,adapter.getItemCount(),options.toBundle());
}
});
}
public void doSmoothScroll(int position){
recyclerView.smoothScrollToPosition(position);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==adapter.getItemCount()){
if(resultCode==RESULT_OK){
String name =data.getStringExtra(EXTRA_NAME);
int type=data.getIntExtra(EXTRA_TYPE,0);
boolean seek = data.getBooleanExtra(EXTRA_SEEK,false);
adapter.addCard(name,type,false,seek);
}else {
if (resultCode==RESULT_OK){
RecyclerView.ViewHolder viewHolder=recyclerView.findViewHolderForAdapterPosition(requestCode);
if(data.getExtras().getBoolean(EXTRA_UPDATE)){
String name = data.getStringExtra(EXTRA_NAME);
int type = data.getIntExtra(EXTRA_TYPE,0);
boolean seek = data.getBooleanExtra(EXTRA_SEEK,false);
viewHolder.itemView.setVisibility(View.INVISIBLE);
adapter.updateCard(name,type,seek,requestCode);
}
}
}
}
}
private void initcards() {
for(int i=0;i<2;i++){
Card card=new Card();
card.setId((long)i);
card.setName(names[i]);
card.setStatus(false);
card.setType(i);
cardsList.add(card);
}
}
}
Heres the Card Class
public class Card {
private long id;
private String name;
private int type;
private boolean status;
private boolean seekstatus;
public long getId() {
return id;
}
public boolean isSeekstatus() {
return seekstatus;
}
public void setSeekstatus(boolean seekstatus) {
this.seekstatus = seekstatus;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
The CardAdapter Class
public class CardAdapter extends
RecyclerView.Adapter<CardAdapter.ViewHolder>{
private static final String DEBUG_TAG = "CardAdapter";
public Context context;
public ArrayList<Card> cardsList;
public CardAdapter(Context context, ArrayList<Card> cardsList){
this.context=context;
this.cardsList=cardsList;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
String name1 = cardsList.get(position).getName();
boolean switchstatus=cardsList.get(position).isStatus();
boolean seekstatus=cardsList.get(position).isSeekstatus();
int type=cardsList.get(position).getType();
TextView nametv= viewHolder.name;
nametv.setText(name1);
Switch device_switch = viewHolder.device_switch;
device_switch.setChecked(switchstatus);
SeekBar seek = viewHolder.seekbar;
if(seekstatus){
seek.setVisibility(View.VISIBLE);
}else {
seek.setVisibility(View.INVISIBLE);
}
}
#Override
public void onViewDetachedFromWindow(ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.itemView.clearAnimation();
}
#Override
public void onViewAttachedToWindow(ViewHolder holder) {
super.onViewAttachedToWindow(holder);
animateCircularReveal(holder.itemView);
}
public void animateCircularReveal(View view){
int centerX=0;
int centerY=0;
int startRadius=0;
int endRadius=Math.max(view.getWidth(),view.getHeight());
Animator animation=ViewAnimationUtils.createCircularReveal(view,centerX,centerY,startRadius,endRadius);
view.setVisibility(View.VISIBLE);
animation.start();
}
public void animateCircularDelete(final View view,final int list_position){
int centerX=view.getWidth();
int centerY=view.getHeight();
int startRadius=view.getWidth();
int endRadius=0;
Animator animation = ViewAnimationUtils.createCircularReveal(view,centerX,centerY,startRadius,endRadius);
animation.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
cardsList.remove(list_position);
notifyItemRemoved(list_position);
}
});
}
public void addCard(String name,int type,boolean switchstatus,boolean seekstatus){
Card card=new Card();
card.setName(name);
card.setType(type);
card.setStatus(switchstatus);
card.setSeekstatus(seekstatus);
((MainActivity)context).doSmoothScroll(getItemCount());
cardsList.add(card);
notifyItemInserted(getItemCount());
}
public void updateCard(String name,int type,boolean seekstatus,int list_position){
cardsList.get(list_position).setName(name);
cardsList.get(list_position).setType(type);
cardsList.get(list_position).setSeekstatus(seekstatus);
notifyItemChanged(list_position);
}
public void deleteCard(View view,int list_position){
animateCircularDelete(view,list_position);
}
#Override
public int getItemCount() {
if(cardsList.isEmpty()){
return 0;
} else {
return cardsList.size();
}
}
#Override
public long getItemId(int position) {
return cardsList.get(position).getId();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater li=LayoutInflater.from(viewGroup.getContext());
View v=li.inflate(R.layout.card_view_holder,viewGroup,false);
return new ViewHolder(v);
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView name;
private Switch device_switch;
private ImageView device_image;
private SeekBar seekbar;
public ViewHolder(View v) {
super(v);
name=v.findViewById(R.id.name);
device_image=v.findViewById(R.id.device_image);
device_switch=v.findViewById(R.id.device_switch);
seekbar=v.findViewById(R.id.seekbar);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int requestCode= getAdapterPosition();
String name = cardsList.get(requestCode).getName();
int type = cardsList.get(requestCode).getType();
boolean seekstatus=cardsList.get(requestCode).isSeekstatus();
Intent transitionIntent=new Intent(context,TransitionEditActivity.class);
transitionIntent.putExtra(MainActivity.EXTRA_NAME,name);
transitionIntent.putExtra(MainActivity.EXTRA_TYPE,type);
transitionIntent.putExtra(MainActivity.EXTRA_SEEK,seekstatus);
transitionIntent.putExtra(MainActivity.EXTRA_UPDATE,false);
transitionIntent.putExtra(MainActivity.EXTRA_DELETE,false);
((AppCompatActivity)context).startActivityForResult(transitionIntent,requestCode);
}
});
device_switch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText((context),""+name.getText()+" is turned "+(device_switch.isChecked()?"On":"Off"),Toast.LENGTH_SHORT).show();
}
});
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Toast.makeText((context),""+name.getText()+"is set at "+i+"%",Toast.LENGTH_SHORT).show();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar){
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
}
The EditActivity
public class TransitionEditActivity extends AppCompatActivity {
private EditText device_name;
private Spinner device_type;
private CheckBox seek_status;
ArrayAdapter<CharSequence> adapter;
private int type;
Button savebutton;
private Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_edit);
device_name =findViewById(R.id.name);
device_type=findViewById(R.id.type);
seek_status=findViewById(R.id.cbintensity);
savebutton=findViewById(R.id.add);
adapter= ArrayAdapter.createFromResource(this,R.array.appliances,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
device_type.setAdapter(adapter);
device_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
type = i;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
intent=getIntent();
String name=intent.getStringExtra(MainActivity.EXTRA_NAME);
type=intent.getIntExtra(MainActivity.EXTRA_TYPE,0);
boolean seek=intent.getBooleanExtra(MainActivity.EXTRA_SEEK,false);
device_name.setText(name);
device_name.setSelection(device_name.getText().length());
seek_status.setChecked(seek);
savebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(device_name.getText().toString().trim())){
Toast.makeText(getApplicationContext(),"Enter a valid name",Toast.LENGTH_SHORT).show();
}else {
intent.putExtra(MainActivity.EXTRA_NAME,String.valueOf(device_name.getText()));
intent.putExtra(MainActivity.EXTRA_TYPE,type);
intent.putExtra(MainActivity.EXTRA_SEEK,seek_status.isChecked());
intent.putExtra(MainActivity.EXTRA_UPDATE,true);
setResult(RESULT_OK,intent);
supportFinishAfterTransition();
}
}
});
}
}
My activity main layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kejriwal.shivam.automationcard.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:layout_marginEnd="25dp"
android:elevation="6dp"
android:contentDescription="Add appliance"
android:id="#+id/fab"
android:clickable="true"
android:src="#android:drawable/ic_input_add"
android:tint="#android:color/white"
android:layout_height="wrap_content" />
</RelativeLayout>
the card layout file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/card_layout"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:clickable="true"
android:layout_marginTop="10dp"
android:focusable="true"
android:elevation="6dp"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
card_view:cardBackgroundColor="#color/card"
card_view:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/linear"
android:orientation="vertical"
android:padding="3dp"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/device_image"
android:src="#android:drawable/ic_input_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/name"
android:text="Fan"
android:layout_marginStart="20dp"
android:layout_toRightOf="#+id/device_image"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/device_image"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="#+id/device_switch"
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content" />
</RelativeLayout>
<SeekBar
android:id="#+id/seekbar"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="90dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
The Edit Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="50dp"
android:id="#+id/device_image"
android:layout_gravity="center"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
/>
<EditText
android:layout_width="200dp"
android:id="#+id/name"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
/>
<Spinner
android:layout_width="wrap_content"
android:prompt="#string/spinner_prompt"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:id="#+id/type"
android:layout_marginBottom="20dp"
android:entries="#array/appliances"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Control Intensity"
android:id="#+id/cbintensity"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="Save"/>
</RelativeLayout>
</LinearLayout>
you are binding your data inside viewholder in adapter...
your adapter should look like this...
public class BorderAdapter extends RecyclerView.Adapter<BorderAdapter.ViewHolder> {
private final Context context;
private final BorderSelectedListener borderSelectedListener;
public BorderAdapter(Context context, BorderSelectedListener borderSelectedListener) {
this.context = context;
this.borderSelectedListener = borderSelectedListener;
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(this.context).inflate(R.layout.frames_adapter_item, parent, false));
}
public void onBindViewHolder(final ViewHolder holder, #SuppressLint("RecyclerView") int position) {
Animation animation = AnimationUtils.loadAnimation(context,
(position > lastPosition) ? R.anim.left_to_right
: R.anim.right_to_left);
holder.itemView.startAnimation(animation);
holder.imageView.setBackground(context.getResources().getDrawable(R.drawable.stroke_invisible));
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
borderSelectedListener.onBorderSelected(holder.getAdapterPosition());
}
});
}
public int getItemCount() {
return serverData.getBorderFramesDetail().size();
}
public interface BorderSelectedListener {
void onBorderSelected(int position);
}
class ViewHolder extends RecyclerView.ViewHolder {
final ImageView imageView;
final ProgressBar progress_bar;
ViewHolder(View itemView) {
super(itemView);
this.imageView = itemView.findViewById(R.id.imageView);
this.progress_bar = itemView.findViewById(R.id.progress_bar);
}
}
}
Related
Here, I have two recyclerview that has parentRecyclerViewAdapter and childRecyclerViewAdapter. Parent adapter has LinearLayoutManager.VERTICAL layout manager whereas Clild adapter has GridLayoutManager(mContext, 2) layout manager with itemDecoration.
When scrolling for the first time the RecyclerView scrolling is laggy and once the data is viewed the scrolling is smooth. Until the app instance is not completely removed the scrolling will be smooth and when the app reinitiate the scrolling is laggy again.
Please help me out to figure out this BUG!!
ParentRecyclerViewAdapter
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {
private ArrayList<SectionDataModel> dataList;
private Context mContext;
private RecyclerListItemClick onListClick;
public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> dataList) {
this.dataList = dataList;
this.mContext = context;
onListClick = (RecyclerListItemClick) context;
}
#Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ItemRowHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_template_section, null));
}
#Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {
ArrayList<SingleTemplateModel> templateModelArrayList = dataList.get(i).getTemplateModelArrayList();
String sectionName = dataList.get(i).getHeaderTitle();
itemRowHolder.itemTitle.setText(sectionName);
TemplateChooserAdapter itemListDataAdapter = new TemplateChooserAdapter(mContext, templateModelArrayList , dataList.get(i).getHeaderTitle());
itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);
}
#Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
public class ItemRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView itemTitle;
private RecyclerView recycler_view_list;
private ItemRowHolder(View view) {
super(view);
this.itemTitle = view.findViewById(R.id.itemTitle);
this.recycler_view_list = view.findViewById(R.id.recycler_view_list);
this.recycler_view_list.setOnClickListener(this);
this.recycler_view_list.setHasFixedSize(true);
this.recycler_view_list.setLayoutManager(new GridLayoutManager(mContext, 2));
this.recycler_view_list.addItemDecoration(new SpacesItemDecoration(2 , 25 , false));
}
#Override
public void onClick(View v) {
onListClick.onRecyclerItemClicked(dataList.get(getAdapterPosition()).getHeaderTitle());
}
}
}
ChildRecyclerAdapter
public class TemplateChooserAdapter extends RecyclerView.Adapter<TemplateChooserAdapter.ViewHolder> {
private static final String TAG = TemplateChooserAdapter.class.getSimpleName();
private Context context;
private ArrayList<SingleTemplateModel> templateModelArrayList;
private OnTemplatesListClicked onListClick;
public TemplateChooserAdapter(Context context, ArrayList<SingleTemplateModel> templateModelArrayList, String sectionName) {
this.context = context;
this.templateModelArrayList = templateModelArrayList;
onListClick = (OnTemplatesListClicked) context;
AppUtils.showLog(TAG, "CorporateUserAdapter");
}
#Override
public TemplateChooserAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_template_chooser, parent, false)
);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.templateView.setImageResource(templateModelArrayList.get(position).getImage());
}
#Override
public int getItemCount() {
return templateModelArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView templateView;
public ViewHolder(View itemView) {
super(itemView);
templateView = itemView.findViewById(R.id.template_view);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
templateModelArrayList.get(getAdapterPosition()).setShowIndicator(true);
onListClick.onTemplateClick(templateModelArrayList.get(getAdapterPosition())); // TODO send model when item clicked
}
}
}
Activity.java
private void recyclerViewJob() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
RecyclerViewDataAdapter adapter = new RecyclerViewDataAdapter(this, allSampleData);
recyclerView.setAdapter(adapter);
}
SectionDataModel.java
public class SectionDataModel {
private String headerTitle;
private ArrayList<SingleTemplateModel> templateModelArrayList;
public SectionDataModel() {
}
public SectionDataModel(String headerTitle, ArrayList<SingleTemplateModel> templateModelArrayList) {
this.headerTitle = headerTitle;
this.templateModelArrayList = templateModelArrayList;
}
public String getHeaderTitle() {
return headerTitle;
}
public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
}
public ArrayList<SingleTemplateModel> getTemplateModelArrayList() {
return templateModelArrayList;
}
public void setTemplateModelArrayList(ArrayList<SingleTemplateModel> templateModelArrayList) {
this.templateModelArrayList = templateModelArrayList;
}
}
SingleTemplateModel.java
public class SingleTemplateModel {
private String title;
private String skuName;
private int image;
private boolean showIndicator;
public SingleTemplateModel(String title, String skuName, int image, boolean showIndicator) {
this.title = title;
this.skuName = skuName;
this.image = image;
this.showIndicator = showIndicator;
}
public String getSkuName() {
return skuName;
}
public void setSkuName(String skuName) {
this.skuName = skuName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public boolean isShowIndicator() {
return showIndicator;
}
public void setShowIndicator(boolean showIndicator) {
this.showIndicator = showIndicator;
}
}
single_row_template_section.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="30dp"
android:clickable="true"
android:focusable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:text="Sample title"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingBottom="10dp"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
single_row_template_chooser.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="280dp"
card_view:cardElevation="6dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/template_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#mipmap/model_9" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I think you can try to lazy load your images from resources. There are libraries like Picasso or Glide that will help you with that.
So it may look like this:
Picasso:
import com.squareup.picasso.Picasso;
...
public class TemplateChooserAdapter extends RecyclerView.Adapter<TemplateChooserAdapter.ViewHolder> {
...
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Picasso.with(holder.itemView.getContext()).load(templateModelArrayList.get(position).getImage()).into(holder.templateView);
}
...
}
I wants to develop app in which user could choose multiple photos from gallery and can add caption like there is in whatsapp to add captions in multiple images
Anyone can help me in this.
If you looking for this,
You are on right place,
Here is the full solution I do for helping the beginners :
Layout UI Design
<ImageView
android:contentDescription="#string/app_name"
android:id="#+id/currentStreamImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<ImageView
android:id="#+id/selected_photo"
android:contentDescription="#string/app_name"
android:background="#null"
android:layout_margin="12dp"
android:layout_alignParentEnd="true"
android:src="#drawable/add_image_icon"
android:layout_width="40dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="10dp"
android:background="#drawable/fade_in_black"
android:id="#+id/captionArea"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/caption"
android:hint="#string/enter_caption_here"
android:textStyle="italic"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</LinearLayout>
AddImageWithCaptionFragment
public class AddImageWithCaptionFragment extends Fragment implements ImageWithCaptionListener {
private ArrayList<ImgCap> imgCapArrayList = new ArrayList<>();
private PerfectAdapter adapter;
private RecyclerView recyclerView;
private ImageView select,mainStream;
private EditText captionEt;
private int mCurrentPosition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.add_img_with_cap_layout, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
select = (ImageView) view.findViewById(R.id.selected_photo);
mainStream = (ImageView) view.findViewById(R.id.currentStreamImage);
captionEt = (EditText) view.findViewById(R.id.caption);
select.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TedBottomPicker bottomSheetDialogFragment = new TedBottomPicker.Builder(getActivity())
.setOnMultiImageSelectedListener(new TedBottomPicker.OnMultiImageSelectedListener() {
#Override
public void onImagesSelected(ArrayList<Uri> uriList) {
imgCapArrayList.clear();
for (int i=0;i<uriList.size();i++) {
ImgCap imgCap = new ImgCap(i,"", uriList.get(i));
imgCapArrayList.add(imgCap);
}
adapter = new PerfectAdapter(getActivity(),imgCapArrayList,mainStream,AddImageWithCaptionFragment.this);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
}
})
.setPeekHeight(1600)
.showTitle(false)
.setCompleteButtonText("Done")
.setEmptySelectionText("No Select")
.create();
bottomSheetDialogFragment.show(getActivity().getSupportFragmentManager());
}
});
captionEt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
imgCapArrayList.get(mCurrentPosition).setCaption(s.toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
#Override
public void imgCaptionCallBack(int position) {
mCurrentPosition = position;
captionEt.setText(imgCapArrayList.get(mCurrentPosition).getCaption());
}
}
Custom Adapter Class
public class PerfectAdapter extends RecyclerView.Adapter<PerfectAdapter.MyViewHolder>{
private LayoutInflater inflater;
private Context context;
private ArrayList<ImgCap> imgCapsList;
private ImageView mainStream;
private ImageWithCaptionListener mCallBack;
public PerfectAdapter(Context context,ArrayList<ImgCap> imgCapsList,ImageView mainStream,ImageWithCaptionListener mCallBack) {
inflater = LayoutInflater.from(context);
this.context = context;
this.imgCapsList = imgCapsList;
this.mainStream = mainStream;
this.mCallBack = mCallBack;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.image_item_layout, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder,final int position) {
final ImgCap element = imgCapsList.get(holder.getAdapterPosition());
Glide.with(context).load(element.getImagePath()).into(holder.image);
Glide.with(context).load(imgCapsList.get(0).getImagePath()).into(mainStream);
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Glide.with(context).load(element.getImagePath()).into(mainStream);
mCallBack.imgCaptionCallBack(position);
}
});
}
#Override
public int getItemCount() {
return imgCapsList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
}
Listener Class For CallBack
public interface ImageWithCaptionListener {
void imgCaptionCallBack(int position);
}
Item layout
<RelativeLayout
android:background="#android:color/white"
android:padding="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:contentDescription="#string/app_name"
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_width="100dp"
android:layout_height="100dp" />
</RelativeLayout>
POJO Class
public class ImgCap {
private int position;
private String caption;
private Uri imagePath;
public ImgCap(int position, String caption, Uri imagePath) {
this.position = position;
this.caption = caption;
this.imagePath = imagePath;
}
public int getPosition() {
return position;
}
public String getCaption() {
return caption;
}
public Uri getImagePath() {
return imagePath;
}
public void setPosition(int position) {
this.position = position;
}
public void setCaption(String caption) {
this.caption = caption;
}
public void setImagePath(Uri imagePath) {
this.imagePath = imagePath;
}
}
Just Copy and Paste , Enjoy !!!
I have a recyclerview which populates data from SQL database. Now each row in the recyclerview has a seekbar which when moved displays it's progress in a textview inside the same row. The problem is when I scroll the recyclerview up or down then return back to the first changed row, the seekbar is returned to its default position. How can I make it save the new position ? In normal activities/fragments I use lifecycle methods as "onPause" to save/restore the state. Here we have onAttachedToRecyclerView, I think it should solve my problem but I don't know exactly how.
EDIT : here is a full simple app files which I'm working on to test this problem.
MainActivity.class
public class MainActivity extends AppCompatActivity {
private List<Score> scoreList = new ArrayList<>();
private RecyclerView recyclerView;
private MyAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mAdapter = new MyAdapter(scoreList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareScoreData();
}
private void prepareScoreData() {
Score score = new Score("title", 5);
scoreList.add(score);
for(int i= 0; i<1000; i++){
score = new Score("title", 5);
scoreList.add(score);
}
mAdapter.notifyDataSetChanged();
}
}
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Score> scoresList;
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title, scoreView;
SeekBar seekbar;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
scoreView = (TextView) view.findViewById(R.id.score);
seekbar = (SeekBar) view.findViewById(R.id.seekbar);
}
}
public MyAdapter(List<Score> scoresList) {
this.scoresList = scoresList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Score score = scoresList.get(position);
holder.title.setText(score.getTitle());
if (!score.getProgressed()) {
holder.seekbar.setProgress(0) ;
} else {
holder.seekbar.setProgress(score.getSeekbarProgress());
}
holder.seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
holder.scoreView.setText(String.valueOf(i));
score.setSeekbarProgress(i);
score.setProgressed(true);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
#Override
public int getItemCount() {
return scoresList.size();
}
}
Score class
public class Score {
private String title;
int seekbarProgress;
boolean progressed;
public Score() {
}
public Score(String title,int seekbarProgress) {
this.title = title;
this.seekbarProgress = seekbarProgress;
}
public void setProgressed(boolean progressed) {
this.progressed = progressed;
}
public void setTitle(String title) {
this.title = title;
}
public void setSeekbarProgress(int seekbarProgress) {
this.seekbarProgress = seekbarProgress;
}
public String getTitle() {
return title;
}
public int getSeekbarProgress() {
return seekbarProgress;
}
public boolean getProgressed() {
return progressed;
}
}
MainActivity_Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.moaness.tut_recyclerview.MainActivity">
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="60dp"
android:paddingBottom="60dp"
android:layout_marginBottom="10dp"
android:clickable="true"
android:background="#f2f2f2"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:text="title"
android:textColor="#color/title"
android:textSize="16dp"
android:paddingTop="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/score"
android:text="score"
android:layout_below="#+id/title"
android:textSize="16dp"
android:paddingBottom="16dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/score"
android:id="#+id/seekbar"
/>
</RelativeLayout>
If you are using recyclerview you need to maintain states of each row, means if you are checking using a condition(i.e. if) at any stage of recyclerview item(in recyclerview adapter class) then you need to handle else as well. I can send you a code snippet so you can have a good idea for recyclerview adapter.
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
List<ViewHolder> holders = new ArrayList<ViewHolder>();
private ArrayList<ContactModel> arrayList = new ArrayList<>();
private Context context;
private LayoutInflater inflater;
public void clearAdapter() {
arrayList.clear();
notifyDataSetChanged();
}
public ContactsAdapter(Context context, ArrayList<ContactModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
public void setList(ArrayList<ContactModel> listSearch) {
this.arrayList = listSearch;
notifyItemRangeChanged(0, listSearch.size());
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.custom_row_for_contacts, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
holders.add(viewHolder);
return viewHolder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final ContactModel current = this.arrayList.get(position);
holder.txtDriverName.setText(current.getName());
holder.txtDriverPhone.setText(current.getPhone());
if (current.getImgUrl().length() > 0) {
String urlLicenceThumb = UrlEndPoints.parentUrl + current.getImgUrl();
Glide.with(context).load(urlLicenceThumb).error(R.mipmap.ic_launcher).into(holder.imgDriver);
} else {
Glide.with(context).load(R.mipmap.ic_launcher).into(holder.imgDriver);
}
}
public void delete(int position) {
arrayList.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
return (null != arrayList ? arrayList.size() : 0);
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtDriverName, txtDriverPhone;
private CircleImageView imgDriver;
private Button btnInvite;
private CheckBox chkAdd;
public ViewHolder(View itemView) {
super(itemView);
chkAdd = (CheckBox) itemView.findViewById(R.id.chkAdd);
imgDriver = (CircleImageView) itemView.findViewById(R.id.imgDriver);
txtDriverName = (TextView)itemView.findViewById(R.id.txtDriverName);
txtDriverPhone = (TextView) itemView.findViewById(R.id.txtDriverPhone);
btnInvite = (Button) itemView.findViewById(R.id.btnInvite);
}
}
}
I just moved into the android RecyclerView and CardViewand ... and realized recyclerview don't have an option for Onclick listner, but after implement this feature, i just don't see any result.
hear is my code and layouts:
MainActivity:
public class MainActivity extends AppCompatActivity {
//private usefulSites_RecyclerView_Adapter mAdapter;
final Context context = this;
private RecyclerView mRecyclerView;
private StaggeredGridLayoutManager mGridLayoutManager;
private RecyclerViewAdapter mAdapter;
private String[] mList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
//使用CollapsingToolbarLayout必须把title设置到CollapsingToolbarLayout上,设置到Toolbar上则不会显示
CollapsingToolbarLayout mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
mCollapsingToolbarLayout.setTitle("test");
//通过CollapsingToolbarLayout修改字体颜色
mCollapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);//设置还没收缩时状态下字体颜色
mCollapsingToolbarLayout.setCollapsedTitleTextColor(Color.GREEN);
//mList = getResources().getStringArray(R.array.numbers);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(mGridLayoutManager);
mAdapter = new RecyclerViewAdapter(getApplicationContext());
mRecyclerView.setAdapter(mAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private Context mContext;
private String [] mSiteTitle,mSiteLink;
private int [] mThumbnail;
public RecyclerViewAdapter(Context contexts, String[] list) {
this.mContext = contexts;
this.mSiteTitle = list;
}
public RecyclerViewAdapter(Context contexts) {
this.mContext = contexts;
initList();
}
private void initList(){
UsfulSite_init mInit=new UsfulSite_init();
mSiteTitle= mInit.initSiteTitle();
mSiteLink=mInit.initSiteLink();
mThumbnail=mInit.initThumbnail();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.usful_sites_card_view, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.sTitle.setText(mSiteTitle[position]);
holder.sLink.setText(mSiteTitle[position]);
holder.sImage.setImageResource(mThumbnail[position]);
holder.setClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
if (isLongClick) {
Toast.makeText(mContext, "#" + position + " - " + mSiteTitle[position] + " (Long click)", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "#" + position + " - " + mSiteTitle[position], Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public int getItemCount() {
return mSiteTitle.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener{
private TextView sTitle,sLink;
private ImageView sImage;
private ItemClickListener clickListener;
public ViewHolder(View itemView) {
super(itemView);
sTitle = (TextView)itemView.findViewById(R.id.useful_sites_Title);
sLink=(TextView)itemView.findViewById(R.id.useful_sites_Link);
sImage=(ImageView)itemView.findViewById(R.id.useful_sites_thumbnail);
itemView.setTag(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
#Override
public void onClick(View view) {
clickListener.onClick(view, getPosition(), false);
}
#Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getPosition(), true);
return true;
}
}
}
Activity_main.xml:
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#30469b"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/a"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
CardView Layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="4dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/mainHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/useful_sites_thumbnail"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:tint="#color/photo_tint"
android:layout_centerInParent="true"
/>
<TextView
android:id="#+id/useful_sites_Title"
android:gravity="center"
android:background="?android:selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="24sp"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
/>
<TextView
android:id="#+id/useful_sites_Link"
android:gravity="center"
android:background="?android:selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="24sp"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
android:visibility="gone"
/>
</RelativeLayout>
I tested another ways for clickListner in RV butT, i just stuck in this.
can some one look at my code and guide me, Little about this.
If you want to implement onClick for each cardview, you can do like this:
in your XML:
<android.support.v7.widget.CardView
android:id="#+id/card_view">
...
</android.support.v7.widget.CardView>
Then, in your adapter code:
public static class ViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.card_view);
}
}
...
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//implement onClick
System.out.println("Clicked");
}
});
}
}
You can pass listener from Adapter to ViewHolder, and listen events there, and you also can pass event from Adapter to your Activity or where you create this Adapter
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
private List<Album> albums;
private Context context;
public AlbumAdapterClickListener recListener;
public AlbumAdapter(List<Album> albums, Context context, AlbumAdapterClickListener recListener) {
this.albums = albums;
this.context = context;
this.recListener = recListener;
}
public interface AlbumAdapterClickListener {
void recyclerViewClick(String albumID);
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title;
public TextView count;
public ImageView picture;
public String albumID;
public AlbumClickListener listener;
//listener passed to viewHolder
public interface AlbumClickListener {
void albumOnClick(String albumID);
}
public ViewHolder(View v, AlbumClickListener listener) {
super(v);
title = (TextView) v.findViewById(R.id.album_list_title);
count = (TextView) v.findViewById(R.id.album_list_count);
picture = (ImageView) v.findViewById(R.id.album_list_picture);
this.listener = listener;
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
listener.albumOnClick(this.albumID);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.album_list_item, viewGroup, false),
new ViewHolder.AlbumClickListener() {
#Override
public void albumOnClick(String albumID) {
//TODO show gridView with current albumID
Log.e("fdf", albumID);
// albumID going to Fragment
recListener.recyclerViewClick(albumID);
}
});
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
Album album = albums.get(i);
viewHolder.albumID = album.getId();
viewHolder.count.setText(album.getPhotoCount());
viewHolder.title.setText(album.getName());
Picasso.with(context).load(albums.get(i).getCoverURL()).placeholder(R.mipmap.fb_place_holder).resize(140, 140)
.centerCrop().into(viewHolder.picture);
}
#Override
public int getItemCount() {
return albums.size();
}
}
The answer from VLeong works great for me! But a little clarification. This :
holder.cardView.setOnClickListener(new View.OnClickListener() {
need to be changed to :
holder.itemView.setOnClickListener(new View.OnClickListener(){
You Can Add your listener into constructor of View Holder.
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.album_list_title);
count = (TextView) v.findViewById(R.id.album_list_count);
picture = (ImageView) v.findViewById(R.id.album_list_picture);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Handel yout event here
}
});
}
I'm trying to implement a recyclerview that behaves like my sketch below:
The idea is that there is a parent list, when an list item in the parent list is tapped, that list item reveals a child list that contains it's own data. when a list item is tapped in the child list, the value of that child is reflected and updates the value of the parent in the parent list item.
I've tried to get it working for the past 3 days to no avail. I tried using the AdvancedReyclerview library but to a beginner like me, it was a giant mess of things that didn't make sense especially when passing in the data. I copied and pasted files that I needed to get a minimal working version but I had no idea how to pass my data into the recyclerview and how to update it with the newly selected value.
Is it even possible to do what I'm trying to do or am I way out of my depth here?
If it's still difficult to understand, I can explain it more.
EDIT: someone recommended that I do this with the ExpandableListView rather than the RecyclerView. Any thoughts on that?
1.ExpandableRecyclerAdapter.class
public abstract class ExpandableRecyclerAdapter<T extends ExpandableRecyclerAdapter.ListItem> extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
protected Context mContext;
protected List<T> allItems = new ArrayList<>();
protected List<T> visibleItems = new ArrayList<>();
private List<Integer> indexList = new ArrayList<>();
private SparseIntArray expandMap = new SparseIntArray();
private int mode;
protected static final int TYPE_HEADER = 1000;
private static final int ARROW_ROTATION_DURATION = 150;
public static final int MODE_NORMAL = 0;
public static final int MODE_ACCORDION = 1;
public ExpandableRecyclerAdapter(Context context) {
mContext = context;
}
public static class ListItem {
public int ItemType;
public ListItem(int itemType) {
ItemType = itemType;
}
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public int getItemCount() {
return visibleItems == null ? 0 : visibleItems.size();
}
protected View inflate(int resourceID, ViewGroup viewGroup) {
return LayoutInflater.from(mContext).inflate(resourceID, viewGroup, false);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
}
}
public class HeaderViewHolder extends ViewHolder {
ImageView arrow;
public HeaderViewHolder(View view) {
super(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleExpandedItems(getLayoutPosition(),false);
/*if(isExpanded(getLayoutPosition())){
collapseItems(getLayoutPosition(),false);
}else {
expandItems(getLayoutPosition(),true);
}*/
}
});
}
public HeaderViewHolder(View view, final ImageView arrow) {
super(view);
this.arrow = arrow;
arrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handleClick();
}
});
}
protected void handleClick() {
if (toggleExpandedItems(getLayoutPosition(), false)) {
openArrow(arrow);
} else {
closeArrow(arrow);
}
}
public void bind(int position) {
arrow.setRotation(isExpanded(position) ? 90 : 0);
}
}
public boolean toggleExpandedItems(int position, boolean notify) {
if (isExpanded(position)) {
collapseItems(position, notify);
return false;
} else {
expandItems(position, notify);
if (mode == MODE_ACCORDION) {
collapseAllExcept(position);
}
return true;
}
}
public void expandItems(int position, boolean notify) {
int count = 0;
int index = indexList.get(position);
int insert = position;
for (int i=index+1; i<allItems.size() && allItems.get(i).ItemType != TYPE_HEADER; i++) {
insert++;
count++;
visibleItems.add(insert, allItems.get(i));
indexList.add(insert, i);
}
notifyItemRangeInserted(position + 1, count);
int allItemsPosition = indexList.get(position);
expandMap.put(allItemsPosition, 1);
if (notify) {
notifyItemChanged(position);
}
}
public void collapseItems(int position, boolean notify) {
int count = 0;
int index = indexList.get(position);
for (int i=index+1; i<allItems.size() && allItems.get(i).ItemType != TYPE_HEADER; i++) {
count++;
visibleItems.remove(position + 1);
indexList.remove(position + 1);
}
notifyItemRangeRemoved(position + 1, count);
int allItemsPosition = indexList.get(position);
expandMap.delete(allItemsPosition);
if (notify) {
notifyItemChanged(position);
}
}
protected boolean isExpanded(int position) {
int allItemsPosition = indexList.get(position);
return expandMap.get(allItemsPosition, -1) >= 0;
}
#Override
public int getItemViewType(int position) {
return visibleItems.get(position).ItemType;
}
public void setItems(List<T> items) {
allItems = items;
List<T> visibleItems = new ArrayList<>();
expandMap.clear();
indexList.clear();
for (int i=0; i<items.size(); i++) {
if (items.get(i).ItemType == TYPE_HEADER) {
indexList.add(i);
visibleItems.add(items.get(i));
}
}
this.visibleItems = visibleItems;
notifyDataSetChanged();
}
protected void removeItemAt(int visiblePosition) {
int allItemsPosition = indexList.get(visiblePosition);
allItems.remove(allItemsPosition);
visibleItems.remove(visiblePosition);
incrementIndexList(allItemsPosition, visiblePosition, -1);
incrementExpandMapAfter(allItemsPosition, -1);
notifyItemRemoved(visiblePosition);
}
private void incrementExpandMapAfter(int position, int direction) {
SparseIntArray newExpandMap = new SparseIntArray();
for (int i=0; i<expandMap.size(); i++) {
int index = expandMap.keyAt(i);
newExpandMap.put(index < position ? index : index + direction, 1);
}
expandMap = newExpandMap;
}
private void incrementIndexList(int allItemsPosition, int visiblePosition, int direction) {
List<Integer> newIndexList = new ArrayList<>();
for (int i=0; i<indexList.size(); i++) {
if (i == visiblePosition) {
if (direction > 0) {
newIndexList.add(allItemsPosition);
}
}
int val = indexList.get(i);
newIndexList.add(val < allItemsPosition ? val : val + direction);
}
indexList = newIndexList;
}
public void collapseAll() {
collapseAllExcept(-1);
}
public void collapseAllExcept(int position) {
for (int i=visibleItems.size()-1; i>=0; i--) {
if (i != position && getItemViewType(i) == TYPE_HEADER) {
if (isExpanded(i)) {
collapseItems(i, true);
}
}
}
}
public void expandAll() {
for (int i=visibleItems.size()-1; i>=0; i--) {
if (getItemViewType(i) == TYPE_HEADER) {
if (!isExpanded(i)) {
expandItems(i, true);
}
}
}
}
public static void openArrow(View view) {
view.animate().setDuration(ARROW_ROTATION_DURATION).rotation(180);
}
public static void closeArrow(View view) {
view.animate().setDuration(ARROW_ROTATION_DURATION).rotation(0);
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
}
2.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:id="#+id/main_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3.item_header
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="#dimen/standard_padding">
<LinearLayout
android:id="#+id/lnr_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/txt_header_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#mipmap/ic_usa"
android:gravity="center"
android:text="Beverly Hills"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:id="#+id/img_arrow"
android:layout_width="#dimen/arrow_size"
android:layout_height="#dimen/arrow_size"
android:layout_alignParentRight="true"
android:src="#mipmap/arrow" />
<TextView
android:id="#+id/txt_header_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="Home"
android:textStyle="bold" />
4.item_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rcl_header_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_cancle" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/btn_save" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lnr_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rcl_header_btn"
android:gravity="center_vertical"
android:orientation="vertical">
<EditText
android:id="#+id/edt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="DESCRIPTION" />
<EditText
android:id="#+id/edt_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address" />
<LinearLayout
android:id="#+id/lnr_child_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edt_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="City" />
<EditText
android:id="#+id/edt_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="State" />
</LinearLayout>
<LinearLayout
android:id="#+id/lnr_child_2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edt_zipcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Zip Code" />
<EditText
android:id="#+id/edt_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Country" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/rcl_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lnr_parent">
<CheckBox
android:id="#+id/chk_marked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txt_type" />
<TextView
android:id="#+id/txt_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_delete"
android:layout_toRightOf="#+id/chk_marked"
android:gravity="center"
android:text="SET AS DEFAULT" />
<Button
android:id="#+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="DELETE" />
</RelativeLayout>
5.Adapter
public class PeopleAdapter extends ExpandableRecyclerAdapter<PeopleAdapter.PeopleListItem> {
public static final int TYPE_PERSON = 1001;
public PeopleAdapter(Context context) {
super(context);
setItems(getSampleItems());
}
public static class PeopleListItem extends ExpandableRecyclerAdapter.ListItem {
public String Text;
public PeopleListItem(String group) {
super(TYPE_HEADER);
Text = group;
}
public PeopleListItem(String first, String last) {
super(TYPE_PERSON);
Text = first + " " + last;
}
}
public class HeaderViewHolder extends ExpandableRecyclerAdapter.HeaderViewHolder {
TextView name;
public HeaderViewHolder(View view) {
super(view, (ImageView) view.findViewById(R.id.img_arrow));
name = (TextView) view.findViewById(R.id.txt_header_name);
}
public void bind(int position) {
super.bind(position);
name.setText(visibleItems.get(position).Text);
}
}
public class PersonViewHolder extends ExpandableRecyclerAdapter.ViewHolder {
EditText name;
public PersonViewHolder(View view) {
super(view);
name = (EditText) view.findViewById(R.id.edt_description);
}
public void bind(int position) {
name.setText(name.getText());
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_HEADER:
return new HeaderViewHolder(inflate(R.layout.item_header, parent));
case TYPE_PERSON:
default:
return new PersonViewHolder(inflate(R.layout.item_content, parent));
}
}
#Override
public void onBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case TYPE_HEADER:
((HeaderViewHolder) holder).bind(position);
break;
case TYPE_PERSON:
default:
((PersonViewHolder) holder).bind(position);
break;
}
}
private List<PeopleListItem> getSampleItems() {
List<PeopleListItem> items = new ArrayList<>();
items.add(new PeopleListItem("Friends"));
items.add(new PeopleListItem("", ""));
items.add(new PeopleListItem("Friends"));
items.add(new PeopleListItem("", ""));
return items;
}
}
6.MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recycler;
PeopleAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recycler = (RecyclerView) findViewById(R.id.main_recycler);
adapter = new PeopleAdapter(this);
adapter.setMode(ExpandableRecyclerAdapter.MODE_ACCORDION);
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_expand_all:
adapter.expandAll();
return true;
case R.id.action_collapse_all:
adapter.collapseAll();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You can check my library in here
And create something like below code:
public class PurchaseItemRecyclerViewAdapter extends ExpandableRecyclerView.Adapter<PurchaseItemRecyclerViewAdapter.ChildViewHolder,ExpandableRecyclerView.SimpleGroupViewHolder,String,String>
{
List<ItemModel> itemModels;
public PurchaseItemRecyclerViewAdapter() {
this.itemModels = new ArrayList<>();
itemModels.add(new ItemModel("group 0",3,"subitem :"));
itemModels.add(new ItemModel("group 1",3,"subitem :"));
itemModels.add(new ItemModel("group 2",2,"subitem :"));
itemModels.add(new ItemModel("group 3",1,"subitem :"));
itemModels.add(new ItemModel("group 4",3,"subitem :"));
itemModels.add(new ItemModel("group 5",5,"subitem :"));
}
#Override
public int getGroupItemCount() {
return itemModels.size();
}
#Override
public int getChildItemCount(int i) {
return itemModels.get(i).getSubItemCount();
}
#Override
public String getGroupItem(int i) {
return itemModels.get(i).getParentName();
}
#Override
public String getChildItem(int group, int child) {
return itemModels.get(group).getSubItemPrefix() + child;
}
#Override
protected ExpandableRecyclerView.SimpleGroupViewHolder onCreateGroupViewHolder(ViewGroup parent)
{
return new ExpandableRecyclerView.SimpleGroupViewHolder(parent.getContext());
}
#Override
protected ChildViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType)
{
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.purchase_list_content,parent,false);
return new ChildViewHolder(rootView);
}
#Override
public void onBindGroupViewHolder(ExpandableRecyclerView.SimpleGroupViewHolder holder, int group) {
super.onBindGroupViewHolder(holder, group);
holder.setText(getGroupItem(group));
}
#Override
public void onBindChildViewHolder(ChildViewHolder holder, final int group, int position)
{
super.onBindChildViewHolder(holder, group, position);
holder.name.setText(getChildItem(group, position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemModels.get(group).setParentName("edited Parent");
notifyItemChanged(group);
}
});
}
#Override
public int getChildItemViewType(int i, int i1) {
return 1;
}
public class ChildViewHolder extends RecyclerView.ViewHolder
{
private TextView name;
public ChildViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.item_name);
}
}
}
and this ItemModel class:
public class ItemModel {
String parentName;
int subItemCount;
String subItemPrefix;
public ItemModel(String parentName, int subItemCount, String subItemPrefix) {
this.parentName = parentName;
this.subItemCount = subItemCount;
this.subItemPrefix = subItemPrefix;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public int getSubItemCount() {
return subItemCount;
}
public void setSubItemCount(int subItemCount) {
this.subItemCount = subItemCount;
}
public String getSubItemPrefix() {
return subItemPrefix;
}
public void setSubItemPrefix(String subItemPrefix) {
this.subItemPrefix = subItemPrefix;
}
}
This is bit late but take a look on this advanced recyclerview library https://github.com/h6ah4i/android-advancedrecyclerview
In their documentation you can see Expandable item related classes/interfaces
check it out.
You can easily achieve it with this library, there is a full example here.
Basically you group your items into sections:
class MySection extends StatelessSection {
String header;
List<String> list;
boolean expanded = true;
public MySection(String header, List<String> list) {
// call constructor with layout resources for this Section header and items
super(R.layout.section_header, R.layout.section_item);
this.myHeader = header;
this.myList = list;
}
#Override
public int getContentItemsTotal() {
return expanded? list.size() : 0;
}
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
final HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.tvTitle.setText(title);
headerHolder.rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expanded = !expanded;
headerHolder.imgArrow.setImageResource(
expanded ? R.drawable.ic_keyboard_arrow_up_black_18dp : R.drawable.ic_keyboard_arrow_down_black_18dp
);
sectionAdapter.notifyDataSetChanged();
}
});
}
#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));
}
}
Then create instance of your sections and set up your adapter:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Add your Sections
sectionAdapter.addSection(new MySection("A", Arrays.asList(new String[] {"a", "b", "c" })));
sectionAdapter.addSection(new MySection("B", Arrays.asList(new String[] {"d", "e", "f" })));
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);