I have a recycler view in which I populate a cardview consists of an image and two text fields. It works fine. What i want to know is there some way to remove an image from cardview if it is null (and not showing the blank space for image)and just show the text fields for that specific item in recycler view.
I am parsing a JSON service containing images and text. But some of the images are missing while parsing data and I want to remove that image space while displaying data.
Below is my Cardview.xml i am using inside Recyclerview adapter.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#android:color/black"
android:clickable="true"
android:contextClickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:longClickable="true"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:background="#android:color/black"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textIsSelectable="true"
android:textSize="30dp"
android:textStyle="bold" />
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/heading"
android:background="#CCC" />
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view"
android:background="#000000"
android:clickable="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
/>
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/date"
android:background="#CCC" />
<TextView
android:id="#+id/brief"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view1"
android:background="#000000"
android:clickable="true"
android:contextClickable="true"
android:elegantTextHeight="true"
android:hyphenationFrequency="full"
android:linksClickable="true"
android:text="Large Text"
android:textAlignment="viewStart"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textColorHighlight="#ffffff"
android:textStyle="italic" />
<View
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/brief"
android:background="#CCC" />
</RelativeLayout>
</android.support.v7.widget.CardView>
The RecyclerViewAdapter class is :
private LayoutInflater inflater;
Context context;
List<Data> dataArray;
private int lastPosition = -1;
public RecyclerViewAdapter(Context context) {
//this.dataArray = dataArray;
this.context = context;
inflater = LayoutInflater.from(context);
}
public void setDataArray(List<Data> dataArray) {
this.dataArray = dataArray;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, null);
View view = inflater.inflate(R.layout.cardview, parent, false);
CustomViewHolder holder = new CustomViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Data current = dataArray.get(position);
holder.textView1.setText(current.heading);
holder.textView2.setText(current.date);
holder.textView3.setText(current.brief);
// Using picasso to fetch image as the user scrolls down ... No need to store
// all the images during start up.
Uri uri = Uri.parse(current.getLImage());
//System.out.println("URIiii issss::::"+uri);
Picasso.with(context).load(uri).into(holder.image);
// Animation
setAnimation(holder.relativeLayout, position);
}
#Override
public int getItemCount() {
return dataArray.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView image;
RelativeLayout relativeLayout;
TextView textView1, textView2, textView3;
public CustomViewHolder(View itemView) {
super(itemView);
textView1 = (TextView) itemView.findViewById(R.id.heading);
textView2 = (TextView) itemView.findViewById(R.id.date);
textView3 = (TextView) itemView.findViewById(R.id.brief);
image = (ImageView) itemView.findViewById(R.id.imageView);
}
#Override
public void onClick(View v) {
}
}
private void setAnimation(View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition || position < lastPosition) {
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
}
What i really want is to remove the imageview item inside carview if it is null. I am relatively new to android, so any guideline would be highly appreciated.
Try with this.
If your Image URI is null.
if(uri == null){
Picasso.with(this.context).cancelRequest(holder.image);
holder.image.setVisibility(View.GONE);
}
Hope this helps you.
check two things for this,
first check your limage is null or not, if it is null than hide your image view.
one another thing is check image is already exists on your server url. if not exists on server hide your image view.
you can try below code for that,
if(current.getLImage() == null || !imageExists(**yourImageUrl**)){
Picasso.with(this.context).cancelRequest(holder.image);
holder.image.setVisibility(View.GONE);
}
public static boolean imageExists(String URLName){
HttpClient client= new DefaultHttpClient();
HttpHead headMethod = new HttpHead(urlToImage);
HttpResponse response = client.execute(headMethod);
if(response.getStatusLine().getStatusCode== HttpStatus.SC_NOT_FOUND) {
return false;
} else {
return true;
}
}
Related
I made a ListView with a custom adapter with my own layout, and on this layout there is a text view, a button and three image buttons that are at first with setVisibility.GONE
I am trying to animate this button for when the user clicks on it, it changes its position from right to left to give space for the three image buttons.
The problem is, the only item in which the animation is working is the last item on the list. I want the animation to work in all of the items.
Here's the code for my adapter:
public class ListaAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;
private Button btnAbrir;
private RelativeLayout relativeLayout;
private ObjectAnimator animation;
public ListaAdapter (ArrayList<String> list, Context context, ListView a) {
this.list = list;
this.context = context;
this.lista = a;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.textoItem);
listItemText.setText(list.get(position));
final LinearLayout btnLayout = (LinearLayout)
view.findViewById(R.id.btnLayout);
relativeLayout = (RelativeLayout) view.findViewById(R.id.layout);
btnAbrir = (Button)view.findViewById(R.id.btnAbrir);
ImageButton btnNaoSei = (ImageButton)view.findViewById(R.id.btnNaoSei);
ImageButton btnAceitar =
(ImageButton)view.findViewById(R.id.btnAceitar);
ImageButton btnNegar = (ImageButton)view.findViewById(R.id.btnNegar);
animation = ObjectAnimator.ofFloat(btnAbrir,"x",200);
btnLayout.setVisibility(View.GONE);
btnAbrir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnLayout.setVisibility(View.VISIBLE);
animate();
}
});
return view;
}
private void animate()
{
animation.setDuration(500);
animation.start();
}
}
And this is my custom_list.xml layout:
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/white">
<LinearLayout
android:id="#+id/btnLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_barra">
<ImageButton
android:id="#+id/btnNaoSei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:backgroundTint="#color/compras_barra"
android:contextClickable="false"
app:srcCompat="#drawable/a12" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNaoSei"
android:layout_centerHorizontal="true"
android:text="NÃO SEI"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras">
<ImageButton
android:id="#+id/btnAceitar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:background="#color/compras"
app:srcCompat="#drawable/a11" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnAceitar"
android:paddingEnd="#dimen/_3sdp"
android:paddingTop="#dimen/_8sdp"
android:text="ACEITAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_texto2">
<ImageButton
android:id="#+id/btnNegar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#color/compras_texto2"
app:srcCompat="#drawable/a10" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:paddingBottom="#dimen/_1sdp"
android:text="NEGAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/btnAbrir"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/a09"
android:layout_alignTop="#+id/textoItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textoItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:maxWidth="200dp"
android:text="TextView"
android:textColor="#color/compras_texto2" />
</RelativeLayout>
Any idea on how to accomplish this? Thanks in advance.
The things you need to adopt the following method and create a ViewHolder so that your adapter will know each element of each row in the List
The last UI element of the row only animates because this the only last known id of components for your Adapater when he was creating the Views in your List. So it will animates for the last row elements
You have to add ViewHolder nested class inside your Adapter and declare your UI components. Use setTag and getTag methods inside the getView
Overall you have to create your adapter things like this in getView
public class SListAdapter extends ArrayAdapter<String> {
private Context context;
private String[] seCtnColors;
private List<Subscription> item;
private ViewHolder mainViewHolder = null;
private ViewHolder viewHolder;
private LayoutInflater inflater;
private View row;
public SListAdapter(Context c, List<Subscription> subscriptions)
{
super(c, R.layout.row,R.id.rowNameTV);
this.context=c;
this.item = subscriptions;
}
#Override
public int getCount() {
return this.item.size();
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
row = null;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row,parent,false);
viewHolder = new ViewHolder();
initAdapterUI();
setAdapterDataset(position);
return row;
}
private void initAdapterUI() {
viewHolder.animatedGifViewHol = row.findViewById(R.id.row_animated_gif);
viewHolder.alertBarVerticalViewHol = row.findViewById(R.id.alertBarVerticalView);
viewHolder.firstNameTVHol = (TextView) row.findViewById(R.id.rowNameTV);
viewHolder.phoneNumberTVHol = (TextView) row.findViewById(R.id.rowNumberTV);
viewHolder.switchStateTVHol = (TextView) row.findViewById(R.id.switchStateTV);
row.setTag(viewHolder);
}
private void setAdapterDataset(int position) {
mainViewHolder = (ViewHolder) row.getTag();
mainViewHolder.alertBarVerticalViewHol.setBackgroundColor(Color.RED);
mainViewHolder.switchStateTVHol.setTextColor(Color.RED);
mainViewHolder.firstNameTVHol.setText(item.get(position).getFirstName());
mainViewHolder.phoneNumberTVHol.setText(item.get(position).getNumber());
}
public class ViewHolder{
View animatedGifViewHol;
View alertBarVerticalViewHol;
TextView firstNameTVHol;
TextView switchStateTVHol;
TextView phoneNumberTVHol;
}
}
Before you ask, yes I know there are many questions that are very similar to this and I have tried most of them to no avail. My problem is that a CardView is not displaying within a RecyclerView. The items whithin it are displaying but not the card itself.
Without further or do, here's my code:
Adapter:
Integer count = 0;
Boolean isStart = true;
String datag = "";
String typeg = "";
Integer LastItemType=0; //0=None 1=Text 2=Image
ViewHolder a;
#Override
public EntryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
a= createholder(parent, viewType);
return(a);
}
public ViewHolder createholder(ViewGroup parent, int viewtype) {
if (typeg.equals("image")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
CardView card = (CardView) root.findViewById(R.id.card_view);
ImageView image = (ImageView) root.findViewById(R.id.Img);
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
if (LastItemType == 2) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Img));
}
if (LastItemType == 1) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Txt));
}
ViewHolder vh = new ViewHolder(image, card);
LastItemType = 2;
return vh;
} else {
if (typeg.equals("text")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
CardView card = (CardView) root.findViewById(R.id.card_view);
TextView image = (TextView) root.findViewById(R.id.Txt);
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
if (LastItemType == 1) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Txt));
}
if (LastItemType == 2) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Img));
}
ViewHolder vh = new ViewHolder(image, card);
LastItemType = 1;
return vh;
}
return null; //TODO: REMOVE!
}
}
#Override
public void onBindViewHolder(EntryAdapter.ViewHolder holder, int position) {
// Deal with data
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgg;
public TextView txtg;
public CardView cardg;
public ViewHolder(ImageView image, CardView card) {
super(image);
imgg = image;
cardg = card;
}
public ViewHolder(TextView text, CardView card) {
super(text);
txtg = text;
cardg = card;
}
}
#Override
public int getItemCount() {
return count;
}
public void refresh(String data, String type, ViewGroup parent) {
isStart = false;
datag = data;
typeg = type;
count++;
createholder(parent, -100);
}
listitems.xml:
<?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.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:id="#+id/Img"
android:scaleType="center"
android:maxHeight="100dp"
android:maxWidth="150dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.25"
android:id="#+id/Txt"
android:layout_gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Thanks in advance!
Check out my code, i use it for each of my app after modifying it slightly. You can also use adapter if you have more than one RecyclerViews by changing it's type field and layout and views accordingly. It contains a CoordinatorLayout that has CollapsingLayout with ImageView in it. It contains many layouts for Material Design, i hope it helps.
Activity contains RecyclerView and CardView, i removed things like database, Floating action buttons that you may not need and may make it more difficult to understand. If you need the whole class contact me.
public class MeasureListActivity extends AppCompatActivity
implements MeasureListAdapter.OnRecyclerViewMeasureClickListener {
// Views
private RecyclerView mRecyclerView;
private MeasureListAdapter mAdapter;
private Toolbar toolbar;
// List that keeps values displayed on the screen
private List<Measure> listMeasure;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prev_measures);
setViews();
}
private void setViews() {
/*
* Set toolbar and arrow icon to return back
*/
toolbar = (Toolbar) findViewById(R.id.toolbarPrevMeasure);
setSupportActionBar(toolbar);
// Enable home button for API < 14
getSupportActionBar().setHomeButtonEnabled(true);
// Enable home button for API >= 14
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*
* RecylerView to display items as a list
*/
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewPrevMeasure);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new MeasureListAdapter(this, listMeasure, 0);
// Attach an instance of OnRecyclerViewMeasureClickListener that
// implements itemClicked()
mAdapter.setClickListener(this);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void itemMeasureClicked(View view, int position) {
}
}
public class MeasureListAdapter extends RecyclerView.Adapter<MeasureListAdapter.MyViewHolder> {
private LayoutInflater inflater;
private List<Measure> data = Collections.emptyList();
// This is for delegating event from adapter's onClick() method to
// NavigationDrawerFragment
private OnRecyclerViewMeasureClickListener recyclerClickListener;
private DecimalFormat decimalFormat;
private int type = 0;
private Context mContext;
public MeasureListAdapter(Context context, List<Measure> data, int type) {
mContext = context;
inflater = LayoutInflater.from(context);
this.data = data;
decimalFormat = new DecimalFormat("###.#");
this.type = type;
}
#Override
public int getItemCount() {
return data.size();
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
Measure measure = data.get(position);
String title = measure.getTitle();
String note = measure.getNote();
String date = measure.getFormattedDate();
double angle = measure.getAnglePhoto();
// Compass
double azimuth = measure.getAngleAzimuth();
double pitch = measure.getAnglePitch();
double roll = measure.getAngleRoll();
String bearing = measure.getBearing();
holder.tvTitle.setText(title);
holder.tvNote.setText(note);
holder.tvAngle.setText(
mContext.getString(R.string.angle) + ": " + decimalFormat.format(angle) + ConstantsApp.DEGREE_ICON);
// Compass
holder.tvAzimuth.setText(
mContext.getString(R.string.azimuth) + ": " + decimalFormat.format(azimuth) + ConstantsApp.DEGREE_ICON);
holder.tvPitch.setText(
mContext.getString(R.string.pitch) + ": " + decimalFormat.format(pitch) + ConstantsApp.DEGREE_ICON);
holder.tvRoll.setText(
mContext.getString(R.string.roll) + ": " + decimalFormat.format(roll) + ConstantsApp.DEGREE_ICON);
holder.tvBearing.setText(mContext.getString(R.string.bearing) + " " + bearing);
holder.tvDate.setText("Date" + ": " + measure.getFormattedDate());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
View view = null;
view = inflater.inflate(R.layout.custom_row_angle_photo, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
/**
* get an instance of OnRecyclerViewClickListener interface
*
* #param OnRecyclerViewMeasureClickListener
* callback that is used by adapter to invoke the method of the
* class implements the OnRecyclerViewClickListener interface
*/
public void setClickListener(OnRecyclerViewMeasureClickListener recyclerClickListener) {
this.recyclerClickListener = recyclerClickListener;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// Views
private TextView tvTitle, tvNote, tvAngle, tvAzimuth, tvPitch, tvRoll, tvBearing, tvDate;
public MyViewHolder(View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tvDisplayTitle);
tvAngle = (TextView) itemView.findViewById(R.id.tvDisplayAngle);
// Compass
tvAzimuth = (TextView) itemView.findViewById(R.id.tvDisplayAzimuth);
tvPitch = (TextView) itemView.findViewById(R.id.tvDisplayPitch);
tvRoll = (TextView) itemView.findViewById(R.id.tvDisplayRoll);
tvBearing = (TextView) itemView.findViewById(R.id.tvDisplayBearing);
tvNote = (TextView) itemView.findViewById(R.id.tvDisplayNote);
tvDate = (TextView) itemView.findViewById(R.id.tvDisplayDate);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (recyclerClickListener != null) {
recyclerClickListener.itemMeasureClicked(v, getLayoutPosition());
}
}
}
/**
* RecyclerViewClickListener interface helps user to set a clickListener to
* the RecyclerView. By setting this listener, any item of Recycler View can
* respond to any interaction.
*
* #author Fatih
*
*/
public interface OnRecyclerViewMeasureClickListener {
/**
* This is a callback method that be overriden by the class that
* implements this interface
*/
public void itemMeasureClicked(View view, int position);
}
}
Measure class only contains setters and getters for int and String values so i don't put it.
Layout for Activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutMainMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#android:color/background_light"
android:paddingBottom="50dp" >
<android.support.design.widget.AppBarLayout
android:id="#+id/appbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp" >
<ImageView
android:id="#+id/backgroundPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/bg_material" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#eeeeee"
android:paddingTop="12dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="80dp"
app:layout_anchor="#id/appbarPrevMeasure"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_save_white_36dp"
android:tint="#android:color/white"
app:backgroundTint="#FFA500" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabClearDB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
app:layout_anchor="#id/appbarPrevMeasure"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_delete_white_36dp"
android:tint="#android:color/white"
app:backgroundTint="#D463C3" />
</android.support.design.widget.CoordinatorLayout>
Layout for adapter rows with CardView
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#eeeeee"
cardview:cardCornerRadius="5dp"
cardview:cardElevation="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="#+id/tvDisplayTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textColor="#FF0000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvDisplayAngle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="18sp" />
<TextView
android:id="#+id/tvDisplayAzimuth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayBearing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvDisplayPitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="12dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="12dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
</LinearLayout>
<TextView
android:id="#+id/tvDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="#string/date_"
android:textColor="#9EA9AD"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text=""
android:textColor="#828A8C"
android:textSize="14sp" />
</LinearLayout>
How it looks
Does your RecyclerView show ImageView or TextView only?
If yes, because your ViewHolder Constructor is wrong. You have to call super(itemView) in your ViewHolder's Constructor, itemView is the view which is displayed as a row in RecyclerView. To fix your issue, I think you should change your code as below:
public ViewHolder createholder(ViewGroup parent, int viewtype) {
if (typeg.equals("image") || typeg.equals("text")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
return new ViewHolder(root, typeg.equals("text"));
}
//FIXME: As my expericence you should NOT return null for ViewHolder. You have to sure the typeg is one of "image" or "text". I think you should change typeg to Boolean variable to not return null ViewHolder.
return null;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgg;
public TextView txtg;
public CardView cardg;
public ViewHolder(View itemView, boolean isTypeText) {
super(itemView);
imgg = (ImageView) itemView.findViewById(R.id.Img);
cardg = (CardView) itemView.findViewById(R.id.card_view);
txtg = (TextView) itemView.findViewById(R.id.Txt);
imgg.setVisibility(isTypeText ? View.GONE : View.VISIBLE);
txtg.setVisibility(isTypeText ? View.VISIBLE : View.GONE);
}
}
Here is the proper Documentation Read and And Follow it.
You must be Doing Something Wrong in Gradle.!
so, the issue was Your XML was not showing CardView in it here we go.!
Add following Dependencies.!
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
Here is Your XML is Working Perfectly Fine.!
<?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.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:id="#+id/Img"
android:scaleType="center"
android:maxHeight="100dp"
android:maxWidth="150dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.25"
android:id="#+id/Txt"
android:layout_gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Now Clean and Rebuild Your Project.!
ok try this its Working Fine.now.!
Is it because you are removing card_view like so?
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
I'm using a CardView Layout in a RecyclerView, JSON will load data and fill the adapter. My layout includes a ImageView, a TextView and everything is fine.
This is the Adapter class (UPDATED):
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final int TYPE_HEADER = 4;
private static final int TYPE_ITEM = 1;
Context context;
List<CarData> getCarData; // getDataAdapter
ImageLoader imageThumbLoader;
public RecyclerViewAdapter(List<CarData> getCarData, Context context){
super();
this.getCarData = getCarData;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
// if (!isPositionHeader(position)) {
CarData getCarData1 = getCarData.get(position - 1);
imageThumbLoader = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageThumbLoader.get(getCarData1.getImageThumb(),
ImageLoader.getImageListener(
Viewholder.imageThumb,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.imageThumb.setImageUrl(getCarData1.getImageThumb(), imageThumbLoader);
Viewholder.titleName.setText(getCarData1.getTitleName());
Viewholder.doorName.setText(Html.fromHtml("<b>Số cửa:</b> " + getCarData1.getDoorName()));
Viewholder.seatName.setText(Html.fromHtml("<b>Số ghế:</b> " + getCarData1.getSeatName()));
Viewholder.cityName.setText(Html.fromHtml(getCarData1.getCityName()));
Viewholder.districtName.setText(Html.fromHtml(getCarData1.getDistrictName()));
// }
}
public int getDataAdapter() {
return getCarData == null ? 0 : getCarData.size();
}
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
#Override
public int getItemCount() {
return getDataAdapter(); // + 1
}
private boolean isPositionHeader(int position) {
return position == 0;
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView titleName;
public NetworkImageView imageThumb;
public TextView doorName;
public TextView seatName;
public TextView cityName;
public TextView districtName;
public ViewHolder(View itemView) {
super(itemView);
titleName = (TextView) itemView.findViewById(R.id.titleName);
imageThumb = (NetworkImageView) itemView.findViewById(R.id.imageThumb);
doorName = (TextView) itemView.findViewById(R.id.doorName);
seatName = (TextView) itemView.findViewById(R.id.seatName);
cityName = (TextView) itemView.findViewById(R.id.cityName);
districtName = (TextView) itemView.findViewById(R.id.districtName);
}
}
}
My layout:
<?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:id="#+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="3dp"
card_view:contentPadding="3dp"
card_view:cardCornerRadius="3dp"
card_view:cardMaxElevation="3dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/imageThumb"
android:layout_width="400dp"
android:layout_height="230dp"
android:src="#mipmap/ic_launcher"
android:gravity="top"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Image View"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageThumb"
android:layout_toEndOf="#+id/imageThumb"
android:layout_marginLeft="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/doorName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/seatName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/cityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/districtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
</LinearLayout>
<TextView
android:id="#+id/checkStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="KIỂM TRA TÌNH TRẠNG XE"
android:textColor="#color/color_primary_green_dark"
android:textStyle="bold"
android:textSize="12dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/textView_item32"
android:layout_toEndOf="#+id/textView_item32"
android:layout_marginLeft="20dp"/>
</LinearLayout>
But when I load data for the first time, there is a redundant item. Look at the following attachment:
The exception has happened when updated code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.michalz.hideonscrollexample, PID: 9794
java.lang.ArrayIndexOutOfBoundsException: length=18; index=-1
at java.util.ArrayList.get(ArrayList.java:310)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter$override.onBindViewHolder(RecyclerViewAdapter.java:54)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter$override.access$dispatch(RecyclerViewAdapter.java)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:0)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:20)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
How do I remove this redundant item in the CardView?
You have + 1 in your overridden getItemCount() method. It causes that you have one extra item in your list.
Then you have the method isPositionHeader, which you call in the onBindViewHolder method to find out, if the position is a header position. You are filling your cardview with data, if it is not the header position. But if it is the header position, you just skip it. So the first item, which is blank, is the header using the same cardview, as the other items, in fact.
Try to remove the + 1 and this condition if (!isPositionHeader(position)).
I have a Fragment Navigation Drawer with RecyclerView and some other view in it. Whenever I put my finger on a item on the recyclerview it doesn't show the selected state color. Although I've put every code needed.
RecyclerView Item Row:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
android:id="#+id/layout"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:id="#+id/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:id="#+id/title"/>
</LinearLayout>
RecyclerView Adapter:
List<NavDrawerItems> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
int selectedPosition = 0;
public NavigationDrawerAdapter(Context context, List<NavDrawerItems> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if(selectedPosition == position){
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef));
holder.title.setTextColor(context.getResources().getColor(R.color.dark_red));
holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red));
} else {
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white));
holder.title.setTextColor(context.getResources().getColor(R.color.black));
holder.icon.setColorFilter(context.getResources().getColor(R.color.black));
}
NavDrawerItems current = data.get(position);
holder.title.setText(current.getTitle());
holder.icon.setImageResource(current.getIcon());
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView icon;
LinearLayout layout;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon);
layout = (LinearLayout) itemView.findViewById(R.id.layout);
}
}
If I remove this code from the RecyclerView adapter, it works fine :
if(selectedPosition == position){
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef));
holder.title.setTextColor(context.getResources().getColor(R.color.dark_red));
holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red));
} else {
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white));
holder.title.setTextColor(context.getResources().getColor(R.color.black));
holder.icon.setColorFilter(context.getResources().getColor(R.color.black));
}
But this code is used to show the current selected item in recyclerview. How can I implement both of them.
I solved this issue by modifying the recyclerview item's code.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:id="#+id/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:id="#+id/title"/>
</LinearLayout>
</LinearLayout>
It seems, I was changing the background color of the Layout who was set with background :
android:background="?android:attr/selectableItemBackground"
So I added a new Layout and transfered the content inside it.
I have a recycler view. I have added an expandable view in the list item view. On a click it expands and show the layout. But now I want to close it on click of same item if its open.
Now if I click on 1st item the layout gets expanded and then if i click on 2nd item layout for 2nd item gets expanded and layout for 1st item gets closed.
I have followed this link :
RecyclerView expand/collapse items
Adapter:
public class BookingsAdapter extends RecyclerView.Adapter<BookingsAdapter.MyViewHolder> implements View.OnClickListener{
private int expandedPosition = -1;
private List<Bookings> bookingsList;
int status;
Context context;
public interface OnItemClickListener {
void onItemClick(Events item);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView eventName,eventType,eventDateTime,userName;
public RelativeLayout expandLayout;
public CardView cardView;
public MyViewHolder(View view) {
super(view);
eventName = (TextView) view.findViewById(R.id.text_eventName);
eventType = (TextView) view.findViewById(R.id.textView_EventType);
eventDateTime = (TextView) view.findViewById(R.id.text_dateTime);
userName = (TextView) view.findViewById(R.id.text_userName);
expandLayout = (RelativeLayout) view.findViewById(R.id.expandLayout);
cardView = (CardView) view.findViewById(R.id.card_view);
}
}
public BookingsAdapter(ArrayList<Bookings> bookingsList,Context context) {
this.bookingsList = bookingsList;
this.context = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bookings_card, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);
// Sets the click adapter for the entire cell
// to the one in this class.
holder.itemView.setOnClickListener(BookingsAdapter.this);
holder.itemView.setTag(holder);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Bookings bookings = bookingsList.get(position);
holder.eventName.setText(bookings.getEventName());
holder.eventType.setText(bookings.getEventType());
holder.eventDateTime.setText(bookings.getEventDateTime());
holder.userName.setText(bookings.getUserName());
if (position == expandedPosition) {
holder.expandLayout.setVisibility(View.VISIBLE);
} else {
holder.expandLayout.setVisibility(View.GONE);
}
}
#Override
public void onClick(View view) {
MyViewHolder holder = (MyViewHolder) view.getTag();
Bookings bookingsItem = bookingsList.get(holder.getPosition());
// Check for an expanded view, collapse if you find one
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
// Set the current position to "expanded"
expandedPosition = holder.getPosition();
notifyItemChanged(expandedPosition);
Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
}
#Override
public int getItemCount() {
return bookingsList.size();
}
}
Layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#android:color/white"
android:layout_marginTop="05dp"
android:layout_marginRight="05dp"
android:layout_marginLeft="05dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parentLayout">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="90dp"
android:id="#+id/detailsLayout">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="05dp"
android:layout_marginBottom="05dp"
android:id="#+id/eventLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event name"
android:id="#+id/text_eventName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="14sp"
android:textColor="#android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="("
android:id="#+id/textView26"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/textView_EventType"
android:textSize="12sp"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date and time"
android:id="#+id/text_dateTime"
android:layout_below="#+id/textView26"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="05dp"
android:textSize="12sp"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event type"
android:id="#+id/textView_EventType"
android:layout_below="#+id/text_eventName"
android:layout_toRightOf="#+id/textView26"
android:layout_toEndOf="#+id/textView26"
android:layout_marginTop="05dp"
android:textSize="12sp"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")"
android:id="#+id/textView29"
android:layout_alignTop="#+id/textView_EventType"
android:layout_toRightOf="#+id/textView_EventType"
android:layout_toEndOf="#+id/textView_EventType"
android:textSize="12sp"
android:textColor="#color/colorAccent" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/place_autocomplete_separator"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_alignParentTop="true">
</View>
</RelativeLayout>
<View
android:layout_width="3dp"
android:layout_height="wrap_content"
android:background="#color/grey">
</View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/eventLayout"
android:layout_toRightOf="#+id/eventLayout"
android:layout_toEndOf="#+id/eventLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/eventLayout"
android:id="#+id/profilepicLayout">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:src="#drawable/ic_person_black_48dp"
android:id="#+id/profileImage"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="false"
android:background="#drawable/circle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:id="#+id/text_userName"
android:layout_below="#+id/profileImage"
android:layout_marginTop="05dp"
android:textColor="#android:color/black"
android:textSize="12sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_below="#+id/detailsLayout"
android:id="#+id/expandLayout"
android:visibility="gone">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/place_autocomplete_separator"></View>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="#+id/imageView12"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/ic_call_black_18dp"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="05dp"
android:layout_marginBottom="05dp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="#+id/imageView13"
android:layout_alignTop="#+id/imageView12"
android:layout_centerHorizontal="true"
android:background="#drawable/ic_textsms_black_18dp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="#+id/imageView14"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:background="#drawable/ic_chat_black_18dp"
android:layout_centerVertical="true"
android:layout_alignTop="#+id/imageView13" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
EDIT: I have added boolean variable in class and did this. Noting happens. Layout dose not get close when I click on item
#Override
public void onClick(View view) {
MyViewHolder holder = (MyViewHolder) view.getTag();
Bookings bookingsItem = bookingsList.get(holder.getPosition());
// Check for an expanded view, collapse if you find one
// set previously expanded row to false
for(int i=0;i<bookingsList.size();i++)
{
if(bookingsList.get(i).expanded)
{
bookingsList.get(i).expanded = false;
}
}
//set current item expanded
bookingsList.get(holder.getPosition()).expanded = true;
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
// Set the current position to "expanded"
expandedPosition = holder.getPosition();
notifyItemChanged(expandedPosition);
Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
}
Now I want to close the expanded layout of same item on click of item.
NOTE: Take a boolean variable named expanded in class Bookings and by default save it as false where you are adding values to your list then in your on click do something like this
public class BookingsAdapter extends RecyclerView.Adapter<BookingsAdapter.MyViewHolder> implements View.OnClickListener{
private List<Bookings> bookingsList;
int status;
Context context;
public interface OnItemClickListener {
void onItemClick(Events item);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView eventName,eventType,eventDateTime,userName;
public RelativeLayout expandLayout;
public CardView cardView;
public MyViewHolder(View view) {
super(view);
eventName = (TextView) view.findViewById(R.id.text_eventName);
eventType = (TextView) view.findViewById(R.id.textView_EventType);
eventDateTime = (TextView) view.findViewById(R.id.text_dateTime);
userName = (TextView) view.findViewById(R.id.text_userName);
expandLayout = (RelativeLayout) view.findViewById(R.id.expandLayout);
cardView = (CardView) view.findViewById(R.id.card_view);
}
}
public BookingsAdapter(ArrayList<Bookings> bookingsList,Context context) {
this.bookingsList = bookingsList;
this.context = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bookings_card, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);
// Sets the click adapter for the entire cell
// to the one in this class.
holder.itemView.setOnClickListener(BookingsAdapter.this);
holder.itemView.setTag(holder);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Bookings bookings = bookingsList.get(position);
holder.eventName.setText(bookings.getEventName());
holder.eventType.setText(bookings.getEventType());
holder.eventDateTime.setText(bookings.getEventDateTime());
holder.userName.setText(bookings.getUserName());
if (bookings.expanded) {
holder.expandLayout.setVisibility(View.VISIBLE);
} else {
holder.expandLayout.setVisibility(View.GONE);
}
}
#Override
public void onClick(View view) {
MyViewHolder holder = (MyViewHolder) view.getTag();
if(bookingsList.get(holder.getPosition()).expandad)
{
bookingsList.get(holder.getPosition()).expandad=false;
notifyDataSetChanged();
}
else{
// set previously expanded row to false
for(int i=0;i<bookingsList.size();i++)
{
if(bookingsList.get(i).expanded)
{
bookingsList.get(i).expandad=false;
}
}
//set current item expanded
bookingsList.get(holder.getPosition()).expandad=true;
notifyDataSetChanged();
}
Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
}