Fragment Drawer RecyclerView Item not getting Ripple Effect - android

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.

Related

how to remove footerview from recyclerview when don't need load more item?

you can check my code as below:
private class ContentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_ITEM = 0;
public static final int TYPE_FOOTER = 1;
private ArrayList<BookSearchModel.BookSearchVO> itemList = null;
public ContentListAdapter(ArrayList<BookSearchModel.BookSearchVO> item) {
itemList = item;
}
#Override
public int getItemViewType(int position) {
if(position >= itemList.size()) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_ITEM)
{
LayoutInflater inflater = (LayoutInflater)wrapperContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = inflater.inflate(R.layout.lib_book_search_item, parent, false);
return new BookInfoHolder(item);
}
else
{
LayoutInflater inflater = (LayoutInflater)wrapperContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = inflater.inflate(R.layout.list_footer, parent, false);
return new FooterViewHolder(item);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder wrappedHolder, int position) {
if(wrappedHolder instanceof BookInfoHolder) {
SOME OF VIEW BIND PROCESS....
} else if(wrappedHolder instanceof FooterViewHolder) {
// do nothing.
}
}
#Override
public int getItemCount() {
return itemList.size() + 1;
}
public class BookInfoHolder extends RecyclerView.ViewHolder {
private ImageView bookCover;
private TextView bookTitle;
private TextView bookAuthor;
private TextView bookPublisher;
private TextView bookLocation;
public BookInfoHolder(View itemView) {
super(itemView);
bookCover = (ImageView) itemView.findViewById(R.id.book_cover);
bookTitle = (TextView) itemView.findViewById(R.id.book_title);
bookAuthor = (TextView) itemView.findViewById(R.id.book_author_info);
bookPublisher = (TextView) itemView.findViewById(R.id.book_publisher);
bookLocation = (TextView) itemView.findViewById(R.id.book_location);
}
}
public class FooterViewHolder extends RecyclerView.ViewHolder {
private View v;
public FooterViewHolder(View itemView) {
super(itemView);
this.v = itemView;
}
}
}
i tried RecyclerView.removeViewAt(position)
it casues null pointer exception, i guess recycler's child view not similar to listview.
and, i tried to use layoutmanager, but i have no idea for layoutmanager control childView(or item).
and noted this: Remove RecyclerView FooterView
but, any other doesn't work for me.
if load all of item, how can i remove footerView?
EDIT:
what i see
what i want
lib_book_search_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="135dp"
android:id="#+id/book_cover"
android:transitionName="#string/book_cover_transition"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:textStyle="bold"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:gravity="center_vertical|left"
android:maxLines="2"
android:ellipsize="end"
android:id="#+id/book_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#929292"
android:textStyle="bold"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:gravity="center_vertical|left"
android:maxLines="2"
android:ellipsize="end"
android:id="#+id/book_author_info"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#929292"
android:textStyle="bold"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:gravity="center_vertical|left"
android:maxLines="2"
android:ellipsize="end"
android:layout_marginTop="2dp"
android:id="#+id/book_publisher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#01A997"
android:textStyle="bold"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:gravity="center_vertical|left"
android:maxLines="2"
android:ellipsize="end"
android:layout_marginTop="2dp"
android:id="#+id/book_location"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#3C7FED"
android:textStyle="bold"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:gravity="center_vertical|left"
android:maxLines="2"
android:ellipsize="end"
android:layout_marginTop="2dp"
android:id="#+id/book_status"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#CCCCCC"/>
</LinearLayout>
list_footer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="center"
android:orientation="horizontal">
<ProgressBar
android:layout_width="24dp"
android:layout_height="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/msg_loading_data"
android:gravity="center"
android:layout_marginLeft="10dp"/>
</LinearLayout>
You have to pass the max_items of your list.
max_items = Total items after the list is fully formed after all items are added.
public ContentListAdapter(ArrayList<BookSearchModel.BookSearchVO> item, int max_items) {
itemList = item;
this.max_items = max_items;
}
Then in your onBindViewHolder do this checking.
else if(wrappedHolder instanceof FooterViewHolder) {
// do nothing.
if(position == max_items){
wrappedHolder.rootLayout.setVisibility(View.GONE); // this will remove the footer..
}
}

How to remove redundancy item in CardView layout

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)).

How to close an expanded list item on second click if it's expanded?

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();
}

Removing a cardview item inside a recycler view if it is null

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;
}
}

How to add an editText for every imageView in recyclerView and save it to PARSE?

So in my recyclerview, i have a CARDVIEW which consists of
ImageView, EditText and a button. and when i click the button, it will send the text from EditText to PARSE.com. My problem is that, i cannot link that text to it's ImageView
i want when i open the app, i can see the imageView and at the bottom there will be a description for the image. i want the editText to be dynamic. Can Someone guide me?
this is the XML layout:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card_view"
android:layout_margin="10dp"
android:layout_marginBottom="30dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
<ImageView
android:id="#+id/imageViewRecycler"
android:background="#color/border"
android:layout_width="match_parent"
android:layout_height="380dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:contentDescription="TODO"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/separator"
android:id="#+id/separator"
android:layout_above="#+id/firstLine"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey">
<EditText
android:id="#+id/firstLineText"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignWithParentIfMissing="true"
android:gravity="top"
android:background="#color/colorPrimary"
android:textSize="16sp"
android:textColor="#color/colorAccent"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/linearLayout"
android:layout_alignParentTop="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save"
android:id="#+id/firstLineButton"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
this is my recyclerview adapter:
public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {
LayoutInflater inflater;
List<imageDescription> data= Collections.emptyList();
private Context context;
ViewHolder holder;
public myAdapter(Context context, List<imageDescription> data){
this.context=context;
this.data=data;
}
#Override
public myAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.from(context).inflate(R.layout.custom_layout, parent, false);
holder= new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(myAdapter.ViewHolder holder, int position) {
imageDescription current= data.get(position);
Glide.with(context)
.load(current.url)
.centerCrop()
.override(500,500)
.placeholder(R.drawable.whitebackground)
.into(holder.imageViewRecycler);
holder.firstLineText.setText(current.firstLine);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageViewRecycler;
EditText firstLineText;
Button firstLineButton;
public ViewHolder(final View itemView) {
super(itemView);
imageViewRecycler=(ImageView)itemView.findViewById(R.id.imageViewRecycler);
firstLineText=(EditText)itemView.findViewById(R.id.firstLineText);
firstLineButton=(Button)itemView.findViewById(R.id.firstLineButton);
firstLineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseObject obj= new ParseObject("newMessage");
obj.put("imageDetail",firstLineText.getText());
obj.saveInBackground();
}
});
}
}
}

Categories

Resources