Problems with TransitionManager beginDelayedTransition for RecyclerView's item view - java

I have the following code:
class ProductViewHolder extends RecyclerView.ViewHolder {
...
private Transition transition;
ProductViewHolder(CardViewEx view) {
super(view);
...
transition = TransitionInflater.from(recyclerView.getContext())
.inflateTransition(R.transition.price_button_transition);
transition.addTarget(R.id.button_price)
.addTarget(R.id.button_add_to_cart_or_customize)
.addTarget(R.id.view_price);
}
void changePriceButton(int titleRes) {
TransitionManager.beginDelayedTransition((ViewGroup) itemView, transition);
buttonPrice.setVisibility(View.GONE);
buttonAddToCartOrCustomize.setVisibility(View.VISIBLE);
buttonAddToCartOrCustomize.setText(titleRes);
}
void resetPriceButton(boolean withTransition) {
if (withTransition) {
TransitionManager.beginDelayedTransition((ViewGroup) itemView, transition);
}
buttonPrice.setVisibility(View.VISIBLE);
buttonAddToCartOrCustomize.setVisibility(View.GONE);
}
#OnClick(R.id.button_price)
void onPriceClicked() {
listener.onProductPriceClicked(product);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
resetPriceButton(false);
}
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
resetPriceButtons();
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
}
void resetPriceButtons() {
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
ProductViewHolder holder = (ProductViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
if (holder == null || holder.getLayoutPosition() != i) {
// inconsistency
notifyDataSetChanged();
return;
} else {
holder.resetPriceButton(true);
}
}
notifyItemRangeChanged(0, firstVisibleItemPosition);
notifyItemRangeChanged(lastVisibleItemPosition + 1, getItemCount() - (lastVisibleItemPosition + 1));
}
void changePriceButton(long productId, int titleRes) {
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
boolean found = false;
for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
ProductViewHolder holder = (ProductViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
if (holder == null || holder.getLayoutPosition() != i) {
// inconsistency
notifyDataSetChanged();
return;
} else if (holder.product.getId() == productId) {
holder.changePriceButton(titleRes, true);
found = true;
}
}
if (!found) {
notifyItemRangeChanged(0, firstVisibleItemPosition);
notifyItemRangeChanged(lastVisibleItemPosition + 1, getItemCount() - (lastVisibleItemPosition + 1));
}
}
RecyclerView's item layout:
...
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginBottom="8dp">
<ImageView android:id="#+id/image_product"
android:layout_width="64dp" android:layout_height="64dp"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"/>
<FrameLayout android:id="#+id/view_price"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp" android:layout_marginRight="8dp">
<Button style="#style/Button.Border.Green"
android:id="#+id/button_price"
android:minWidth="76dp"/>
<Button style="#style/Button.Flat.Green"
android:id="#+id/button_add_to_cart_or_customize"
android:layout_width="wrap_content"
android:minWidth="76dp"
android:minHeight="48dp"
android:textSize="13sp"/>
</FrameLayout>
<TextView android:id="#+id/text_product"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="#id/image_product" android:layout_toLeftOf="#id/view_price"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:textAppearance="#style/TextAllerRegular.13sp.Black87per"/>
</RelativeLayout>
I want to animate change price button to "Add to cart" when user clicks on the price, and back to the price when user starts scrolling RecyclerView.
When user clicks buttons and scroll recyclerview, button animations can stop in the middle state. You will see the margin between the right side of the window and the price button, or text "Add to cart" without the right padding on the button.
Then the button will be broken, you can try changePriceButton/resetPriceButton on it, but button won't show fully.
It is easy to reproduce this bug using "Animator duration scale" >= 5x.
It seems that TransitionManager has problems when you start beginDelayedTransition when there is beginDelayedTransition that already started on this view.
Also I think TransitionManager has problems with RecyclerView's view recycling on scrolling. Tried TransitionManager.endTransitions((ViewGroup) itemView) at the beginning of onBindViewHolder, but it didn't help.
The following solves the problem for me, but I am not sure fully.
Good to investigate the problem's reason.
I add the field isResetPriceButton in RecyclerView's holder for preventing attempts to change button to the same state when user starts scrolling RecycleView
private boolean isResetPriceButton;
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
isResetPriceButton = false;
...
resetPriceButton(false);
}
void changePriceButton(int titleRes) {
if (isResetPriceButton) {
isResetPriceButton = false;
TransitionManager.beginDelayedTransition((ViewGroup) itemView);
buttonPrice.setVisibility(View.GONE);
buttonAddToCartOrCustomize.setVisibility(View.VISIBLE);
buttonAddToCartOrCustomize.setText(titleRes);
}
}
void resetPriceButton(boolean withTransition) {
if (!isResetPriceButton) {
isResetPriceButton = true;
if (withTransition) {
TransitionUtils.beginDelayedTransitionIfPossible((ViewGroup) itemView);
}
buttonPrice.setVisibility(View.VISIBLE);
buttonAddToCartOrCustomize.setVisibility(View.GONE);
}
}
And I use the setHasTransientState on the holder's view during transition. But I don't like this, it produces extra views on scrolling when transitions have been started.
transition.addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {
itemView.setHasTransientState(true);
}
#Override
public void onTransitionEnd(Transition transition) {
itemView.setHasTransientState(false);
}
#Override
public void onTransitionCancel(Transition transition) {
itemView.setHasTransientState(false);
}
#Override
public void onTransitionPause(Transition transition) {
}
#Override
public void onTransitionResume(Transition transition) {
}
});

Related

how to callback from fragment to activity With bottom sheet

Hey I'm doing filtering data on my project using bottom Sheet with view-pager and two fragments i can pass data from activity to view-pager adapter and get in fragment through constructor but how can i get data from fragment to activity using bottom-sheet
this is how i send data from activity
adapter = new PagerAdapters(this.getSupportFragmentManager(),
tabLayout.getTabCount(),_id);
and receive data with constructor in fragment
public Filterfragment(String _id) {
this._id = _id;
}
And I'm using recyclerview to show data like this
So how can i send id of the item to activity so i can reload my recyclerview data when the apply button click.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottom_sheet"
android:background="#android:color/white"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabPadding="0dp"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_below="#id/tabs"
android:layout_above="#id/applyBtn"
android:layout_height="match_parent"
/>
<Button
android:id="#+id/applyBtn"
android:layout_width="match_parent"
android:text="Apply"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</RelativeLayout>
This is main recyclerview adapter
public class FilterAdapter extends RecyclerView.Adapter<FilterAdapter.FilterViewHolder> {
private static final String TAG = "FilterAdapter";
Context context;
List<TagTypeResult> tagTypeModels;
public static int current_pos = -1;
TagAdapter tagAdapter;
int rotationAngle = 0;
public FilterAdapter(Context context, List<TagTypeResult> tagTypeModels) {
this.context = context;
this.tagTypeModels = tagTypeModels;
}
#NonNull
#Override
public FilterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_row_item,parent,false);
return new FilterAdapter.FilterViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull FilterViewHolder filterViewHolder, int i) {
TagTypeResult tagTypeModel = tagTypeModels.get(i);
filterViewHolder.txt.setText(tagTypeModel.getName());
filterViewHolder.sub_list_recycler.setVisibility(View.GONE);
if (tagTypeModel.getTagsLists() != null) {
if (tagTypeModel.getTagsLists().size() <= 0) {
filterViewHolder.arrow.setVisibility(View.GONE);
filterViewHolder.arrow.setRotationX(180);
} else {
filterViewHolder.arrow.setVisibility(View.VISIBLE);
filterViewHolder.arrow.setRotationX(0);
}
}else {
filterViewHolder.arrow.setVisibility(View.GONE);
}
tagAdapter = new TagAdapter(context, tagTypeModel.getTagsLists(), new TagInterface() {
#Override
public void tagClick(View view, int pos, String tagID) {
Log.d(TAG, "tagClick: "+tagID);
}
});
filterViewHolder.sub_list_recycler.setAdapter(tagAdapter);
tagAdapter.notifyDataSetChanged();
if (current_pos == filterViewHolder.getAdapterPosition()){
if (filterViewHolder.sub_list_recycler.getVisibility() == View.GONE) {
filterViewHolder.sub_list_recycler.setVisibility(View.VISIBLE);
}else {
filterViewHolder.sub_list_recycler.setVisibility(View.GONE);
}
}else {
Log.i(TAG, "onBindViewHolder: sublist gone "+tagTypeModel.getName());
filterViewHolder.sub_list_recycler.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return tagTypeModels.size();
}
class FilterViewHolder extends RecyclerView.ViewHolder {
TextView txt;
ImageView arrow;
RecyclerView sub_list_recycler;
RelativeLayout linearLayout;
FilterViewHolder(#NonNull View itemView) {
super(itemView);
txt = itemView.findViewById(R.id.txt);
arrow = itemView.findViewById(R.id.arrow);
linearLayout = itemView.findViewById(R.id.main_cat_lay);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "onClick: "+current_pos);
if (current_pos != getAdapterPosition()) {
current_pos = getAdapterPosition();
notifyDataSetChanged();
}
else{
current_pos = -1;
notifyDataSetChanged();
}
}
});
sub_list_recycler = itemView.findViewById(R.id.sub_list_recycler);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context) {
#Override
public boolean canScrollVertically() {
return false;
}
};
sub_list_recycler.setLayoutManager(mLayoutManager);
sub_list_recycler.addItemDecoration(new SimpleDividerItemDecoration(context));
}
}
}
sub recyclerView adapter :
public class TagAdapter extends RecyclerView.Adapter<TagAdapter.TagViewHolder> {
private static final String TAG = "SublistAdapter";
Context context;
List<TagsList> tagsLists;
int pos = -1;
TagInterface tagInterface;
public TagAdapter(Context context, List<TagsList> tagsLists,TagInterface tagInterface) {
this.context = context;
this.tagsLists = tagsLists;
this.tagInterface = tagInterface;
}
#NonNull
#Override
public TagViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tag_item,parent,false);
return new TagAdapter.TagViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull TagViewHolder tagViewHolder, int i) {
TagsList tagsList = tagsLists.get(i);
tagViewHolder.tagtxt.setText(tagsList.getName());
if (tagsList.isChecked()){
tagViewHolder.tagName.setChecked(true);
}else {
tagViewHolder.tagName.setChecked(false);
}
}
#Override
public int getItemCount() {
return tagsLists.size();
}
public class TagViewHolder extends RecyclerView.ViewHolder {
TextView tagtxt;
CheckBox tagName;
RelativeLayout childClik;
public TagViewHolder(#NonNull View itemView) {
super(itemView);
//pri_txt = itemView.findViewById(R.id.pri_txt);
tagName = itemView.findViewById(R.id.tagName);
tagtxt = itemView.findViewById(R.id.tagtxt);
childClik = itemView.findViewById(R.id.childClik);
tagName.setEnabled(false);
childClik.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isChecked = true;
if (isChecked)
{
tagsLists.get(getAdapterPosition()).setChecked(true);
tagName.setChecked(true);
tagInterface.tagClick(v,getAdapterPosition(),tagsLists.get(getAdapterPosition()).get_id());
Toast.makeText(context, "[pos]"+tagsLists.get(getAdapterPosition()).get_id(), Toast.LENGTH_SHORT).show();
}else
{
tagsLists.get(getAdapterPosition()).setChecked(false);
tagName.setChecked(false);
}
}
});
}
}
}

Android Databinding in ExpandableListView

I have a problem in expandableListView that is weird with databinding.There is textview of quantity which can be incremented/decremented on button click.The Textview correctly updates at specific position. But when scrolled down the value changes the to textview at different position. Also , when group expanded value move the below item textview.Below are adapter , xml and model class.
public class ProductExpandableListAdapter extends BaseExpandableListAdapter {
private Activity contextActivity;
private ArrayList<ProductItems> productItemsArrayList;
public ProductExpandableListAdapter(ProductActivity productActivity, ArrayList<ProductItems> productItemsArrayList) {
this.contextActivity = productActivity;
this.productItemsArrayList = productItemsArrayList;
}
#Override
public void registerDataSetObserver(DataSetObserver dataSetObserver) {
}
#Override
public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
}
#Override
public int getGroupCount() {
return this.productItemsArrayList.size();
}
#Override
public int getChildrenCount(int i) {
return 1;
}
#Override
public Object getGroup(int i) {
return this.productItemsArrayList.get(i);
}
#Override
public Object getChild(int i, int i1) {
return this.productItemsArrayList.get(i).getProductItemDescription();
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final ProductItems productItems = productItemsArrayList.get(i);
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
final ProductItemBinding productItemsBinding = DataBindingUtil.inflate(layoutInflater, R.layout.product_items, viewGroup, false);
HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productTitleTextView);
productItemsBinding.productTitleTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_64_pt));
view = productItemsBinding.getRoot();
HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productPriceTextView);
productItemsBinding.productPriceTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_72_pt));
HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.qtyTextView);
productItemsBinding.qtyTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_104_pt));
productItemsBinding.setHandler(new QuantityHandler() {
#Override
public void onQuantityIncrement() {
changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.ADDITION);
Toast.makeText(contextActivity, "quantityIncrement : ", Toast.LENGTH_LONG).show();
}
#Override
public void onQuantityDecrement() {
changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.SUBTRACTION);
Toast.makeText(contextActivity, "quantityDecrement : ", Toast.LENGTH_LONG).show();
}
});
productItemsBinding.setProductItems(productItems);
}
return view;
}
#Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
final ProductItemsChildBinding productItemsChildBinding = DataBindingUtil.inflate(layoutInflater, R.layout.product_items_child, viewGroup, false);
HelveticaNeueFontHelper.applyHelveticaneuelightFont(contextActivity, productItemsChildBinding.productDescriptionTextView);
productItemsChildBinding.productDescriptionTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_64_pt));
productItemsChildBinding.setProductItems(productItemsArrayList.get(i));
view = productItemsChildBinding.getRoot();
}
return view;
}
#Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEmpty() {
return false;
}
#Override
public void onGroupExpanded(int i) {
}
#Override
public void onGroupCollapsed(int i) {
}
#Override
public long getCombinedChildId(long l, long l1) {
return 0;
}
#Override
public long getCombinedGroupId(long l) {
return 0;
}
private void changeQuantity(final ProductItemBinding productItemsBinding, ProductItems productItems, ObservableInt observableIntQty, String operand) {
switch (operand) {
case Constants.ADDITION:
if (observableIntQty.get() >= 0) {
observableIntQty.set(observableIntQty.get() + 1);
}
break;
case Constants.SUBTRACTION:
if (observableIntQty.get() > 0) {
observableIntQty.set(observableIntQty.get() - 1);
}
break;
}
contextActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
productItemsBinding.executePendingBindings();
}
});
}
}
<data class="ProductItemBinding">
<variable
name="productItems"
type="com.vtrio.waterapp.data.ProductItems" />
<variable
name="handler"
type="com.vtrio.waterapp.listeners.QuantityHandler" />
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/rounded_corner_bluegradient"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="#+id/productImageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
app:imgSrc="#{productItems.productItemImage}" />
<LinearLayout
android:id="#+id/productInfoLinearLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="0.45"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/productTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{productItems.productItemTitleName}" />
<TextView
android:id="#+id/productPriceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{productItems.productItemPrice}" />
</LinearLayout>
<RelativeLayout
android:id="#+id/quantityModRelativeLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_weight="0.25"
android:gravity="center">
<Button
android:id="#+id/subtractQtyButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/addsubColor"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="#{(v) -> handler.onQuantityDecrement()}"
android:text="#string/sub"
android:textColor="#color/addsubTextColor" />
<TextView
android:id="#+id/qtyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#{Integer.toString(productItems.productItemQty)}" />
<Button
android:id="#+id/addQtyButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#color/addsubColor"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="#{(v) -> handler.onQuantityIncrement()}"
android:text="#string/add"
android:textColor="#color/addsubTextColor" />
</RelativeLayout>
</LinearLayout> </layout>
ProductItems
public class ProductItems extends BaseObservable {
private String productItemTitleName;
private String productItemPrice;
private int productItemImage;
private String productItemDescription;
private ObservableInt productItemQty;
private ArrayList<ProductItems> cartItem;
public ProductItems(String productItemTitleName, String productItemPrice, int productItemImage, String productItemDescription, ObservableInt productItemQty) {
this.productItemTitleName = productItemTitleName;
this.productItemPrice = productItemPrice;
this.productItemImage = productItemImage;
this.productItemDescription = productItemDescription;
this.productItemQty = productItemQty;
}
public String getProductItemTitleName() {
return productItemTitleName;
}
public void setProductItemTitleName(String productItemTitleName) {
this.productItemTitleName = productItemTitleName;
}
public String getProductItemPrice() {
return productItemPrice;
}
public void setProductItemPrice(String productItemPrice) {
this.productItemPrice = productItemPrice;
}
public int getProductItemImage() {
return productItemImage;
}
public void setProductItemImage(int productItemImage) {
this.productItemImage = productItemImage;
}
public String getProductItemDescription() {
return productItemDescription;
}
public void setProductItemDescription(String productItemDescription) {
this.productItemDescription = productItemDescription;
}
#Bindable
public ObservableInt getProductItemQty() {
return productItemQty;
}
public void setProductItemQty(ObservableInt productItemQty) {
this.productItemQty = productItemQty;
notifyPropertyChanged(BR.productItemQty);
}
What is the problem and what can be done to solve this.
Maybe the problem is solved already but if not, I would change the getGroupView(...) method (and getChildView likewise) for the case when view is not null like this:
#Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final ProductItems productItems = productItemsArrayList.get(i);
final ProductItemBinding productItemsBinding;
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
productItemsBinding = DataBindingUtil.inflate(layoutInflater, R.layout.product_items, viewGroup, false);
HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productTitleTextView);
productItemsBinding.productTitleTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_64_pt));
view = productItemsBinding.getRoot();
HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productPriceTextView);
productItemsBinding.productPriceTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_72_pt));
HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.qtyTextView);
productItemsBinding.qtyTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_104_pt));
}
else
{
productItemsBinding = (ProductItemBinding) view.getTag();
}
productItemsBinding.setHandler(new QuantityHandler() {
#Override
public void onQuantityIncrement() {
changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.ADDITION);
Toast.makeText(contextActivity, "quantityIncrement : ", Toast.LENGTH_LONG).show();
}
#Override
public void onQuantityDecrement() {
changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.SUBTRACTION);
Toast.makeText(contextActivity, "quantityDecrement : ", Toast.LENGTH_LONG).show();
}
});
productItemsBinding.setProductItems(productItems);
view.setTag(productItemsBinding);
return view;
}

Fix blinking effect when scrolling down Recyclerview with custom behaviour

I have a code that is experiencing a weird behaviour when scrolling the view down the second time. Basically, the app bar layout should resize the vertical space when swiping down and the user should be able to see the entire image. But when swiping down the second time the screen starts blinking super fast. Any help it'll be greatly appreciated.
ItemDetailFragment.java
public class ItemDetailFragment extends BaseFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = ItemDetailFragment.class.getSimpleName();
private static final int LOAD_ITEM = 1002;
#BindView(R.id.photo)
ImageView mItemImageView;
#BindView(R.id.rv_item)
RecyclerView rvItem;
#BindView(R.id.toolbar)
Toolbar toolbar;
private Unbinder unbinder;
private int currentId;
private Cursor mCursor;
private ItemDetailAdapter mAdapter;
private ShareActionProvider mShareActionProvider;
public static Fragment newInstance(int id) {
ItemDetailFragment detailFragment = new ItemDetailFragment();
Bundle args = new Bundle();
args.putInt(Commons.ITEM_ID_KEY, id);
detailFragment.setArguments(args);
return detailFragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
currentId = getArguments().getInt(Commons.ITEM_ID_KEY);
if(isVisible()){
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
rvItem.setLayoutManager(mLayoutManager);
mAdapter = new ItemDetailAdapter();
rvItem.setAdapter(mAdapter);
getLoaderManager().restartLoader(LOAD_ITEM, null, this);
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
setHasOptionsMenu(isVisible());
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_share, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_item_share:
Log.d(TAG, "Share");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return ItemLoader.loadItemById(getContext(), currentId);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (!isAdded()) {
if (cursor != null) {
cursor.close();
}
return;
}
mCursor = cursor;
if (mCursor != null && !mCursor.moveToFirst()) {
Log.e(TAG, "Error reading item detail cursor");
mCursor.close();
mCursor = null;
}
String title = mCursor.getString(ItemLoader.Query.TITLE);
float price = 0;
String excerpt = mCursor.getString(ItemLoader.Query.EXCERPT);
mAdapter.swap(new Item(title, price, excerpt));
loadItemView();
}
private void loadItemView() {
if (mCursor != null){
BindingUtils.loadImage(mItemImageView, mCursor.getString(ItemLoader.Query.PHOTO_URL));
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursor = null;
loadItemView();
}
}
fragment_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:clipToPadding="false"
app:layout_behavior="la.disfruta.disfruta.custom.behaviour.VerticalScrollingBehaviour"/>
<android.support.design.widget.AppBarLayout
android:id="#+id/barLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_layout_height"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="#color/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:fitsSystemWindows="true"
android:background="#color/photo_placeholder"
android:contentDescription="#string/app_name"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_gravity="bottom"
app:layout_collapseMode="pin">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/logo" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
VerticalScrollingBehaviour.java
public class VerticalScrollingBehaviour extends AppBarLayout.ScrollingViewBehavior {
private static final String TAG = "Behaviour";
private View mTargetView;
private View mBanner;
private View mToolbar;
private int mInitialHeight;
private int mExtHeight;
private int mLastHeight;
public VerticalScrollingBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalScrollingBehaviour() {
super();
}
#Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof AppBarLayout;
}
#Override
public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
boolean layoutHandler = super.onLayoutChild(parent, child, layoutDirection);
if(mTargetView == null){
mTargetView = parent.findViewById(R.id.barLayout);
mBanner = parent.findViewById(R.id.photo);
mToolbar = parent.findViewById(R.id.toolbar);
if(mTargetView != null){
init(child);
}
}
return layoutHandler;
}
#Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target,
int dx, int dy, int[] consumed) {
mExtHeight = mBanner.getHeight() + mToolbar.getHeight();
if (mTargetView != null &&
(dy < 0 && ( child.getTop() >= mInitialHeight && child.getTop() < mExtHeight) ||
(dy > 0 && ( child.getTop() > mInitialHeight && child.getTop() <= mExtHeight) ) )){
Log.d(TAG, "onNestedPreScroll - Custom Behaviour");
resize(dy);
} else {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
}
}
#Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, View child, View target,
float velocityX, float velocityY) {
return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
}
#Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child,
View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == View.SCROLL_AXIS_VERTICAL;
}
#Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) {
super.onStopNestedScroll(coordinatorLayout, child, target);
}
private void init(View recyclerView){
recyclerView.setClipToOutline(false);
mInitialHeight = recyclerView.getTop();
}
private void resize(int dy) {
mLastHeight = mTargetView.getLayoutParams().height + -dy;
mTargetView.getLayoutParams().height = mLastHeight;
mTargetView.requestLayout();
}
}
By the way, the activity containing these fragments has a viewpager. I don't know if that would be relevant. But just to let you guys know.

Show empty view when data set is empty in recyclerview

I have a recyclerview which shows multiple cardviews with items. In the recyclerview, I have a popup menu to delete that current cardview.
When the dataset is empty I would like to show an empty view. Something with an image and text saying "empty" I have tried some online examples. No success.
My layout is a cardview(card_view_row.xml) is a simple view with a cardview that shows a few items.
Here is my recyclerview
public class AlarmRecyclerViewAdapter extends
RecyclerView.Adapter<AlarmRecyclerViewAdapter.DataObjectHolder> {
private ArrayList<Alarm> mDataset;
private static AlarmRecyclerViewAdapter.MyClickListener myClickListener;
private AlarmDataAccess dataAccess;
private Alarm alarm;
private int switchOn = 1;
private int switchOff = 0;
private static Context context;
private PopupMenu popupMenu;
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
TextView dateTime;
TextView label2;
TextView textViewLabel;
TextView gender;
TextView daysofweek;
Switch aSwitch;
ImageView trash;
public DataObjectHolder(View itemView) {
super(itemView);
dateTime = (TextView) itemView.findViewById(R.id.textView2);
label = (TextView) itemView.findViewById(R.id.textView);
label2 =(TextView) itemView.findViewById(R.id.textView3);
aSwitch = (Switch)itemView.findViewById(R.id.switchButton);
trash = (ImageView)itemView.findViewById(R.id.imageTrash);
gender = (TextView)itemView.findViewById(R.id.textViewGender);
daysofweek = (TextView)itemView.findViewById(R.id.textViewDays);
textViewLabel = (TextView)itemView.findViewById(R.id.textViewLabel);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(AlarmRecyclerViewAdapter.MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public AlarmRecyclerViewAdapter(ArrayList<Alarm> myDataset, Context context2) {
mDataset = myDataset;
context = context2;
}
#Override
public AlarmRecyclerViewAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row, parent, false);
AlarmRecyclerViewAdapter.DataObjectHolder dataObjectHolder = new AlarmRecyclerViewAdapter.DataObjectHolder(view);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(final AlarmRecyclerViewAdapter.DataObjectHolder holder, final int position) {
boolean status = false;
int gender;
alarm = new Alarm();
dataAccess = new AlarmDataAccess(context);
dataAccess.open();
holder.label.setText(mDataset.get(position).getHourOfDay() + ":" + mDataset.get(position).getMinute() + " " + mDataset.get(position).getTimeSet());
holder.textViewLabel.setText(mDataset.get(position).getLabel());
gender = mDataset.get(position).getGender();
if(gender == 0){
holder.gender.setText("Male voice");
}else{
holder.gender.setText("Female voice");
}
holder.daysofweek.setText(mDataset.get(position).getDays());
holder.label2.setText("" + mDataset.get(position).getAffirmationName());
holder.trash.setImageResource(R.drawable.menu2);
if( mDataset.get(position).getStatus() == 0){
status = false;
}else {
status = true;
}
holder.aSwitch.setChecked(status);
holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isPressed()) {
if (isChecked) {
mDataset.get(position).setStatus(switchOn);
} else {
mDataset.get(position).setStatus(switchOff);
}
alarm.setId(mDataset.get(position).getId());
alarm.setStatus(mDataset.get(position).getStatus());
dataAccess.updateStatus(alarm);
}
}
});
holder.trash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupMenu = new PopupMenu(AlarmRecyclerViewAdapter.context, v);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
long selectedAlarmId = mDataset.get(position).getId();
Intent intent = new Intent(AlarmRecyclerViewAdapter.context, EditAlarmActivity.class);
intent.putExtra("id", selectedAlarmId);
intent.putExtra("Edit", "FromEdit");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmRecyclerViewAdapter.context.startActivity(intent);
return true;
case R.id.delete:
long id = mDataset.get(position).getId();
mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataset.size());
dataAccess.deleteAlarm(id);
return true;
}
return false;
}
});
popupMenu.show();
}
});
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
#Override
public int getItemCount() {
return mDataset.size();
}
}
My recyclerview is inside a fragment view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_place">
<ImageButton
android:id="#+id/add_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="end|bottom"
android:tint="#color/icon"
android:elevation="12dp"
android:background="#drawable/oval_ripple"
tools:backgroundTint="#96ceb4"
android:src="#android:drawable/ic_input_add"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="14dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_below="#+id/add_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
</RelativeLayout>
Is there a way to detect if the data set is empty to show an empty view with text saying empty or something? I need this done inside recyclerview since I am deleting items inside there.
EDIT
I finally figured out how to do it. I am posting this incase anyone else has the same problem.
http://akbaribrahim.com/empty-view-for-androids-recyclerview/
For my personal use I use this trick :
I use two LinearLayout, one with my RecyclerView and another with a simple TextView saying "Empty". Both are in the same parent view.
<LinearLayout
android:id="#+id/linearContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="invisible">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearEmpty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="#string/dialog_no_result"
android:textColor="#color/cardview_dark_background" />
</LinearLayout>
And when I update my list of item I call this method :
if (linearContent != null && linearEmpty != null) {
if (mListItems.isEmpty()) {
linearContent.setVisibility(View.GONE);
linearEmpty.setVisibility(View.VISIBLE);
textEmpty.setVisibility(View.VISIBLE);
} else {
linearContent.setVisibility(View.VISIBLE);
linearEmpty.setVisibility(View.GONE);
textEmpty.setVisibility(View.GONE);
}
}
Then I use the same layout, but just change the visibility of some objects.
Hope it help.

Checkbox for a row in ListView

I want to add, for each row in ListView, a checkbox which will be activated and shown on long press, I don't know whether I think correctly, I should add in row layout a Checkbox which is default hidden and when action start all check box on list will be shown and able to check?
To show CheckBox on each row:
1. Add an extra boolean variable isLongPressed to your adapter class and initialized with default false value from adapter constructor.
2. In your adapter getView()/ onBindViewHolder() method add an condition like this:
CheckBox checkBox = (CheckBox) view.findViewById(R.id.check);
if(isLongPressed)
{
checkBox.setVisibility(View.VISIBLE);
} else {
checkBox.setVisibility(View.GONE);
}
3. Add an method showCheckbox() to your adapter class to update ListView with checkbox visible state.
public void showCheckbox()
{
isLongPressed = true;
notifyDataSetChanged(); // Required for update
}
4. Call showCheckbox() from onItemLongClick:
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Long Click", Toast.LENGTH_SHORT).show();
your_adapter.showCheckbox();
return true;
}
});
Here is good tutorial about Contextual Action Mode
Hope this will help~
Try this:
We will use a recyclerview, and a checkbox adapter
Chechbox Adapter 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="#dimen/checkbox_adapter_item_height"
android:gravity="center_vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:theme="#style/MyCheckBoxTheme"
android:clickable="false"
android:longClickable="false"
android:focusable="false"/>
<TextView
android:id="#+id/tvItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item1"
android:visibility="visible"
android:padding="6dp"
android:paddingStart="12dp"
android:textColor="#color/colorMaterialBlack"
android:textSize="16sp" />
</LinearLayout>
A style for checkbox, keep this in style
<style name="MyCheckBoxTheme" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/colorBlackDimText</item>
<item name="colorControlActivated">#color/greenStatus</item>
</style>
A Model for the Checkbox adapter you can define/add/remove vars in your model
public class CheckboxTitlesData {
private String title;
private boolean isSelected;
public CheckboxTitlesData(String title, boolean isSelected) {
this.title = title;
this.isSelected = isSelected;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
Checkbox Adapter
public class CheckboxTitleAdapter extends RecyclerView.Adapter<CheckboxTitleAdapter.ViewHolder> implements GenericAdapterInterface{
List<CheckboxTitlesData> dataList = new ArrayList<>();
private CheckboxTitlesData previousSelection;
protected MyApplication.MenuSelectionListener listener;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(getRootLayout(), parent, false);
return new ViewHolder(view);
}
public CheckboxTitleAdapter(MyApplication.MenuSelectionListener listener){
this.listener = listener;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
CheckboxTitlesData data = dataList.get(position);
if (data.isSelected()){
previousSelection = data;
holder.checkBox.setChecked(true);
}else holder.checkBox.setChecked(false);
holder.tvItem.setText(data.getTitle());
}
#Override
public int getItemCount() {
return dataList.size();
}
#Override
public void changeData(List dataList) throws IllegalArgumentException{
if (dataList == null || dataList.size() <= 0)
return;
if (!(dataList.get(0) instanceof CheckboxTitlesData))
throw new IllegalArgumentException("Required data type \"CheckboxTitlesData\"");
this.dataList.clear();
this.dataList.addAll(dataList);
(new Handler(Looper.getMainLooper())).postDelayed(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
}, 100);
}
#Override
public int getRootLayout() {
return R.layout.adapter_title_checkbox;
}
#Override
public void setOnClickListener(RecyclerView.ViewHolder holder) {
holder.itemView.setOnLongClickListener((View.OnLongClickListener) holder);
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
#Bind(R.id.tvItem)
TextView tvItem;
#Bind(R.id.checkbox)
CheckBox checkBox;
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
setOnClickListener(this);
}
#Override
public boolean onLongClick(View v) {
final int pos = dataList.indexOf(previousSelection);
if (pos == getAdapterPosition())
return;
if (listener != null)
listener.onMenuSelected(dataList.get(getAdapterPosition()));
CheckboxTitlesData data = dataList.get(getAdapterPosition());
data.setSelected(true);
dataList.set(getAdapterPosition(), data);
if (pos != -1) {
previousSelection.setSelected(false);
dataList.set(pos, previousSelection);
}
(new Handler(Looper.getMainLooper())).postDelayed(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
}, 100);
return true
}
}
}
Interface, u can remove the interface if you wanted, I just use this for my adapters usually for readability for other dev:
public interface GenericAdapterInterface {
void changeData(List dataList) throws Exception;
int getRootLayout();
void setOnClickListener(RecyclerView.ViewHolder holder);
}
Recycler view layout xml, add the recyclerview whr you need, this is just an eg
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingEnd="#dimen/padding_normal"
android:paddingStart="#dimen/padding_normal">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Activity/fragment that uses the recycler view must do this
#Bind(R.id.rvMenu)
RecyclerView rvMenu;
private CheckboxTitleAdapter menuAdapter;
//Define an interface for callback on long press
public interface YourOwnInterface {
void onLonPress(Object data);
}
private void setUpRecycleView() {
RecyclerView.LayoutManager mLayoutManager = new
LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
rvMenu.setHasFixedSize(false);
rvMenu.setLayoutManager(mLayoutManager);
rvMenu.setItemAnimator(new DefaultItemAnimator());
YourOwnInterface listener = new YourOwnInterface () {
#Override
public void onLonPress(Object data) {
updateView((CheckboxTitlesData) data);
}
};
//this interface is needed wen a longpress is made adapter and the callback is given to your Acitivity/Fragment you can perform necessary opreation
menuAdapter = new CheckboxTitleAdapter(listener);
rvMenu.setAdapter(menuAdapter);
}
private void updateView(CheckboxTitlesData data) {
//perform operation on long press
}
Done it works

Categories