I have in my MainActivity 3 RecyclerViews .
One of them in a bottom sheet and it is the main one (By Default the Bottom sheet is Open To Display this one ), in its adapter's onbind method I made an onClickListener so that I want when the user clicks on an item in it,
I want to go back to the main activity class to set To Start a method which it's rolled is to close the Bottom Sheet and set the data for the next recycling view (which will appear when the Bottom Sheet is closed)
..... The issue here is how to start this method from the onBind method's Listener and give it a parameter from this viewHolder as its name and some of its attributes
if there is something not clear please let me know
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
Easiest:
You declare the method you want to call in your Activity. It has to be public:
public void method(){}
Then, in the Constructor of the Adapter, you pass a reference to the Activity as a parameter:
public MyAdapter(Activity activity){}
And, in the onBindViewHolder:
MyActivity mActivity=(MyActivity)activity;
mActivity.method();
You can do that easily.
Define custom interface
public Interface CustomEventListener
{
public void MyEventListener(String message); //you can change parameters
}
In your adapter class
public Adapter ......... {
private CustomEventListener listener;
public void setListener(CustomEventListener listener)
{
this.listener = listener;
}
//Your onBind
Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null)
listener.MyEventListener("Message");
}
});
}
}
In your Activity when create Adapter add this code
public void InitAdapter()
{
yourAdapter = new Adapter(); // bloa bla bla
yourAdapter.setListener(new CustomEventListener() {
public void MyEventListener(String message)
{
// then do what you want
}
}
}
You need to use Listener for handling the click in your Adapter something like this:
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
then call the listener with:
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
...
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call the listener.
mListener.onItemClick(v, viewHolder.getAdapterPosition());
}
});
}
then when you're using the adapter, set the listener with something like this:
adapter.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(View view, int position) {
// do something with the view and position.
}
});
You want your recycleviews to be aware of each other. You'll need a class, maybe MainActivity, that will keep the selected value, also clear it upon request, in between recycleviews. Then you'll need that value to propagate the next recycleview. If you want to be efficient then you'll use one recycleview and swap the data in between selections, and animate it so it looks like a new recycleview is created.
Related
I'm developing an application with the MVP architecture. I have a RecyclerView with the following method:
#Override
public void onBindViewHolder(#NonNull ChatRoomAdapter.ChatRoomViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mChatRooms.get(position).getUid();
}
});
}
I need to pass the mChatRooms.get(position).getUid() to the next acitivty/presenter/interactor but how can I cleanly do this? Can I use an intent using the putExtra method to pass the data?
You must do something like
interface YourPresenter {
void onItemClicked(int uid);
}
class YourPresenterImpl : YourPresenter {
#Override
void onItemClicked(int uid) {
view.startActivity(uid) // method from your interface for you view
}
}
and then you must add your presenter's reference to you item click in holder
I have an activity that has a TabLayout(where my Fragments are in)
and myFragment has a RecyclerView itself.
I want to show every touched Item's name in a textView in myActicvity.
This is an image that explains my problem.
How should I do that? Thanks for any Help!.
You can use Event bus: https://github.com/greenrobot/EventBus. This library help you to send events from one activity/fragment/view to another. You must just add onclicklistener for item root element, and do something like this: EventBus.getDefault().post(getItemName());. And at your activty add this:
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(String name) {
textView.setText(name);
};
And don't forget to register and unregister listener.
you should write onItemCLickListener in your recyclerView adapter , and then call it from your activity to set textview text.
Adapter Code:
OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onClick(int position,String itemName);
}
#Override
public void onBindItemViewHolder(final AdapterRecycelerView.ViewHolderRecyclerItem holder, final int position) {
try {
holder.linearItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (onItemClickListener != null)
onItemClickListener.onClick(position,arrayList.get(position).getName());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Activity Code:
adapterRecycelerView.setOnItemClickListener(new AdapterRecycelerView.OnItemClickListener() {
#Override
public void onClick(int position,String itemName) {
textview.setText(itemName);
}
});
I have a delete button in my adapter call and when i press the delete button i want to
1. remove the value from list and,
2. Update the size of the list in the main activity
and . Have tried using Interface where i referred here but it's not working and i am so confused.
So can any one provide me an best way
My Adapter Code:
#Override
public void onBindViewHolder(final AddLineItem_Adapter.ViewHolder holder, final int position) {
final AddLineItem_ListView addLineItem_listView = addLineItem_listViews.get(position);
holder.tv_OrderID.setText(addLineItem_listView.getItemID());
holder.tv_ProductName.setText(addLineItem_listView.getProductName());
holder.tv_Quantity.setText(addLineItem_listView.getQuantity());
holder.tv_UnitPrice.setText(addLineItem_listView.getUnitPrice());
holder.tv_TotalAmount.setText(addLineItem_listView.getTotalAmount());
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addLineItem_listViews.remove(position);
notifyDataSetChanged();
}
});
}
I want to update the addLineItem_listView size on the main activity in TextView.
Create the interface..
Pass it to the adapter..
Call interface function after deleting (providing the size of list)
Create interface
public interface ShowDeleted {
void showDeleted(int size);
}
Initialize the interface anonymously in the activity like this
ShowDeleted showDeleted = new ShowDeleted() {
#Override
public void showDeleted(int size) {
// show the changed list size or update UI
}
};
Pass the interface to the recyclerAdapter
YourAdapter youradpter = new YourAdpater(context,list,showDeleted );
Initialize the constructor of recyclerAdapter like this:-
public YourAdapter(Context context, YourList yourList, ShowDeleted
showDeleted)
{
}
holder.onClick do this
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addLineItem_listViews.remove(position);
notifyDataSetChanged();
this.showDeleted.showDeleted(addLineItem_listViews.size());
}
});
try to use view.setTag(position)
#Override
public void onBindViewHolder(final AddLineItem_Adapter.ViewHolder holder, final int position) {
final AddLineItem_ListView addLineItem_listView = addLineItem_listViews.get(position);
holder.tv_OrderID.setText(addLineItem_listView.getItemID());
holder.tv_ProductName.setText(addLineItem_listView.getProductName());
holder.tv_Quantity.setText(addLineItem_listView.getQuantity());
holder.tv_UnitPrice.setText(addLineItem_listView.getUnitPrice());
holder.tv_TotalAmount.setText(addLineItem_listView.getTotalAmount());
holder.btn_Delete.setTag(position)
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addLineItem_listViews.remove((Integer)v.getTag());
notifyDataSetChanged();
}
});
}
Good Luck!
use this function
public void removeAt(int position) {
list.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, list.size());
}
I have 2 Recyclerviews inside 2 different Fragments contained in a ViewPager. When a card in my RecyclerviewA is clicked I would like to pass the respective object over to RecyclerviewB currently I have an onClick in RecyclerView with the following code:
#Override
public void onBindViewHolder(ViewHold holder, int position) {
holder.nameTextView.setText(list.get(position).getString("name"));
holder.addressTextView.setText(list.get(position).getString("fromAddress"));
final int pos = position;
holder.acceptButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ApplicationClass.bus.post(list.get(pos));
}
});
In RecyclerViewB I have
public PickupListAdapter(Context context, List<ParseObject> ddList) {
this.ddList = ddList;
this.mContext = context;
ApplicationClass.bus.register(mContext);
}
#Subscribe
public void answerAvailable(ParseObject object) {
ddList.add(object);
notifyDataSetChanged();
}
Inside my Application Class I have:
public static Bus bus;
#Override
public void onCreate(){
super.onCreate();
bus = new Bus(ThreadEnforcer.MAIN);
}
The "answerAvailable" method is not called. What is currently incorrect regarding my implementation?
I can see only one change from my working code. Can you try changing this:
bus= new Bus(ThreadEnforcer.ANY);
I have a Button on Listview and a custom adapter that loads the ListView. I want to call the Button onClickListner in main class not in the adapter class. how can i do this.
You can create one Global Interface
Like this
package com.radiofrance.interfaces;
public abstract class GlobalInterface implements eventInterface {
public void onClick(int values) {
}
public void onCancel() {
}
}
Then Implement this interface in Your Activity from which you call your adapter
public class SampleClickListener extends GlobalInterface {
#Override
public void onClick(final int values, final boolean state) {
}
}
now in your adapter pass this object of interface
Adapter adt = new Adpater(this, R.layout.row, arrRow,
new SampleClickListener());
and from your adapter call
listner.onClick(position, false);
Thanks