Hello everyone i am facing a problem with recycle view.
im using a recycle view with swipe to change item position functionality where user can change the position of items by long press items.
all items comes dynamical with different types of views.
everything is working good but when im trying to scroll the view then items displaying in recycle view are overlap each other.
i search on google and got the solution that is override the method .
#Override
public int getItemViewType(int position) {
return position;
}
when im using this method in my adapter class then my swipe to change view functionality stop working.
can anyone explain my why it's happening..?
Here is whole adapter class
public class CreateEmailAdapter extends RecyclerView.Adapter<CreateEmailAdapter.MyViewHolder> implements ItemTouchHelperAdapter {
private static final String TAG = "CreateEmailAdapter";
public int position = 0;
public ArrayList<CampaignData> List;
private final OnStartDragListener mDragStartListener;
public CreateEmailAdapter(ArrayList<CampaignData> List, OnStartDragListener mDragStartListener) {
this.List = List;
this.mDragStartListener = mDragStartListener;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView imageView, imageView2, imageView3;
RelativeLayout MainListView;
public MyViewHolder(final View view) {
super(view);
parentLayout = (RelativeLayout) view.findViewById(R.id.parentLayout);
MainListView = (RelativeLayout) view.findViewById(R.id.MainListView);
title = (TextView) view.findViewById(R.id.MainTitle);
imageView2 = (ImageView) view.findViewById(R.id.imageViewPopup);
imageView3 = (ImageView) view.findViewById(R.id.movearrows);
imageView = (ImageView) view.findViewById(R.id.lytPatternColorDraw);
parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
position = getAdapterPosition();
HandleResponse(title.getText().toString(), view1, getAdapterPosition());
}
});
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
return new MyViewHolder(mView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
CampaignData campaignData = this.List.get(position);
holder.parentLayout.setTag(campaignData);
String extraData = campaignData.getExtraData();
String MediaType = campaignData.getMediaType();
holder.MainListView.setVisibility(View.VISIBLE);
holder.title.setText(campaignData.getTitle());
holder.imageView.setImageResource(campaignData.getimage());
holder.imageView2.setTag(campaignData);
holder.imageView3.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragStartListener.onStartDrag(holder);
}
return false;
}
});
}
#Override
public boolean onItemMove(int fromPosition, int toPosition) {
Collections.swap(List, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
#Override
public void onItemDismiss(int position) {
List.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
return List.size();
}
// #Override
// public int getItemViewType(int position) {
// return position;
// }
public int getPosition() {
return position;
}
}
XMl file list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<RelativeLayout
android:id="#+id/MainListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/lytGrab"
android:visibility="visible">
<LinearLayout
android:id="#+id/lytPatternColor"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<ImageView
android:id="#+id/movearrows"
android:layout_width="18dp"
android:layout_height="36dp"
android:layout_marginRight="10dp"
android:background="#drawable/movearrows" />
<ImageView
android:id="#+id/lytPatternColorDraw"
android:layout_width="30dp"
android:layout_height="40dp"
android:orientation="vertical" />
</LinearLayout>
<TextView
android:id="#+id/MainTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/lytPatternColor"
android:gravity="center_vertical"
android:padding="10dp"
android:singleLine="true"
android:text="Title Here "
android:textColor="#000"
android:textSize="16sp" />
</RelativeLayout>
<!--Copy images -->
<LinearLayout
android:id="#+id/lytGrab"
android:layout_width="30dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/three" />
</LinearLayout>
</RelativeLayout>
Related
I have 2 recycler views and a view pager in a fragment.
Everything else works fine but both recycler view items are showing a random mark on the upper left corner of every items. And same thing is happening for the view pager items.
This fragment is in a fragment activity and view pager is using pager adapter. And I am using Picasso library to load images on the items and also using network policy to cache the images in disk.
recycler view items appearing like this
view pager items appearing like this
Recycler View adapter
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseListViewHolder> {
public static final int HOME_PAGE = 1;
public static final int DISPLAY_COURSE = 2;
private Picasso mPicasso;
private List<DisplayCourse> courseList;
private static final String TAG = "CourseListAdapter";
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position, View view);
}
private String mSender;
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public CourseListAdapter(List<DisplayCourse> courseList, String sender) {
this.courseList = courseList;
this.mSender = sender;
mPicasso = Picasso.get();
}
#NonNull
#Override
public CourseListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case 1:
View view1 = inflater.inflate(R.layout.recom_course_home_layout, parent, false);
return new CourseListViewHolder(view1, mListener);
case 2:
View view2 = inflater.inflate(R.layout.display_course_layout, parent, false);
return new CourseListViewHolder(view2, mListener);
default:
View view3 = inflater.inflate(R.layout.section_video_item_layout, parent, false);
return new CourseListViewHolder(view3, mListener);
}
}
#Override
public void onBindViewHolder(#NonNull CourseListViewHolder holder, int position) {
holder.title.setText(courseList.get(position).getCourseTitle());
mPicasso.load(courseList.get(position).getThumbnailURL())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
mPicasso.load(courseList.get(position).getThumbnailURL())
.error(R.drawable.ofklogo)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
}
#Override
public int getItemCount() {
return courseList.size();
}
public static class CourseListViewHolder extends RecyclerView.ViewHolder {
ImageView thumbnail;
TextView title;
public CourseListViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
title = itemView.findViewById(R.id.courseTitle);
thumbnail = itemView.findViewById(R.id.courseThumbNailImageView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null) {
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, view);
}
}
}
});
}
}
#Override
public int getItemViewType(int position) {
if (mSender.equals("home_page")) {
return HOME_PAGE;
} else if (mSender.equals("displayCourse")) {
return DISPLAY_COURSE;
}
return -1;
}
}
View pager adapter
public class VideoSliderAdapter extends PagerAdapter {
private boolean doNotifyDataSetChangedOnce = false;
private static final String TAG = "VideoSliderAdapter";
private YouTubePlayerView youTubePlayerView;
private View gradientView;
private ImageView thumbNail;
private LinearLayout layout;
private Picasso picasso;
private List<Video> videoList;
private Context mContext;
private Lifecycle mLifeCycle;
public VideoSliderAdapter(List<Video> videoList, Context context, Lifecycle lifecycle) {
this.videoList = videoList;
this.mContext = context;
this.mLifeCycle = lifecycle;
doNotifyDataSetChangedOnce = true;
picasso = Picasso.get();
}
#Override
public int getCount() {
if (doNotifyDataSetChangedOnce) {
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
}
return videoList.size();
}
#Override
public int getItemPosition(#NonNull Object object) {
return POSITION_NONE;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return (view == (CardView) object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.video_paly_layout, container, false);
TextView title = view.findViewById(R.id.videoTitle);
title.setText(videoList.get(position).getVideoTitle());
gradientView = view.findViewById(R.id.gradientView);
thumbNail = view.findViewById(R.id.videoThumbNail);
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
picasso.load(videoList.get(position).getVideoThumbNail())
.error(R.drawable.ofklogo)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
layout = view.findViewById(R.id.videoPlayLayout);
youTubePlayerView = view.findViewById(R.id.youtube_player_view);
mLifeCycle.addObserver(youTubePlayerView);
new AddListener(youTubePlayerView, position).execute();
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((CardView) object);
}
Recycler view holder layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/courseThumbNailImageView"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/art_thumb" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/courseThumbNailImageView">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="2"
android:text="Course title"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
view pager layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:autoPlay="false" />
<ImageView
android:id="#+id/videoThumbNail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<View
android:id="#+id/gradientView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_drawable" />
<LinearLayout
android:id="#+id/videoPlayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="8dp"
android:src="#drawable/play_button" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="Course title"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
When loading image using Picasso use: setIndicatorsEnabled(false)
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.setIndicatorsEnabled(false)
.into(...);
The colors indicate this:
Green: Image is fetched from memory
Blue: Image is fetched from disk
Red: Image is fetched from network
I design a layout and I get a problem with ScrollView. Specially, I have a ScrollView and a RecyclerView inside this ScrollView. When I select items in the last row of RecyclerView, the View does not stay at the last row, it moves up to the upper row.
For example, my view has 4 rows. When I select an item in 4th row, the view automatically moves up to 3th row. So, how I can prevent it so that when I select items in the last row, the view will stay and not move up? Thank you
Layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/selectPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/select_payment_method"
android:textSize="#dimen/sp_20"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/choosePayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="#dimen/sp_16"
android:text="#string/choose_payment_method"
app:layout_constraintStart_toStartOf="#+id/selectPayment"
app:layout_constraintTop_toBottomOf="#+id/selectPayment" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/dp_30"
android:layout_marginBottom="10dp"
android:fadeScrollbars="false"
app:layout_constraintBottom_toTopOf="#+id/continueButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/choosePayment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/paymentMethodRV"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="#+id/continueButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/continue_in_cap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter:
public class PaymentAdapter extends RecyclerView.Adapter<PaymentAdapter.ViewHolder> {
private Context context;
private List<CardItems> cardItemsList;
private int rowIndex = -1;
public PaymentAdapter(Context context, List<CardItems> cardItemsList) {
this.context = context;
this.cardItemsList = cardItemsList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.payment_method_items, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.paymentImage.setImageResource(cardItemsList.get(position).getCardImage());
holder.paymentTitle.setText(cardItemsList.get(position).getCardNumber());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(int position) {
rowIndex = position;
notifyDataSetChanged();
}
});
//set highlight color when option is clicked
if (rowIndex == position){
holder.frameContainer.setBackgroundResource(R.drawable.green_rounded_button);
}
else {
holder.frameContainer.setBackgroundResource(R.drawable.white_grey_rounded_button);
}
}
#Override
public int getItemCount() {
return cardItemsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView paymentImage;
TextView paymentTitle;
FrameLayout frameContainer;
ItemClickListener itemClickListener;
public ViewHolder(#NonNull View itemView) {
super(itemView);
paymentImage = itemView.findViewById(R.id.paymentImage);
paymentTitle = itemView.findViewById(R.id.paymentTitle);
frameContainer = itemView.findViewById(R.id.frameContainer);
itemView.setOnClickListener(this);
}
//handle click on each category
#Override
public void onClick(View view) {
itemClickListener.onItemClick(getAdapterPosition());
notifyDataSetChanged();
}
public void setItemClickListener(ItemClickListener listener){
itemClickListener = listener;
}
}
//for listening from holder
public interface ItemClickListener{
void onItemClick(int position);
}
}
RecyclerView Adapter Class.
public class TravelListAdapter extends RecyclerView.Adapter<TravelListAdapter.ViewHolder> {
Context mContext;
OnItemClickListener mItemClickListener;
String []names = {"Hotels", "Travel", "Medicine", "Education", "Travel", "Hotels"};
private int[] advertImageList = {R.drawable.hotel, R.drawable.travel, R.drawable.medical, R.drawable.education, R.drawable.travel, R.drawable.hotel};
// 2
public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public TravelListAdapter(Context context) {
this.mContext = context;
}
// 3
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public LinearLayout placeHolder;
public LinearLayout placeNameHolder;
public TextView placeName;
public ImageView placeImage;
public ViewHolder(View itemView) {
super(itemView);
placeHolder = itemView.findViewById(R.id.mainHolder);
placeName = itemView.findViewById(R.id.placeName);
placeNameHolder = itemView.findViewById(R.id.placeNameHolder);
placeImage = itemView.findViewById(R.id.placeImage);
placeHolder.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(itemView, getPosition());
}
}
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_row_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// final Place place = new PlaceData().placeList().get(position);
holder.placeName.setText(names[position]);
holder.placeName.setTextColor(R.color.black);
Picasso.with(mContext).load(advertImageList[position]).into(holder.placeImage);
Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image2);
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
//int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.transparent));
holder.placeNameHolder.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
}
});
}
#Override
public int getItemCount() {
return names.length;
}
}`
My Xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/placeCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardBackgroundColor="#android:color/transparent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/placeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:transitionName="tImage"
android:layout_centerInParent="true"
android:tint="#android:color/white"
android:padding="10dp"/>
<!-- Used for the ripple effect on touch -->
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/placeNameHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:transitionName="tNameHolder"
android:padding="5dp"
android:layout_below="#+id/placeImage">
<TextView
android:id="#+id/placeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#android:color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
I am using vector images generated from Android Studio but those images are not displayed in recyclerView.
What should i do? Let me mention that the vector images that i use inside recyclerView are properly displayed if i use them in simple image views.
Based on my previous question , I have a problem with onClick on CardView in my RecyclerView. I need to change the card layout when the CardView is clicked with another card layout xml. I follow this and this this but its not work. I dont too understand how to declare one CardView layout again in my code. Here my Code :
Adapter.Java
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int view1 = 0;
public static final int view2 = 1;
private static OnRecyclerViewItemClickedListener recyclerViewItemClickedListener;
public void setOnRecyclerViewClickedListener (OnRecyclerViewItemClickedListener l) {
recyclerViewItemClickedListener = l;
}
public interface OnRecyclerViewItemClickedListener {
void OnRecyclerViewItemClicked(int position);
void OnRecyclerViewItemBind(ViewHolder holder, int position);
int OnRecyclerViewItemCount();
}
public Adapter() {
super();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == view1) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_empty, parent, false);
return new ViewHolder(itemView);
}
else if (viewType == view2) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_occupied, parent, false);
return new ViewHolder2(itemView);
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
recyclerViewItemClickedListener.OnRecyclerViewItemBind((ViewHolder) holder, position);
}
#Override
public int getItemCount() {
return recyclerViewItemClickedListener.OnRecyclerViewItemCount();
}
public int getItemViewType() { return recyclerViewItemClickedListener.getItemViewType(); }
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table;
public TextView txt_no_table_occ;
public TextView txt_pax_occ;
public TextView txt_guest_name_occ;
public TextView txt_bill_occ;
public Chronometer txt_time_occ;
public CardView cardview_table;
public LinearLayout title_layout;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
txt_no_table_occ = (TextView) itemView.findViewById(R.id.txt_no_table);
txt_pax_occ = (TextView) itemView.findViewById(R.id.txt_pax);
txt_guest_name_occ = (TextView)itemView.findViewById(R.id.txt_guestname);
txt_bill_occ = (TextView) itemView.findViewById(R.id.txt_bill);
txt_time_occ = (Chronometer) itemView.findViewById(R.id.txt_time);
cardview_table = (CardView) itemView.findViewById(R.id.table_card_empty);
title_layout = (LinearLayout) itemView.findViewById(R.id.table_ll1);
}
#Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
}
}
public class ViewHolder2 extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table2;
public TextView txt_pax2;
public TextView txt_guest_name2;
public TextView txt_bill2;
public TextView txt_time2;
public ViewHolder2 (View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table2 = (TextView) itemView.findViewById(R.id.txt_no_table_);
txt_pax2 = (TextView) itemView.findViewById(R.id.txt_pax_);
txt_guest_name2 = (TextView)itemView.findViewById(R.id.txt_guestname_);
txt_bill2 = (TextView) itemView.findViewById(R.id.txt_bill_);
txt_time2 = (TextView) itemView.findViewById(R.id.txt_time_);
}
#Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
}
}
}
Here My Activity :
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
#Override
public void OnRecyclerViewItemClicked(int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
if (currTable.has("selected")) {
currTable.put("selected", !currTable.getBoolean("selected"));
} else {
currTable.put("selected",true);
}
adapter.notifyItemChanged(position);
} catch (JSONException e) {
e.printStackTrace();
}
try
{
Toast.makeText(TableActivity.this, filteredTableList.getJSONObject(position).getString("tischnr"), Toast.LENGTH_SHORT).show();
}
catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void OnRecyclerViewItemBind(Adapter.ViewHolder holder, int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
holder.txt_no_table.setText(currTable.getString("tischnr"));
holder.txt_guest_name_occ.setText("");
holder.txt_pax_occ.setText("");
holder.txt_bill_occ.setText("");
int queasy33Index = ProgramMethod.getJSONArrayIndex(queasy33List,"number2", currTable.getInt("tischnr"));
if (queasy33Index >= 0) {
holder.txt_guest_name_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("char2"));
holder.txt_pax_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("number3"));
}
if (currTable.has("selected") && currTable.getBoolean("selected")) {
holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
holder.txt_no_table.setVisibility(View.INVISIBLE);
holder.txt_no_table_occ.setVisibility(View.VISIBLE);
holder.txt_bill_occ.setVisibility(View.VISIBLE);
holder.txt_pax_occ.setVisibility(View.VISIBLE);
holder.txt_time_occ.setVisibility(View.VISIBLE);
holder.txt_guest_name_occ.setVisibility(View.VISIBLE);
} else {
holder.itemView.setBackgroundResource(R.color.colorTableGreen);
holder.txt_no_table.setVisibility(View.VISIBLE);
holder.txt_no_table_occ.setVisibility(View.INVISIBLE);
holder.txt_bill_occ.setVisibility(View.INVISIBLE);
holder.txt_pax_occ.setVisibility(View.INVISIBLE);
holder.txt_time_occ.setVisibility(View.INVISIBLE);
holder.txt_guest_name_occ.setVisibility(View.INVISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public int OnRecyclerViewItemCount() {
return filteredTableList.length();
}
});
My table_item_empty.xml (first layout) :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/table_card_empty"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/colorTableGreen"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
android:layout_margin="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:gravity="center">
<RelativeLayout
android:id="#+id/table_rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/table_ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txt_no_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="15dp"
android:gravity="center"
android:textColor="#color/colorWhite"
android:visibility="invisible"/>
</LinearLayout>
<TextView
android:id="#+id/txt_pax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:text="5"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_airline_seat_recline_normal_white_18dp"
android:visibility="invisible"/>
<TextView
android:id="#+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:00"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_access_time_white_18dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_pax"
android:layout_marginStart="2dp"
android:visibility="invisible"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/table_rl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/table_rl1">
<TextView
android:id="#+id/txt_guestname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:text="Budi"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_supervisor_account_white_18dp"
android:visibility="invisible"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/table_rl2">
<TextView
android:id="#+id/txt_bill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10000"
android:drawableLeft="#drawable/ic_attach_money_white_18dp"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:layout_marginStart="1dp"
android:visibility="invisible"/>
</RelativeLayout>
<TextView
android:id="#+id/txt_no_table_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12"
android:textSize="25dp"
android:gravity="center_vertical|center_horizontal"
android:textColor="#color/colorWhite"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
App looks like :
My code make a first layout gone when i clicked but i want to show/change another card layout.
This is CardView1 Looks like :
Another CardView xml (CardView2) :
EDITED :
I trick it. Now when one or more item clicked, it will change the view into like this :
But i still want to try with two different layout.xml view when item clicked. Its work because i only 'play' with 'INVISIBLE' and 'VISIBLE' in element on one layout, not the xml.
[EDITED] :
I'm still try to make my RecyclerView with two Layout view. I improve my Adapter like above. I follow this but i get a litle problem. In my Acivity, i call my itemView from ViewHolder1 with holder (example: holder.txt_time....) like my code above. I want to know how i can call my second holder (ViewHolder2) in my Activity? I try to make hard code with add some listener but its not work for me :(
Maybe somebody can help and guide me to fix my code. Every answer will helpful for me. Thanks before.
I've a TextView inside a CardView. Now the problem is when I use setText method on the TextView it's giving me nullPointerException. According to my tests textViewAuthor in the below code is null even after initializing it. Weird part is textViewName which is initialized the same way is not null. Why is it staying null even after assigning a view?
public class EBooksRecyclerViewAdapter extends RecyclerView.Adapter<EBooksRecyclerViewAdapter.ViewHolder> {
private String[] ebookName, ebookAuthor;
private OnItemClickListener mItemClickListener;
public EBooksRecyclerViewAdapter(String[] ebookName, String[] ebookAuthor) {
this.ebookName = ebookName;
this.ebookAuthor = ebookAuthor;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView textViewName, textViewAuthor;
private CardView cardView;
public ViewHolder(View v) {
super(v);
textViewName = (TextView)v.findViewById(R.id.textViewName); //not null
textViewAuthor = (TextView)v.findViewById(R.id.textViewAuthor); //NULL !!
if(textViewName == null)
Log.e("textViewAuthor","null");
if(textViewAuthor == null)
Log.e("textViewAuthor","null");
cardView = (CardView)v.findViewById(R.id.card_view);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mItemClickListener.onItemClick(textViewName.getText().toString());
}
});
}
}
public void setOnItemClickListener(OnItemClickListener mItemClickListener){
this.mItemClickListener = mItemClickListener;
}
public interface OnItemClickListener {
public void onItemClick(String eventName);
}
#Override
public EBooksRecyclerViewAdapter.ViewHolder onCreateViewHolder(
ViewGroup parent,int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.event_card_view, parent, false);
return new ViewHolder(v);
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.e("NAME", ebookName[position]); //not null
Log.e("AUTHOR", ebookAuthor[position]); //not null
holder.textViewName.setText(ebookName[position]);
holder.textViewAuthor.setText(ebookAuthor[position]); //nullPointerException
}
#Override
public int getItemCount() {
return ebookName.length;
}
}
Here's my layout code:
<?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:weightSum="1">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clickable="true"
android:layout_weight="0.25">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp">
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_margin="2dp" />
<TextView
android:id="#+id/textViewAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="2dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>