ScrollView doesn't work in Android - android

I have a fragment that contains a view pager, a recycler view, etc.
I want to place all of that inside a scroll view , but it just refuses to scroll. Am I missing something here?
I've tried setting fillViewport false, and removing the scrollbars:none, and a couple of other things but nothing helps..
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abc"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_below="#id/headerLayout"
android:id="#+id/recyclerView"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:layout_below="#id/recyclerView"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v4.view.ViewPager>
</RelativeLayout>
</ScrollView>

As I responded in comment, check the adapter class of RecyclerView. I won't be explaining it as it is too big to explain. But for starters follow the link Recyclerview and handling different type of row inflation
For the code below you can relate it with StyFi app from google store
public class FeedRecyclerStickyHeaderAdapter<T> extends RecyclerStickyTypeWrapper<T>
implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {
private static final String TAG = FeedRecyclerStickyHeaderAdapter.class.getSimpleName();
public static final int VIEW_LOOK = 0;
public static final int VIEW_PRODUCT = 1;
public static final int VIEW_BLOG = 2;
public static final int VIEW_BRAND = 3;
public static final int VIEW_PROGRESS = 4;
public static final int VIEW_AD = 5;
public static final int VIEW_NEW_ARRIVALS = 6;
public static final int VIEW_FEATURED_BRANDS = 7;
public static final int VIEW_BIG_BANNER = 8;
public static final int VIEW_BRAND_FEED = 9;
private static final int TOTAL_VIEWS = 10;
private OnInteractionListener listener;
private OnButtonClickListener callback;
private Fragment fragment;
private Handler handler;
private TimerTask task;
private ArrayList<RecyclerView.ViewHolder> holderArrayList;
public FeedRecyclerStickyHeaderAdapter(Activity context, #NonNull List<T> objects, #NonNull OnInteractionListener listener) {
super();
this.context = context;
this.objects = objects;
this.listener = listener;
holderArrayList = new ArrayList<>(objects.size());
}
public FeedRecyclerStickyHeaderAdapter(Activity context, #NonNull List<T> objects, Fragment fragment,
#NonNull OnInteractionListener listener) {
super();
this.context = context;
this.objects = objects;
this.fragment = fragment;
this.listener = listener;
}
/**
* Used for feeds for adding products to wishlist
*
* #param context
* #param objects
* #param fragment
* #param listener
* #param callback
*/
public FeedRecyclerStickyHeaderAdapter(Activity context, #NonNull List<T> objects, Fragment fragment,
#NonNull OnInteractionListener listener, #NonNull OnButtonClickListener callback) {
super();
this.context = context;
this.objects = objects;
this.fragment = fragment;
this.listener = listener;
this.callback = callback;
}
#Override
public int getItemViewType(int position) {
if (position > -1 && position < objects.size()) {
if (objects.get(position) instanceof Feed) {
switch (((Feed) objects.get(position)).getViewType()) {
case Feed.LOOK_VIEW:
return VIEW_LOOK;
case Feed.BLOG_VIEW:
return VIEW_BLOG;
case Feed.PRODUCT_VIEW:
return VIEW_PRODUCT;
case Feed.PROGRESS_VIEW:
return VIEW_PROGRESS;
case Feed.FEATURED_BRANDS_VIEW:
return VIEW_FEATURED_BRANDS;
case Feed.NEW_ARRIVAL_VIEW:
return VIEW_NEW_ARRIVALS;
case Feed.AD_VIEW:
return VIEW_AD;
case Feed.BRAND_VIEW:
return VIEW_BRAND;
}
} else if (objects.get(position) instanceof BrandFeed) {
switch (((BrandFeed) objects.get(position)).getViewType()) {
case BrandFeed.AD_VIEW:
return VIEW_AD;
case BrandFeed.BIG_BANNER_VIEW:
return VIEW_BIG_BANNER;
case BrandFeed.BRAND_FEED_VIEW:
return VIEW_BRAND_FEED;
case BrandFeed.PROGRESS_VIEW:
return VIEW_PROGRESS;
}
} else if (objects.get(position) instanceof Looks) {
if (null != ((Looks) objects.get(position)).getLookId())
return VIEW_LOOK;
else
return VIEW_PROGRESS;
} else if (null == objects.get(position))
return VIEW_PROGRESS;
}
return super.getItemViewType(position);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType) {
case VIEW_LOOK: {
ViewHolderLook holder = new ViewHolderLook(LayoutInflater.from(context)
.inflate(R.layout.listitem_look, parent, false));
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
initializeHeaders(holder.view, holder);
}
return holder;
}
case VIEW_PRODUCT: {
ViewHolderProduct holder = new ViewHolderProduct(LayoutInflater.from(context)
.inflate(R.layout.listitem_new_product, parent, false));
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
initializeHeaders(holder.view, holder);
}
return holder;
}
case VIEW_BLOG: {
ViewHolderBlog holder = new ViewHolderBlog(LayoutInflater.from(context)
.inflate(R.layout.blog_list_item_new, parent, false));
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
initializeHeaders(holder.view, holder);
}
return holder;
}
case VIEW_BRAND: {
ViewHolderBrands holder = new ViewHolderBrands(LayoutInflater.from(context)
.inflate(R.layout.listitem_feed_brand, parent, false));
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
initializeHeaders(holder.view, holder);
}
return holder;
}
case VIEW_PROGRESS: {
v = LayoutInflater.from(context).inflate(R.layout.dialog_progress, parent, false);
return new ViewHolderProgress(v);
}
case VIEW_AD: {
v = LayoutInflater.from(context).inflate(R.layout.listitem_offers, parent, false);
return new ViewHolderAd(v);
}
case VIEW_NEW_ARRIVALS: {
v = LayoutInflater.from(context).inflate(R.layout.listitem_new_arrivals, parent, false);
return new ViewHolderNewArrivals(v);
}
case VIEW_FEATURED_BRANDS: {
v = LayoutInflater.from(context).inflate(R.layout.listitem_featured_brands, parent, false);
return new ViewHolderFeaturedBrands(v);
}
case VIEW_BRAND_FEED: {
v = LayoutInflater.from(context).inflate(R.layout.listitem_brandfeed, parent, false);
return new ViewHolderBrandFeed(v);
}
case VIEW_BIG_BANNER: {
v = LayoutInflater.from(context).inflate(R.layout.listitem_brandfeed_big_banner, parent, false);
return new ViewHolderBigBanner(v);
}
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (BuildConfig.DEBUG)
Log.d(TAG, "onBindViewHolder: " + position);
if (null != holder) {
if (holder instanceof ViewHolderLook) {
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
setHeadersData((ViewHolderLook<T>) holder, position);
}
bindLookView((ViewHolderLook<T>) holder, position);
} else if (holder instanceof ViewHolderProduct) {
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
setHeadersData((ViewHolderProduct<T>) holder, position);
}
bindProductView((ViewHolderProduct<T>) holder, position);
} else if (holder instanceof ViewHolderBlog) {
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
setHeadersData((ViewHolderBlog<T>) holder, position);
}
bindBlogView((ViewHolderBlog<T>) holder, position);
} else if (holder instanceof ViewHolderBrands) {
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
setHeadersData((ViewHolderBrands<T>) holder, position);
}
bindBrand((ViewHolderBrands<T>) holder, position);
} else if (holder instanceof ViewHolderAd)
bindAd((ViewHolderAd<T>) holder, position);
else if (holder instanceof ViewHolderNewArrivals)
bindNewArrival((ViewHolderNewArrivals<T>) holder, position);
else if (holder instanceof ViewHolderFeaturedBrands)
bindFeaturedBrands((ViewHolderFeaturedBrands<T>) holder, position);
else if (holder instanceof ViewHolderProgress) {
if (2 == context.getResources().getInteger(R.integer.gridviewcolumn)) {
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
}
} else if (holder instanceof ViewHolderBigBanner) {
bindBigBanner((ViewHolderBigBanner<T>) holder, position);
} else if (holder instanceof ViewHolderBrandFeed) {
bindBrandFeed((ViewHolderBrandFeed<T>) holder, position);
}
if (null == holderArrayList)
holderArrayList = new ArrayList<>();
if (null != objects && position != -1 && position < objects.size()) {
if (position < holderArrayList.size() || position == 0)
holderArrayList.add(position, holder);
else
holderArrayList.add(holder);
}
}
}
#Override
public long getHeaderId(int position) {
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn))
return -1;
switch (getItemViewType(position)) {
case VIEW_PROGRESS:
case VIEW_AD:
case VIEW_NEW_ARRIVALS:
case VIEW_FEATURED_BRANDS:
case VIEW_BIG_BANNER:
case VIEW_BRAND_FEED:
return -1;
default:
return getItem(position).hashCode();
}
}
#Override
public long getItemId(int position) {
return getItem(position).hashCode();
}
#Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
StickyHeaderViewHolder<T> holder = new StickyHeaderViewHolder<T>(LayoutInflater.from(context)
.inflate(R.layout.listitem_header, parent, false));
initializeHeaders(holder.view, holder);
return holder;
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (BuildConfig.DEBUG)
Log.d(TAG, "onBindHeaderViewHolder: " + position);
if (viewHolder instanceof StickyHeaderViewHolder) {
setHeadersData((StickyHeaderViewHolder) viewHolder, position);
}
}
/**
* Overloading function for setting headers for tabs for sticky headers
*
* #param holder
* #param position
*/
private void setHeadersData(StickyHeaderViewHolder holder, int position) {
holder.view.setOnClickListener(new View.OnClickListener() {
// This click listener is not working as sticky headers are just canvas and not views
// as desired for click listeners.
#Override
public void onClick(View v) {
if (null != v.getTag()) {
if (v.getTag() instanceof Looks) {
Looks data = (Looks) v.getTag();
openLook(data);
} else if (v.getTag() instanceof Brand) {
Brand data = (Brand) v.getTag();
openBrand(data.getBrandId(), data.getBrandName());
} else if (v.getTag() instanceof Product) {
Product data = (Product) v.getTag();
openBrand(data.getBrandId(), data.getBrandName());
} else if (v.getTag() instanceof Blog) {
Blog data = (Blog) v.getTag();
}
}
}
});
final T type = getItem(position);
if (type instanceof Feed) {
Feed object = (Feed) type;
switch (object.getViewType()) {
case Feed.LOOK_VIEW: {
Looks data = object.getLook();
holder.imgHeaderDp.setImageUrl(data.getStylistImage(), MyApp.getInstance().getImageLoader());
holder.lblHeaderName.setText(data.getStylistName());
holder.lblHeaderTime.setText(TimeConversions.longToPrettyTimeString(data.getCreatedDate()));
holder.lblHeaderTime.setVisibility(View.VISIBLE);
holder.view.setVisibility(View.VISIBLE);
holder.view.setTag(data);
break;
}
case Feed.BLOG_VIEW: {
Blog data = object.getBlog();
// holder.imgHeaderDp.setImageUrl(data.getThumb(), MyApp.getInstance().getImageLoader());
holder.lblHeaderName.setText(data.getAuthorName());
holder.lblHeaderTime.setText(TimeConversions.longToPrettyTimeString(data.getPostedDate()));
//
holder.lblHeaderTime.setVisibility(View.VISIBLE);
holder.view.setVisibility(View.VISIBLE);
holder.view.setTag(data);
break;
}
case Feed.PRODUCT_VIEW: {
Product data = object.getProduct();
// holder.imgHeaderDp.setImageUrl(data.getProductThumbnail(), MyApp.getInstance().getImageLoader());
holder.lblHeaderName.setText(data.getBrandName());
holder.lblHeaderTime.setVisibility(View.GONE);
holder.view.setVisibility(View.VISIBLE);
holder.view.setTag(data);
break;
}
case Feed.BRAND_VIEW: {
Brand data = object.getBrand();
holder.imgHeaderDp.setImageUrl(data.getImgLogoUrl(), MyApp.getInstance().getImageLoader());
holder.lblHeaderName.setText(data.getBrandName());
holder.lblHeaderTime.setVisibility(View.GONE);
holder.view.setVisibility(View.VISIBLE);
holder.view.setTag(data);
}
}
} else if (objects.get(position) instanceof Looks) {
if (null != ((Looks) objects.get(position)).getLookId()) {
// Looks in Looks feed
Looks data = ((Looks) objects.get(position));
holder.imgHeaderDp.setImageUrl(data.getStylistImage(), MyApp.getInstance().getImageLoader());
holder.lblHeaderName.setText(data.getStylistName());
holder.lblHeaderTime.setText(TimeConversions.longToPrettyTimeString(data.getCreatedDate()));
holder.lblHeaderTime.setVisibility(View.VISIBLE);
holder.view.setVisibility(View.VISIBLE);
holder.view.setTag(data);
}
}
}
class ViewHolderLook<T> extends RecyclerView.ViewHolder {
// Header
RoundedNetworkImageView imgHeaderDp;
FontTextView lblHeaderName;
FontTextView lblHeaderTime;
View view;
// Body
FontTextView lblLookName, lblLookDesc;
CustomCheckbox chkFavorite;
ImageView btnShare;
SquareNetworkImageView imgLook;
public ViewHolderLook(View view) {
super(view);
chkFavorite = (CustomCheckbox) view.findViewById(R.id.chk_favorite);
btnShare = (ImageView) view.findViewById(R.id.btn_share);
imgLook = (SquareNetworkImageView) view.findViewById(R.id.img_look);
lblLookName = (FontTextView) view.findViewById(R.id.lbl_title);
lblLookDesc = (FontTextView) view.findViewById(R.id.lbl_desc);
// if it's tab then only initialize else not required
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
this.view = view.findViewById(R.id.header);
}
}
}
class ViewHolderBlog<T> extends RecyclerView.ViewHolder {
// Header
RoundedNetworkImageView imgHeaderDp;
FontTextView lblHeaderName;
FontTextView lblHeaderTime, lblReadFull;
View view;
View shareView, root;
LinearLayout llShare;
FontTextView lblBlogTitle, lblAuthor, lblRead, lblTime, lblDescription, lblLookCount;
FontButton btnCopyToClipboard, btnWhatsApp, btnEmail, btnFacebook, btnTwitter, btnGPlus, btnPinterest, btnInstagram;
SquareNetworkImageView imgBlog;
CustomNetworkImageView blog_image;
ImageView chkShare;
public ViewHolderBlog(View view) {
super(view);
lblBlogTitle = (FontTextView) view.findViewById(R.id.blog_title);
lblDescription = (FontTextView) view.findViewById(R.id.blog_description);
lblLookCount = (FontTextView) view.findViewById(R.id.lbl_looks_count);
lblAuthor = (FontTextView) view.findViewById(R.id.txt_author);
imgBlog = (SquareNetworkImageView) view.findViewById(R.id.iv_blogimage);
lblRead = (FontTextView) view.findViewById(R.id.txt_read);
lblTime = (FontTextView) view.findViewById(R.id.txt_timer);
llShare = (LinearLayout) view.findViewById(R.id.ll_share_container);
chkShare = (ImageView) view.findViewById(R.id.share);
blog_image = (CustomNetworkImageView) view.findViewById(R.id.blog_image);
lblReadFull = (FontTextView) view.findViewById(R.id.read_full);
this.root = view;
// if it's tab then only initialize else not required
if (2 <= context.getResources().getInteger(R.integer.gridviewcolumn)) {
this.view = view.findViewById(R.id.header);
}
}
}
class ViewHolderAd<T> extends RecyclerView.ViewHolder {
protected CustomViewPager viewPager;
protected LinearLayout llPage;
public ViewHolderAd(View view) {
super(view);
viewPager = (CustomViewPager) view.findViewById(R.id.view_pager);
llPage = (LinearLayout) view.findViewById(R.id.ll_page);
}
}
.
.
.
.
}
And this is the layout for the first Feed screen
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/lookforu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mycompany.myapp.app.custom.widget.GifSwipeRefreshLayout
android:id="#+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<view
android:id="#+id/grid_view"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_padding"
android:gravity="center" />
</com.mycompany.myapp.app.custom.widget.GifSwipeRefreshLayout>
</RelativeLayout>

Related

(android) Problems with random changes in dynamic nested recycler view items

Hi,
I'm making a dynamic Nested RecyclerView.
The Outer RecyclerView (Routine) consists of parts (A, B, C...), add button,
and delete button, and the inner RecyclerView (Detail Routine) consists of text view (set, weight and count).
When i add an outer item, it have one inner RecyclerView item by default.
I thought the implementation was successful, but it wasn't.
The picture is a condition that I have made some items in advance.
This is a picture that creates additional items there.
However, in the picture, when I added the G item, you can see that the F item
and the B item have a change.
And the G item, oddly enough, has two basic items. Originally, it is one.
The second picture is the later item addition.
If i add a new outer recyclerview item, there will be a new change in the whole
item again.
I am not sure why this change is happening. If you add a new item, you want the
existing item to remain.
Please tell me the cause
Here is Code
RoutineAdapter.java ( Outer RecyclerView Adpater )
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
public RoutineDetailAdapter detailAdapter;
OnRoutineItemClickListener listener;
public void setOnRoutineClickListener(OnRoutineItemClickListener listener) {
this.listener = listener;
}
public void addItem(RoutineModel item) {
routineItems.add(item);
notifyDataSetChanged();
}
public void addDetailItem(RoutineDetailAdapter curDetailAdapter) {
curDetailAdapter.addItem(new RoutineDetailModel());
}
public void deleteDetailItem(RoutineDetailAdapter curDetailAdapter) {
curDetailAdapter.deleteItem();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
detailAdapter = new RoutineDetailAdapter();
holder.setRoutineDetailRecyClerView();
holder.rv_detail.setAdapter(detailAdapter);
detailAdapter.addItem(new RoutineDetailModel());
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineModel curRoutineItem = routineItems.get(position);
holder.setItems(curRoutineItem);
}
#Override
public int getItemCount() {
return routineItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView rv_detail;
TextView routine;
Button addSet;
Button deleteSet;
public ViewHolder(#NonNull View itemView) {
super(itemView);
initViews();
addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RoutineDetailAdapter curDetailAdapter = (RoutineDetailAdapter) rv_detail.getAdapter();
listener.OnAddBtnClick(curDetailAdapter);
}
});
deleteSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RoutineDetailAdapter curDetailAdapter = (RoutineDetailAdapter) rv_detail.getAdapter();
listener.OnDeleteBtnClick(curDetailAdapter);
}
});
}
private void initViews() {
routine = itemView.findViewById(R.id.routine);
rv_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
deleteSet = itemView.findViewById(R.id.delete_set);
}
private void setItems(RoutineModel routineItem) {
routine.setText(routineItem.getRoutine());
}
public void setRoutineDetailRecyClerView() {
rv_detail.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
}
}
public interface OnRoutineItemClickListener {
public void OnAddBtnClick(RoutineDetailAdapter detailAdapter);
public void OnDeleteBtnClick(RoutineDetailAdapter detailAdapter);
}
}
RoutineDetailAdapter.java ( Inner RecyclerView Adapter )
public class RoutineDetailAdapter extends RecyclerView.Adapter<RoutineDetailAdapter.ViewHolder>{
ArrayList<RoutineDetailModel> items = new ArrayList<>();
Context context;
int insertPosition;
int deletePosition;
public void addItem(RoutineDetailModel item) {
items.add(item);
notifyItemInserted(insertPosition);
}
public void deleteItem() {
try {
if(items.size() > 1) { // Leave at least one set
items.remove(deletePosition);
notifyItemRemoved(deletePosition);
deletePosition -= 1;
}
} catch (Exception e) {
// empty
}
}
public ArrayList<RoutineDetailModel> getItem() {
return this.items;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.routine_detail_item, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineDetailModel item = items.get(position);
holder.setItem(item, position);
insertPosition = position + 1;
deletePosition = position;
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView set;
public ViewHolder(#NonNull View itemView) {
super(itemView);
set = itemView.findViewById(R.id.set);
}
private void setItem(RoutineDetailModel item, int setCount) {
set.setText(setCount + 1 + "set");
}
}
}
WriteRoutineActivity.java
public class WriteRoutineActivity extends AppCompatActivity {
Button add_routine_btn;
TextView title;
RecyclerView routine_rv;
RoutineAdapter routineAdapter;
LinearLayoutManager routineLayoutManger;
ArrayList<String> titleData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_routine);
initViews();
setPageTitle(getIntent());
setRoutineRecyclerview();
routineAdapter = new RoutineAdapter();
routine_rv.setAdapter(routineAdapter);
add_routine_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WorkoutListDialogFragment routineDialog = new WorkoutListDialogFragment();
routineDialog.show(getSupportFragmentManager(), "RoutineListDialog");
}
});
routineAdapter.setOnRoutineClickListener(new RoutineAdapter.OnRoutineItemClickListener() {
#Override
public void OnAddBtnClick(RoutineDetailAdapter curDetailAdapter) {
routineAdapter.addDetailItem(curDetailAdapter);
}
#Override
public void OnDeleteBtnClick(RoutineDetailAdapter curDetailAdapter) {
routineAdapter.deleteDetailItem(curDetailAdapter);
}
});
}
private void initViews() {
title = findViewById(R.id.body_part_detail_title);
routine_rv = findViewById(R.id.routine_recyclerview);
add_routine_btn = findViewById(R.id.add_routine);
}
private void setRoutineRecyclerview() {
routineLayoutManger = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);
routine_rv.setLayoutManager(routineLayoutManger);
routine_rv.setHasFixedSize(true);
RecyclerView.ItemDecoration divider = new DividerItemDecorator(ContextCompat.getDrawable(getApplicationContext(), R.drawable.divider));
routine_rv.addItemDecoration(divider);
}
public void setPageTitle(Intent intent) {
if(intent != null) {
titleData = intent.getStringArrayListExtra("bodypart");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String new_title = String.join(" / ", titleData);
title.setText(new_title);
}
else {
StringBuilder new_title = new StringBuilder();
for (int i = 0; i < titleData.size(); i++) {
if (i == titleData.size() - 1) {
new_title.append(titleData.get(i));
break;
}
new_title.append(titleData.get(i)).append(" / ");
}
title.setText(new_title);
}
}
}
public void addRoutine(String routine) {
routineAdapter.addItem(new RoutineModel(routine));
routine_rv.smoothScrollToPosition(routineAdapter.getItemCount() - 1);
}
}
ADDED
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".data.DailyRecordDetailActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AppBarOverlay"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/body_part_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PART"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<Button
android:id="#+id/add_routine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROUTINE ADD"
android:backgroundTint="#color/light_green"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/routine_recyclerview"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/routine_recyclerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/add_routine"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
RoutineItem.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/white"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<LinearLayout
android:id="#+id/linear_routine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<TextView
android:id="#+id/routine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:text="ROUTINE"
android:textSize="16dp"
android:textStyle="bold"
android:layout_weight="2"/>
<Button
android:id="#+id/add_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:text="ADD"
android:textColor="#color/black"
android:gravity="center"
android:layout_weight="1"/>
<Button
android:id="#+id/delete_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:text="DELETE"
android:textColor="#color/black"
android:layout_weight="1"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/detail_routine"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#+id/linear_routine"
app:layout_constraintLeft_toLeftOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
RoutineDetail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:padding="10dp"
android:clipToPadding="false">
<TextView
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SET"/>
<TextView
android:id="#+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0 kg"/>
<TextView
android:id="#+id/reps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
</LinearLayout>
For every RoutineModel item you recreate detailAdapter but you need new instance. Check TODO in code below:
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
//public RoutineDetailAdapter detailAdapter; //TODO move it to local variable in method onCreateViewHolder
OnRoutineItemClickListener listener;
like this
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
RoutineDetailAdapter detailAdapter = new RoutineDetailAdapter(); //TODO Create local variable
detailAdapter.addItem(new RoutineDetailModel());
holder.setRoutineDetailRecyClerView();
holder.rv_detail.setAdapter(detailAdapter);
return holder;
}
But this is not good way to solve this... Its better to use one Adapter with multiple item views. I have no time now to explain, I will post some code later...
EDITED
I had same problem and and solved this on this (better) way:
Create Adapter that support multiple views
public class MultipleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Object> items; //List<Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
private OnItemClickListener listener; //TODO new interface
..............
}
Override onCreateViewHolder like this
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if(viewType == 1){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_item, parent, false);
return new RoutineViewHolder (itemView);
}/*else{*/
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_detail_item, parent, false);
return new RoutineDetailsViewHolder (itemView);
//}
}
Override onBindViewHolder like this and create your updateViews methods
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Object object = items.get(position); //Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
if(object instanceof RoutineModel) {
//create method
updateRoutineViews((RoutineViewHolder) holder, (RoutineModel) object, position);
}else if(object instanceof RoutineDetailsModel) {
//create method
updateRoutineDetailsViewHolder((RoutineDetailsViewHolder) holder, (RoutineDetailModel) object);
}
}
and override getItemViewType method
#Override
public int getItemViewType(int position) {
Object object = items.get(position);
if(object instanceof RoutineModel){
return 1;
}
//else if instanceOf RoutineDetailModel return 0
return 0;
}
This is so simple and better for performance...
Also in OnItemClickListener get item by position and check instanceOf object like above. You can add new item to the clickedItemPosition + 1 with adapter.notifyDataSetChanges()
In MainActivity you have to create mixed list with Routine and Routine details
List<Object> mixedList = new ArrayList<>();
for(RoutineModel rm: routineList){
mixedList.add(rm);
if(rm has routineDetailModels){ //pseudo code
for(RoutineDetailModels rmdetilas: rm.getRoutineDetailModels()){
mixedList.add(rmdetilas);
}
}
}
adpater.swapData(mixedList); //create your method to swap or set data to adapter
EDITED
handle click
public interface OnItemClickListener{
void onClick(View view, int position);
void onLongClick(View view, int position);
}
and
adapter.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onClick(View view, int position) {
Object item = (Object) adapter.getItem(position);
if(item instanceof Routine) {
if (view.getId() == R.id.delete_id) {
//TODO delete form list, notifyItemRemoved or generate all mixeddata again and swap
} else if (view.getId() == R.id.whateveryouwant) {
//TODO doSomething()
}
}
}
#Override
public void onLongClick(View view, int position) {
}
});
The code above is just an example of how to solve the problem
For more help feel free to comment
EDITED 2
public class RoutineViewHolder extends RecyclerView.ViewHolder {
public RecyclerView rv_detail;
public TextView routine;
public Button addSet;
public Button deleteSet;
public ViewHolder(#NonNull View itemView) {
super(itemView);
//initViews(); in constructor
routine = itemView.findViewById(R.id.routine);
rv_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
deleteSet = itemView.findViewById(R.id.delete_set);
}
}
update method called from onBindViewHolder
private void updateRoutineViews(RoutineViewHolder holder, RoutineModel routineItem, position){
holder.routine.setText(routineItem.getRoutine());
holder.rv_detail.setText(routineItem.getWhateverYouWant());
holder.deleteSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onClick(v, position); //or holder.getAdapterPosition()
}
});
}
COMPLETE CODE
Create complete new app to test it and understand (copy/paste this code), then integrate in your app...
There are more improvements but I don't have time, this is a quick fix
Activity
public class RoutineActivity extends AppCompatActivity {
private MultipleViewAdapter adapter;
private List<RoutineModel> routineList;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routine);
RecyclerView contentList = findViewById(R.id.list);
contentList.setLayoutManager(new LinearLayoutManager(this));
adapter = new MultipleViewAdapter(this, new ArrayList<>());
contentList.setAdapter(adapter);
adapter.setOnItemClickListener(new MultipleViewAdapter.OnItemClickListener() {
#Override
public void onClick(View view, int position) {
Object item = (Object) adapter.getItem(position);
if(item instanceof RoutineModel) {
RoutineModel routineModel = (RoutineModel) item;
if (view.getId() == R.id.add_set) {
int weight = randomInt(99);
Toast.makeText(RoutineActivity.this, "New item with weight: " + weight + " kg", Toast.LENGTH_SHORT).show();
routineModel.addDetails(new RoutineDetailsModel(routineModel.getDetailsSize() + 1, weight));
adapter.swapData(getMixedList()); // OR add item to adapter and notify item inserted
//TODO implement SnappingLinearLayoutManager and set list.smoothScroleto... routineModel
} else if (view.getId() == R.id.delete_set) {
//TODO doSomething()
boolean deleted = routineModel.removeDetails(routineModel.getDetailsSize() - 1); // -1 !!! to delete last item
Toast.makeText(RoutineActivity.this, deleted ? "Last item is deleted": "No more items", Toast.LENGTH_SHORT).show();
adapter.swapData(getMixedList()); // OR remove item from adapter and notify item removed
//TODO implement SnappingLinearLayoutManager and set list.smoothScroleto.... routineModel
}
}else if(item instanceof RoutineDetailsModel) {
RoutineDetailsModel routineModel = (RoutineDetailsModel) item;
Toast.makeText(RoutineActivity.this, "Weight: " + routineModel.getWeight() + " kg", Toast.LENGTH_SHORT).show();
//TODO EDITED 4 (copy/paste this) random new Weight
routineModel.setWeight(randomInt(99));
adapter.notifyItemChanged(position);
Toast.makeText(RoutineActivity.this, "New Weight: " + routineModel.getWeight() + " kg", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onLongClick(View view, int position) {
}
});
initFakeData();
adapter.swapData(getMixedList());
}
private List<Object> getMixedList() {
List<Object> mixedList = new ArrayList<>();
for(RoutineModel rm: routineList){
mixedList.add(rm);
if(rm.getRoutineDetailsModel() != null && rm.getRoutineDetailsModel().size() > 0){
for(RoutineDetailsModel rmdetilas: rm.getRoutineDetailsModel()){
mixedList.add(rmdetilas);
}
}
}
return mixedList;
}
private void initFakeData() {
routineList = new ArrayList<>();
for(int i = 0; i < 5; i++){
RoutineModel routineModel = new RoutineModel(String.valueOf(i + 1));
for(int j = 0; j < 4; j++){
routineModel.addDetails(new RoutineDetailsModel(j+1, randomInt(99)));
}
routineList.add(routineModel);
}
}
private int randomInt(int max) {
return (int) Math.floor(Math.random() * max);
}
}
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Adapter
public class MultipleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context; // TODO only if you need conetxt
private List<Object> items;
private OnItemClickListener onItemClickListener;
public MultipleViewAdapter(Context context, List<Object> items) {
this.context = context;
this.items = items;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if(viewType == 1){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_item, parent, false);
return new RoutineViewHolder(itemView);
}/*else{*/
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_detail_item, parent, false);
return new RoutineDetailsViewHolder(itemView);
//}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
Object object = items.get(position); //Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
if(object instanceof RoutineModel) {
updateRoutineViews((RoutineViewHolder) holder, (RoutineModel) object, position);
}else if(object instanceof RoutineDetailsModel) {
updateRoutineDetailsViewHolder((RoutineDetailsViewHolder) holder, (RoutineDetailsModel) object, position);
}
}
private void updateRoutineViews(RoutineViewHolder holder, RoutineModel routineItem, int position){
holder.routine.setText("Routine " + routineItem.getRoutine());
holder.addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onItemClickListener != null) onItemClickListener.onClick(v, position);
}
});
holder.deleteSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onItemClickListener != null) onItemClickListener.onClick(v, position);
}
});
}
private void updateRoutineDetailsViewHolder(RoutineDetailsViewHolder holder, RoutineDetailsModel routineDetailsModel, int position){
holder.set.setText(routineDetailsModel.getSet() + " set");
holder.weight.setText(routineDetailsModel.getWeight() + " kg");
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onItemClickListener != null) onItemClickListener.onClick(v, position);
}
});
}
#Override
public int getItemViewType(int position) {
Object object = items.get(position);
if(object instanceof RoutineModel){
return 1;
}
//else if instanceOf RoutineDetailModel return 0
return 0;
}
#Override
public int getItemCount() {
if(items == null) return 0;
return items.size();
}
public Object getItem(int position) {
if(this.items == null || position < 0 || position >= this.items.size()) return null;
return this.items.get(position);
}
public void swapData(List<Object> newItems) {
if (newItems != null) {
this.items = newItems;
notifyDataSetChanged();
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RoutineViewHolder extends RecyclerView.ViewHolder {
public TextView routine;
public Button addSet;
public Button deleteSet;
public RoutineViewHolder(#NonNull View itemView) {
super(itemView);
//initViews(); in constructor
routine = itemView.findViewById(R.id.routine);
addSet = itemView.findViewById(R.id.add_set);
deleteSet = itemView.findViewById(R.id.delete_set);
}
}
public static class RoutineDetailsViewHolder extends RecyclerView.ViewHolder {
public TextView set;
public TextView weight;
public RoutineDetailsViewHolder(#NonNull View itemView) {
super(itemView);
set = itemView.findViewById(R.id.set);
weight = itemView.findViewById(R.id.weight);
}
}
}
RoutineModel
public class RoutineModel {
private List<RoutineDetailsModel> routineDetailsList;
private String routine;
public RoutineModel(String routine) {
this.routine = routine;
}
public List<RoutineDetailsModel> getRoutineDetailsModel() {
return routineDetailsList;
}
public void setRoutineDetailsModel(List<RoutineDetailsModel> routineDetailsModel) {
this.routineDetailsList = routineDetailsModel;
}
public void addDetails(RoutineDetailsModel item) {
if(routineDetailsList == null) {
routineDetailsList = new ArrayList<>();
}
this.routineDetailsList.add(item);
}
public boolean removeDetails(int index) {
if(routineDetailsList == null || index >= routineDetailsList.size() || index < 0) return false;
this.routineDetailsList.remove(index);
return true;
}
public String getRoutine() {
return routine;
}
public void setRoutine(String routine) {
this.routine = routine;
}
public int getDetailsSize() {
if(routineDetailsList == null) return 0;
return routineDetailsList.size();
}
}
RoutineDetailsModel
public class RoutineDetailsModel {
private int set;
private int weight;
public RoutineDetailsModel(int set, int weight) {
this.set = set;
this.weight = weight;
}
public int getSet() {
return set;
}
public void setSet(int set) {
this.set = set;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
Item layouts
Find in question: routine_item.xml and routine_detail_item.xml

Nested RecyclerView onClick not triggered

I have two nested RecyclerViews - basically a list of horizontal RecyclerViews and I am struggling to make the items in the horizontal RecyclerView clickable.
This is the main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/overview_list"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<components.emptyView.EmptyView
android:id="#+id/empty_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"/>
</LinearLayout>
Then RecyclerView has the following item layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView android:id="#+id/tables"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:clipToPadding="false"
android:paddingEnd="18dp"
android:paddingStart="18dp"/>
And I'm trying to get the onclick setup in the adapter for the inner RecyclerView as follows:
#Override
public void onBindViewHolder(#NonNull LeagueTableViewHolder holder, int position) {
Pair<Long, String> table = tables.get(position);
holder.bindTable(table.first, table.second);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// click event here
}
});
}
I've tried making the ViewHolder's itemview as clickable programatically and in XML and it doesn't seem to make a difference. Same for the inner RecyclerView.
Any idea what I'm doing wrong?
Edit: Added parent RecyclerView adapter
public class OverviewListAdapter extends RecyclerView.Adapter<BaseViewHolder> {
public final static int TEAM_PICTURE = 0;
public final static int FIXTURES = 1;
public final static int LEAGUE_TABLES = 2;
public final static int LATEST_NEWS = 3;
public final static int LATEST_EVENTS = 4;
private final RecyclerView.RecycledViewPool viewPool;
private List<BaseOverviewItemVM> viewModels = new ArrayList<>();
private int tint;
private OverviewLatestNewsVM newsVM;
private OverviewLatestEventsVM eventsVM;
private OverviewTablesVM tablesVM;
private OverviewFixturesVM fixturesVM;
private OverviewTeamPicVM teamPicVM;
OverviewListAdapter(int tint) {
this.tint = tint;
viewPool = new RecyclerView.RecycledViewPool();
}
void addViewModel(OverviewLatestNewsVM viewModel){
if(newsVM == null){
newsVM = viewModel;
viewModels.add(newsVM);
sort();
int viewModelIndex = viewModels.indexOf(newsVM);
notifyItemInsertedIfValid(viewModelIndex);
} else {
int currentNewsIndex = viewModels.indexOf(newsVM);
newsVM = viewModel;
if(currentNewsIndex >= 0) {
this.viewModels.set(currentNewsIndex, newsVM);
}
notifyItemChangedIfValid(currentNewsIndex);
}
}
void addViewModel(OverviewLatestEventsVM viewModel){
if(eventsVM == null){
eventsVM = viewModel;
viewModels.add(eventsVM);
sort();
int viewModelIndex = viewModels.indexOf(eventsVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(eventsVM);
eventsVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, eventsVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
void addViewModel(OverviewFixturesVM viewModel){
if(fixturesVM == null){
fixturesVM = viewModel;
viewModels.add(fixturesVM);
sort();
int viewModelIndex = viewModels.indexOf(fixturesVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(fixturesVM);
fixturesVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, fixturesVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
void addViewModel(OverviewTablesVM viewModel){
if(tablesVM == null){
tablesVM = viewModel;
viewModels.add(tablesVM);
sort();
int viewModelIndex = viewModels.indexOf(tablesVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(tablesVM);
tablesVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, tablesVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
void addViewModel(OverviewTeamPicVM viewModel){
if(teamPicVM == null){
teamPicVM = viewModel;
viewModels.add(teamPicVM);
sort();
int viewModelIndex = viewModels.indexOf(teamPicVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(teamPicVM);
teamPicVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, teamPicVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
private void sort(){
// sort by the item type defined at the top of this class
Collections.sort(viewModels, (o1, o2) -> {
if(o1.getType() > o2.getType()){
return 1;
} else if(o1.getType() < o2.getType()){
return -1;
}
return 0;
});
}
private void notifyItemInsertedIfValid(int index){
if(index >= 0 && index < getItemCount()){
notifyItemInserted(index);
} else {
notifyDataSetChanged();
}
}
private void notifyItemChangedIfValid(int index){
if(index >= 0 && index < getItemCount()){
notifyItemChanged(index);
} else {
notifyDataSetChanged();
}
}
#NonNull
#Override
public BaseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType){
case FIXTURES:
View fixturesView = inflater.inflate(R.layout.row_overview_fixtures, parent, false);
OverviewFixturesVH fixturesHolder = new OverviewFixturesVH(fixturesView);
fixturesHolder.fixturesOverview.setRecycledViewPool(viewPool);
return fixturesHolder;
case LEAGUE_TABLES:
View tablesView = inflater.inflate(R.layout.row_overview_league_tables, parent, false);
OverviewTablesVH tablesHolder = new OverviewTablesVH(tablesView);
tablesHolder.tablesList.setRecycledViewPool(viewPool);
return tablesHolder;
case LATEST_EVENTS:
View upcomingEvents = inflater.inflate(R.layout.row_latest_list_view, parent, false);
return new OverviewLatestEventsVH(upcomingEvents, tint);
case TEAM_PICTURE:
View teamPicView = inflater.inflate(R.layout.row_overview_team_pic, parent, false);
return new OverviewTeamPicVH(teamPicView);
default:
View latestNewsView = inflater.inflate(R.layout.row_latest_list_view, parent, false);
return new OverviewLatestNewsVH(latestNewsView, tint);
}
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
if(holder instanceof OverviewFixturesVH) {
Optional<BaseOverviewItemVM> fixturesVM = getViewModelOfType(FIXTURES);
fixturesVM.ifPresent(viewModel -> ((OverviewFixturesVH) holder).setFixtures(((OverviewFixturesVM)viewModel).getFixtures()));
} else if (holder instanceof OverviewTablesVH) {
Optional<BaseOverviewItemVM> tablesVM = getViewModelOfType(LEAGUE_TABLES);
tablesVM.ifPresent(vm -> ((OverviewTablesVH) holder).setTableIds(((OverviewTablesVM) vm).getTableIds()));
} else if (holder instanceof OverviewLatestEventsVH){
Optional<BaseOverviewItemVM> eventsVM = getViewModelOfType(LATEST_EVENTS);
eventsVM.ifPresent(vm -> ((OverviewLatestEventsVH) holder).setEvents(((OverviewLatestEventsVM) vm).getEvents()));
} else if (holder instanceof OverviewLatestNewsVH){
Optional<BaseOverviewItemVM> newsVM = getViewModelOfType(LATEST_NEWS);
newsVM.ifPresent(vm -> ((OverviewLatestNewsVH) holder).setNews(((OverviewLatestNewsVM) vm).getNews()));
} else if (holder instanceof OverviewTeamPicVH){
Optional<BaseOverviewItemVM> teamPicVM = getViewModelOfType(TEAM_PICTURE);
teamPicVM.ifPresent(vm -> ((OverviewTeamPicVH) holder).setPictureUrl(((OverviewTeamPicVM) vm).getTeamPicUrl()));
}
}
#Override
public int getItemViewType(int position) {
return viewModels.get(position).getType();
}
#Override
public int getItemCount() {
return viewModels.size();
}
private Optional<BaseOverviewItemVM> getViewModelOfType(int type){
if(viewModels != null ) {
for (BaseOverviewItemVM viewModel : viewModels) {
if(viewModel.getType() == type){
return Optional.of(viewModel);
}
}
}
return Optional.empty();
}
}
I think you will be able to handle it creating a listener and handle the call with it:
public interface clickListenerItem {
void onItemClick(Item element);
}
add this interface in your adapter and in your main.
Adding this function to your *nameclass*ViewHolder extends RecyclerView.ViewHolder :
public void bind(final ItemName item, final clickListenerItem listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
then add to your Adapter variables: private final clickListenerItem clickItem;
in your constructor: this.clickItem = clickItem;
and in onBindViewHolder of your adapter call:
public void onBindViewHolder(final #NonNull ItemViewHolder holder, int position) {
holder.bind(list.get(position), clickItem);
}
now when initiliazing your adapter in the main you just add your listener adding it with a call like this:
adapter = new ItemAdapter(this, list, (new ItemAdapter.clickListenerItem(){
#Override public void onItemClick(Item thisItem) {
Toast.makeText(getApplicationContext(), "Loading Item...", Toast.LENGTH_LONG).show();
Intent viewElem = new Intent(getApplicationContext(), ViewItem.class);
startActivityForResult(viewElem ,codeForResult);
}
}));
I hope this will help you, obviusly you can change the last part for not calling a new activity but changing it with what you want to see or do with your item.

Error while displaying items in RecyclerView with Multiple ViewTypes

I am using Recyclerview with multiple ViewTypes.I have two Model classes for two different type of item in recyclerview as.I want to display two different type of item in same RecyclerView
My RecyclerViewAdapter is
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
ArrayList<OneView> oneList;
ArrayList<TwoView> twoList;
public RecyclerAdapter(Context context,ArrayList<OneView> oneList,ArrayList<TwoView> twoList){
this.context=context;
this.oneList=oneList;
this.twoList=twoList;
}
#Override
public int getItemViewType(int position) {
switch (position){
case 0:
return 0;
case 1:
return 1;
}
return -1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout=0;
RecyclerView.ViewHolder viewHolder;
if(viewType==0){
layout=R.layout.oneview;
View one= LayoutInflater.from(parent.getContext()).inflate(layout,parent,false);
return new ViewHolder(one);
}else if(viewType==1){
layout=R.layout.twoview;
View two= LayoutInflater.from(parent.getContext()).inflate(layout,parent,false);
return new SecondView(two);
}else {
return null;
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType=holder.getItemViewType();
switch (viewType){
case 0:
OneView oneView=oneList.get(position);
((ViewHolder)holder).name.setText(oneView.getName());
break;
case 1:
TwoView twoView=twoList.get(position);
((SecondView)holder).name.setText(twoView.getName());
((SecondView)holder).address.setText(twoView.getAddress());
break;
default:
break;
}
}
#Override
public int getItemCount() {
return (this.oneList.size()+this.twoList.size());
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView name;
public ViewHolder(View itemView) {
super(itemView);
name=(TextView)itemView.findViewById(R.id.nameOne);
}
}
public class SecondView extends RecyclerView.ViewHolder {
private TextView name,address;
public SecondView(View itemView) {
super(itemView);
name=(TextView)itemView.findViewById(R.id.nameTwo);
address=(TextView)itemView.findViewById(R.id.address);
}
}
}
I want to display two type of item in same RecyclerView.But I am getting Attempt to write to field 'int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null object reference exception. How to resolve this ?
here is the complete example. I've used a RecyclerView with Multiple View Type. My scenario was, I have to show "cuisine list" [which is a horizontal view of multiple cuisines], then "number of restaurant", then "restaurant list".
so, i've used VIEW_RESTAURANT for restaurant view, VIEW_CUISINE for cuisine, VIEW_INFO for number of restaurant. As for my case, there's pagination, so VIEW_PROGRESS is used for showing progress bar at the bottom [by that time new set of data is fetch, if avaiable], VIEW_NO_DATA is used if restaurant list is empty.
hopefully from here, You can manage based on your requirement.
public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private String TAG = HomeAdapter.class.getSimpleName();
class ViewType {
int VIEW_PROGRESS = 1;
int VIEW_NO_DATA = 2;
int VIEW_RESTAURANT = 3;
int VIEW_CUISINE = 4;
int VIEW_INFO = 6;
}
private ViewType mViewType = new ViewType();
private Context mContext;
private int mCuisineItem = 1;
private int mInfoItem = 1;
private int mShowNoItem = 0;
private boolean mIsLoading;
private int mRestaurantNumber;
private int mCurrentPage;
private List<Data> mDataList;
private List<CuisineData> mCuisines;
private PaginationListener mPaginationListener;
private HorizontalPaginationListener mHorizontalPaginationListener;
private SearchResultClickListener mSearchResultClickListener;
private String mFrom;
public HomeAdapter(Context context, List<Data> dataList, List<CuisineData> cuisines, RecyclerView recyclerView, SearchResultClickListener searchResultClickListener) {
mContext = context;
mDataList = dataList;
mCuisines = cuisines;
mSearchResultClickListener = searchResultClickListener;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager llManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visible_threshold = 1;
int total_item_count = llManager.getItemCount();
int last_visible_item = llManager.findLastVisibleItemPosition();
if (!mIsLoading && total_item_count <= (last_visible_item + visible_threshold)) {
if (mPaginationListener != null) mPaginationListener.onPagination();
mIsLoading = true;
}
}
});
}
}
public void setmPaginationListener(PaginationListener paginationListener) {
mPaginationListener = paginationListener;
}
public void setmHorizontalPaginationListener(HorizontalPaginationListener horizontalPaginationListener) {
mHorizontalPaginationListener = horizontalPaginationListener;
}
public void setLoaded() {
mIsLoading = false;
}
public void setmRestaurantNumber(int restaurantNumber) {
mRestaurantNumber = restaurantNumber;
}
public void setmCurrentPage(int currentPage) {
mCurrentPage = currentPage;
}
public void addCuisine(String from) {
mFrom = from;
notifyDataSetChanged();
}
public void addCuisines(List<CuisineData> cuisines, String from) {
mFrom = from;
mCuisines.clear();
mCuisines = cuisines;
notifyDataSetChanged();
}
public void addRestaurants(List<Data> dataList) {
mShowNoItem = 0;
if(mCurrentPage == 1) notifyItemChanged(1);
if (mCurrentPage == 1) notifyItemChanged(2);
int insert_index = mDataList.size();
int pos_start = insert_index + mCuisineItem + mInfoItem;
mDataList.addAll(insert_index, dataList);
notifyItemRangeInserted(pos_start, dataList.size());
}
public void removeRestaurants() {
mShowNoItem = 1;
mDataList.clear();
mDataList = new ArrayList<>();
notifyItemRangeRemoved(mCuisineItem + mInfoItem, mDataList.size());
notifyItemChanged(2);
}
public void addProgressBar(Data data) {
mDataList.add(data);
notifyItemInserted(getItemCount() - 1);
}
public void removeProgressBar() {
if (mDataList.size() > 0) {
mDataList.remove(mDataList.size() - 1);
notifyItemRemoved(mCuisineItem + mInfoItem + mDataList.size());
}
}
public void removeAll() {
mDataList.clear();
mCuisines.clear();
mShowNoItem = 0;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return mDataList.size() + mShowNoItem + mCuisineItem + mInfoItem;
}
#Override
public int getItemViewType(int position) {
if (position == 0) return mViewType.VIEW_CUISINE;
if (position == 1) return mViewType.VIEW_INFO;
return mShowNoItem == 0 ? (mDataList.get(position - (mCuisineItem + mInfoItem)) != null ? mViewType.VIEW_RESTAURANT : mViewType.VIEW_PROGRESS)
: mViewType.VIEW_NO_DATA;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == mViewType.VIEW_CUISINE) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_cuisines, parent, false);
return new ViewHolderCuisinesList(mContext, view, mHorizontalPaginationListener);
}
if (viewType == mViewType.VIEW_INFO) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_restaurant_number, parent, false);
return new ViewHolderRestaurantNumber(mContext, view);
}
if (mShowNoItem == 0) {
if (viewType == mViewType.VIEW_RESTAURANT) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_item_nearest_restaurant, parent, false);
return new ViewHolderRestaurant(mContext, view, mSearchResultClickListener);
} else if (viewType == mViewType.VIEW_PROGRESS) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false);
return new ViewHolderProgress(view);
}
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_empty, parent, false);
return new ViewHolderEmpty(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolderSearch) {
((ViewHolderSearch) holder).onBind();
}
if (holder instanceof ViewHolderCuisinesList) {
if (!TextUtils.isEmpty(mFrom)) {
if (mFrom.equalsIgnoreCase(AppConstants.CUISINES))
((ViewHolderCuisinesList) holder).onBind(mCuisines);
else if (mFrom.equalsIgnoreCase(AppConstants.CUISINE))
((ViewHolderCuisinesList) holder).onBind_();
}
}
if (holder instanceof ViewHolderRestaurantNumber) {
((ViewHolderRestaurantNumber) holder).onBind(mRestaurantNumber);
}
if (mShowNoItem == 0) {
if (holder instanceof ViewHolderRestaurant) {
((ViewHolderRestaurant) holder).onBind(position - (mCuisineItem + mInfoItem), mDataList.get(position - (mCuisineItem + mInfoItem)));
} else if (holder instanceof ViewHolderProgress) {
((ViewHolderProgress) holder).onBind();
}
} else {
if (holder instanceof ViewHolderEmpty) {
((ViewHolderEmpty) holder).onBind(mContext.getString(R.string.no_result_found));
}
}
}
}

RecyclerView.Adapter shows 2 item in a single row (how to change that)

So I am working on a project which already has some code in it. I would like to change the way my adapter items are being shown.
Right now there are 2 items in each row. What I want to do is have only 1 item in each row.
Here is an image of how the image layout is right now.
2 items appear in each row while I need only 1 to appear.
Here is the code on the xmls
Item xml
<com.inthessaloniki.cityguide.view.SelectorRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_poi_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/selector_clickable_item_bg_inverse">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
</RelativeLayout>
List xml
<FrameLayout
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="match_parent"
android:animateLayoutChanges="true">
<LinearLayout
android:id="#+id/container_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment_poi_list_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical"
android:background="#color/global_bg_front" />
<com.google.android.gms.ads.AdView
android:id="#+id/fragment_poi_list_adview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:adUnitId="#string/admob_unit_id_poi_list"
app:adSize="BANNER" />
</LinearLayout>
<include
layout="#layout/placeholder_progress"
android:id="#+id/container_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<include
layout="#layout/placeholder_offline"
android:id="#+id/container_offline"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<include
layout="#layout/placeholder_empty"
android:id="#+id/container_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
ListAdapter code
public class SubCategoryListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
public static final int VIEW_TYPE_POI = 1;
public static final int VIEW_TYPE_IMAGE_FOOTER = 2;
public static final int VIEW_TYPE_FOOTER = 3;
private List<CategoryModel> mPoiList;
private List<Object> mFooterList;
private PoiViewHolder.OnItemClickListener mListener;
private int mGridSpanCount;
private Location mLocation;
private boolean mAnimationEnabled = true;
private int mAnimationPosition = -1;
private ImageLoader mImageLoader = ImageLoader.getInstance();
private DisplayImageOptions mDisplayImageOptions;
private ImageLoadingListener mImageLoadingListener;
//Ares
private long mCategory;
public SubCategoryListAdapter(List<CategoryModel> poiList, List<Object> footerList, PoiViewHolder.OnItemClickListener listener, int gridSpanCount, Location location, long category)
{
mPoiList = poiList;
mFooterList = footerList;
mListener = listener;
mGridSpanCount = gridSpanCount;
mLocation = location;
//Ares
mCategory = category;
// image caching options
mDisplayImageOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(android.R.color.transparent)
.showImageForEmptyUri(R.drawable.placeholder_photo)
.showImageOnFail(R.drawable.placeholder_photo)
.cacheInMemory(true)
.cacheOnDisk(true)
.displayer(new SimpleBitmapDisplayer())
.build();
mImageLoadingListener = new AnimateImageLoadingListener();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// inflate view and create view holder
View view;
if(viewType== VIEW_TYPE_POI)
{
if(mCategory == -4) {
view = inflater.inflate(R.layout.fragment_poi_list_item_home, parent, false);
}
else
{
view = inflater.inflate(R.layout.fragment_poi_list_item, parent, false);
}
return new PoiViewHolder(view, mListener, mImageLoader, mDisplayImageOptions, mImageLoadingListener, mCategory);
}
else if(viewType==VIEW_TYPE_IMAGE_FOOTER)
{
view = inflater.inflate(R.layout.footer_image_layout, parent, false);
return new ImageFooterViewHolder(view, mListener);
}
else if(viewType==VIEW_TYPE_FOOTER)
{
view = inflater.inflate(R.layout.fragment_poi_list_footer, parent, false);
return new FooterViewHolder(view);
}
else
{
throw new RuntimeException("There is no view type that matches the type " + viewType);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
// bind data
if(viewHolder instanceof PoiViewHolder)
{
// entity
CategoryModel poi = mPoiList.get(getPoiPosition(position));
// render view
if(poi != null)
{
((PoiViewHolder) viewHolder).bindData(poi, mLocation, mCategory);
}
}
else if(viewHolder instanceof FooterViewHolder)
{
// entity
Object object = mFooterList.get(getFooterPosition(position));
// render view
if(object != null)
{
((FooterViewHolder) viewHolder).bindData(object);
}
}
// set item margins
setItemMargins(viewHolder.itemView, position);
// set animation
setAnimation(viewHolder.itemView, position);
}
#Override
public int getItemCount()
{
int size = 0;
if(mPoiList !=null) size += mPoiList.size();
if(mFooterList!=null) size += mFooterList.size();
if(mFooterList!=null) size += 1; //ImageFooter
return size;
}
#Override
public int getItemViewType(int position)
{
int pois = mPoiList.size();
int footers = mFooterList.size();
int imageFooter = 1;
if(position < pois) return VIEW_TYPE_POI;
else if(position < pois+imageFooter) return VIEW_TYPE_IMAGE_FOOTER;
else if(position < pois+imageFooter+footers) return VIEW_TYPE_FOOTER;
else return -1;
}
public int getPoiCount()
{
if(mPoiList !=null) return mPoiList.size();
return 0;
}
public int getFooterCount()
{
if(mFooterList!=null) return mFooterList.size();
return 0;
}
public int getPoiPosition(int recyclerPosition)
{
return recyclerPosition;
}
public int getFooterPosition(int recyclerPosition)
{
return recyclerPosition - getPoiCount();
}
public int getRecyclerPositionByPoi(int poiPosition)
{
return poiPosition;
}
public int getRecyclerPositionByFooter(int footerPosition)
{
return footerPosition + getPoiCount();
}
public void refill(List<CategoryModel> poiList, List<Object> footerList, PoiViewHolder.OnItemClickListener listener, int gridSpanCount, Location location)
{
mPoiList = poiList;
mFooterList = footerList;
mListener = listener;
mGridSpanCount = gridSpanCount;
mLocation = location;
notifyDataSetChanged();
}
public void stop()
{
}
public void setLocation(Location location)
{
mLocation = location;
}
public void setAnimationEnabled(boolean animationEnabled)
{
mAnimationEnabled = animationEnabled;
}
private void setAnimation(final View view, int position)
{
if(mAnimationEnabled && position>mAnimationPosition)
{
view.setScaleX(0f);
view.setScaleY(0f);
view.animate()
.scaleX(1f)
.scaleY(1f)
.setDuration(300)
.setInterpolator(new DecelerateInterpolator());
mAnimationPosition = position;
}
}
private void setItemMargins(View view, int position)
{
int height = 0;
if(mCategory != -4)
height = (int) CityGuideApplication.getContext().getResources().getDimension(R.dimen.fragment_poi_list_recycler_item_size);
int marginTop = 0;
if(position<mGridSpanCount)
{
TypedArray a = CityGuideApplication.getContext().obtainStyledAttributes(null, new int[]{android.R.attr.actionBarSize}, 0, 0);
marginTop = (int) a.getDimension(0, 0);
a.recycle();
if(mCategory != -4)
height += marginTop;
}
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
marginParams.setMargins(0, marginTop, 0, 0);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if(mCategory != -4)
layoutParams.height = height;
}
public static final class PoiViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView nameTextView;
private TextView distanceTextView;
private ImageView imageView;
private OnItemClickListener mListener;
private ImageLoader mImageLoader;
private DisplayImageOptions mDisplayImageOptions;
private ImageLoadingListener mImageLoadingListener;
public interface OnItemClickListener
{
public void onItemClick(View view, int position, long id, int viewType);
}
public PoiViewHolder(View itemView, OnItemClickListener listener, ImageLoader imageLoader, DisplayImageOptions displayImageOptions, ImageLoadingListener imageLoadingListener, long category)
{
super(itemView);
mListener = listener;
mImageLoader = imageLoader;
mDisplayImageOptions = displayImageOptions;
mImageLoadingListener = imageLoadingListener;
// set listener
itemView.setOnClickListener(this);
// find views
if(category == -4)
{
Log.d("Category Status: ", String.valueOf(category));
}
else
{
nameTextView = (TextView) itemView.findViewById(R.id.fragment_poi_list_item_name);
distanceTextView = (TextView) itemView.findViewById(R.id.fragment_poi_list_item_distance);
distanceTextView.setVisibility(View.GONE);
}
imageView = (ImageView) itemView.findViewById(R.id.fragment_poi_list_item_image);
}
#Override
public void onClick(View view)
{
mListener.onItemClick(view, getPosition(), getItemId(), getItemViewType());
}
public void bindData(CategoryModel subCategory, Location location, long category)
{
if(category == -4)
{
}
else
{
nameTextView.setText(subCategory.getName());
}
mImageLoader.displayImage(subCategory.getImage2(), imageView, mDisplayImageOptions, mImageLoadingListener);
// if(location!=null)
// {
// LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
// LatLng poiLocation = new LatLng(poi.getLatitude(), poi.getLongitude());
// String distance = LocationUtility.getDistanceString(LocationUtility.getDistance(myLocation, poiLocation), LocationUtility.isMetricSystem());
// distanceTextView.setText(distance);
// distanceTextView.setVisibility(View.VISIBLE);
// }
// else
// {
// distanceTextView.setVisibility(View.GONE);
// }
}
}
public static final class ImageFooterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private PoiViewHolder.OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position, long id, int viewType);
}
public ImageFooterViewHolder(View itemView, PoiViewHolder.OnItemClickListener listener) {
super(itemView);
mListener = listener;
// set listener
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
mListener.onItemClick(view, getPosition(), getItemId(), getItemViewType());
}
public void bindData(PoiModel poi, Location location) {
}
}
public static final class FooterViewHolder extends RecyclerView.ViewHolder
{
public FooterViewHolder(View itemView)
{
super(itemView);
}
public void bindData(Object object)
{
// do nothing
}
}
}
GritLayout
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration
{
private int mSpacing;
public GridSpacingItemDecoration(int spacingPixelSize)
{
mSpacing = spacingPixelSize;
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state)
{
super.getItemOffsets(outRect, view, recyclerView, state);
int position = recyclerView.getChildPosition(view);
int spanCount = getSpanCount(recyclerView);
int itemCount = recyclerView.getAdapter().getItemCount();
// top offset
if(position < spanCount)
{
outRect.top = mSpacing;
}
else
{
outRect.top = mSpacing/2;
}
// bottom offset
if(itemCount%spanCount == 0 && position >= itemCount - spanCount)
{
outRect.bottom = mSpacing;
}
else if(itemCount%spanCount != 0 && position >= itemCount - itemCount%spanCount)
{
outRect.bottom = mSpacing;
}
else
{
outRect.bottom = mSpacing/2;
}
// left offset
if(position%spanCount == 0)
{
outRect.left = mSpacing;
}
else
{
outRect.left = mSpacing/2;
}
// right offset
if(position%spanCount == spanCount-1)
{
outRect.right = mSpacing;
}
else
{
outRect.right = mSpacing/2;
}
}
private int getSpanCount(RecyclerView recyclerView)
{
if(recyclerView.getLayoutManager() instanceof GridLayoutManager)
{
GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
return gridLayoutManager.getSpanCount();
}
else
{
throw new IllegalStateException(this.getClass().getSimpleName() + " can only be used with a " + GridLayoutManager.class.getSimpleName());
}
}
}
I don't think that the problem is in the XML files. It must be somewhere in the code. I just can't track it down and modify it... :S
I've been searching for hours.
Thanks in advance!!!
You have not posted the piece of code where you are setting LayoutManager to your RecyclerView.
Is seems you are using GridLayoutManager:
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 2);
myRecyclerView.setLayoutManager(mLayoutManager);
Try changing it to LinearLayoutManager:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
myRecyclerView.setLayoutManager(mLayoutManager);

i am trying to set a header to staggardGridLayoutManeger

i am trying to set a header to staggardGridLayoutManeger i have the code from this url https://github.com/willblaschko/header-footer-recycler-view. this code contains header and footer for all the types of layout. can anyone help to set the adapter only for staggardGridLayout this is the code form the sample at
enter code here
public class RecyclerViewHeaderFooterAdapter extends RecyclerView.Adapter {
public static final int TYPE_MANAGER_OTHER = 0;
public static final int TYPE_MANAGER_LINEAR = 1;
public static final int TYPE_MANAGER_GRID = 2;
public static final int TYPE_MANAGER_STAGGERED_GRID = 3;
public static final int TYPE_HEADER = 7898;
public static final int TYPE_FOOTER = 7899;
private List<View> mHeaders = new ArrayList<>();
private List<View> mFooters = new ArrayList<>();
private int mManagerType;
private RecyclerView.LayoutManager mManager;
private IRecyclerViewIntermediary mIntermediary;
public RecyclerViewHeaderFooterAdapter(RecyclerView.LayoutManager manager, IRecyclerViewIntermediary intermediary){
setManager(manager);
this.mIntermediary = intermediary;
}
public void setLayoutManager(RecyclerView.LayoutManager manager){
setManager(manager);
}
private void setManager(RecyclerView.LayoutManager manager){
mManager = manager;
if(mManager instanceof GridLayoutManager){
mManagerType = TYPE_MANAGER_GRID;
((GridLayoutManager) mManager).setSpanSizeLookup(mSpanSizeLookup);
}else if(mManager instanceof LinearLayoutManager){
mManagerType = TYPE_MANAGER_LINEAR;
}else if(mManager instanceof StaggeredGridLayoutManager){
mManagerType = TYPE_MANAGER_STAGGERED_GRID;
((StaggeredGridLayoutManager) mManager).setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
}else{
mManagerType = TYPE_MANAGER_OTHER;
}
}
public int getManagerType(){
return mManagerType;
}
public int getGridSpan(int position){
if(isHeader(position) || isFooter(position)){
return getSpan();
}
position -= mHeaders.size();
if(mIntermediary.getItem(position) instanceof IGridItem){
return ((IGridItem) mIntermediary.getItem(position)).getGridSpan();
}
return 1;
}
private int getSpan(){
if(mManager instanceof GridLayoutManager){
return ((GridLayoutManager) mManager).getSpanCount();
}else if(mManager instanceof StaggeredGridLayoutManager){
return ((StaggeredGridLayoutManager) mManager).getSpanCount();
}
return 1;
}
private GridLayoutManager.SpanSizeLookup mSpanSizeLookup = new GridLayoutManager.SpanSizeLookup(){
#Override
public int getSpanSize(int position) {
return getGridSpan(position);
}
};
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
//if our position is one of our items (this comes from getItemViewType(int position) below)
if(type != TYPE_HEADER && type != TYPE_FOOTER) {
return mIntermediary.getViewHolder(viewGroup, type);
//else we have a header/footer
}else{
//create a new framelayout, or inflate from a resource
FrameLayout frameLayout = new FrameLayout(viewGroup.getContext());
//make sure it fills the space
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return new HeaderFooterViewHolder(frameLayout);
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder vh, int position) {
//check what type of view our position is
if(isHeader(position)){
View v = mHeaders.get(position);
//add our view to a header view and display it
prepareHeaderFooter((HeaderFooterViewHolder) vh, v);
}else if(isFooter(position)){
View v = mFooters.get(position-mIntermediary.getItemCount()-mHeaders.size());
//add our view to a footer view and display it
prepareHeaderFooter((HeaderFooterViewHolder) vh, v);
}else {
//it's one of our items, display as required
mIntermediary.populateViewHolder(vh, position-mHeaders.size());
}
}
private void prepareHeaderFooter(HeaderFooterViewHolder vh, View view){
//if it's a staggered grid, span the whole layout
if(mManagerType == TYPE_MANAGER_STAGGERED_GRID){
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.setFullSpan(true);
vh.itemView.setLayoutParams(layoutParams);
}
//if the view already belongs to another layout, remove it
if(view.getParent() != null){
((ViewGroup)view.getParent()).removeView(view);
}
//empty out our FrameLayout and replace with our header/footer
vh.base.removeAllViews();
vh.base.addView(view);
}
private boolean isHeader(int position){
return(position < mHeaders.size());
}
private boolean isFooter(int position){
return(position >= mHeaders.size() + mIntermediary.getItemCount());
}
#Override
public int getItemCount() {
return mHeaders.size() + mIntermediary.getItemCount() + mFooters.size();
}
#Override
public int getItemViewType(int position) {
//check what type our position is, based on the assumption that the order is headers > items > footers
if(isHeader(position)){
return TYPE_HEADER;
}else if(isFooter(position)){
return TYPE_FOOTER;
}
int type = mIntermediary.getItemViewType(position);
if(type == TYPE_HEADER || type == TYPE_FOOTER){
throw new IllegalArgumentException("Item type cannot equal "+TYPE_HEADER+" or "+TYPE_FOOTER);
}
return type;
}
//add a header to the adapter
public void addHeader(View header){
if(!mHeaders.contains(header)){
mHeaders.add(header);
//animate
notifyItemInserted(mHeaders.size()-1);
}
}
//remove a header from the adapter
public void removeHeader(View header){
if(mHeaders.contains(header)){
//animate
notifyItemRemoved(mHeaders.indexOf(header));
mHeaders.remove(header);
}
}
//add a footer to the adapter
public void addFooter(View footer){
if(!mFooters.contains(footer)){
mFooters.add(footer);
//animate
notifyItemInserted(mHeaders.size()+mIntermediary.getItemCount()+mFooters.size()-1);
}
}
//remove a footer from the adapter
public void removeFooter(View footer){
if(mFooters.contains(footer)) {
//animate
notifyItemRemoved(mHeaders.size()+mIntermediary.getItemCount()+mFooters.indexOf(footer));
mFooters.remove(footer);
}
}
//our header/footer RecyclerView.ViewHolder is just a FrameLayout
public static class HeaderFooterViewHolder extends RecyclerView.ViewHolder{
FrameLayout base;
public HeaderFooterViewHolder(View itemView) {
super(itemView);
base = (FrameLayout) itemView;
}
}
}
I solved this problem adding a new type to my adapter for the header. Look at this example: https://stackoverflow.com/a/26573338/1949163.
After that, to make the header span all columns I created a new LayoutParams and set full span to true in the adapter implementation:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof CommonItemViewHolder) {
...
} else if (holder instanceof HeaderViewHolder) {
...
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
}
}

Categories

Resources