I'm working on a Material design app. One feature I want to implement is some kind of a poll. When a user clicks an element of a list, the persistent bottom sheet dialog, which looks like this should show up:
Then, when user clicks any button, this dialog should go away and the modal bottom sheet dialog should show up, providing a user with more information about the list item which was clicked at the beginning. It looks like this:
I can't find any clear explanations about BottomSheetDialog and BottomSheetDialogFragment, and how to use them correctly, even after reading some information about AppCompat dialogs. So, my questions are:
In what way are they different and which one should I use for each
case?
How to get data in the activity about which button was pressed in the dialog?
Any links to the code of implementations or tutorials about using them?
Finally, I've found the solution and it works. Tell me if I'm doing something wrong. It basically works like DialogFragment from this guide, but I've done it a bit different.
1) Their difference is the same as it of DialogFragment and Dialog, and they both are modal. If you need persistent dialog, use BottomSheetBehaviour instead (I found out that both dialogs had to be modal in my app).
2) I have to answer the third question with some code first, and then it will be easy to answer the second one.
3) Create a new public class, which extends BottomSheetDialogFragment, I called it FragmentRandomEventPoll. There are two two things which have to be implemented here.
Override method onCreateView. It is nearly the same as onCreate method in Activities, except for that it returns the View it should inflate:
// We will need it later
private static String headerItem;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_random_event_poll, container, false);
header = (TextView) v.findViewById(R.id.uRnd_fragment_bottom_sheet_poll_header);
skip = (Button) v.findViewById(R.id.uRnd_fragment_bottom_sheet_button_skip);
header.setText(...);
// I implemented View.OnClickListener interface in my class
skip.setOnClickListener(this);
return v;
}
Static method which you can pass necessary data to and get new instance of this class (Probably I could have just used a regular constructor, I'll have to experiment with it a bit more). URandomEventListItem is the data model class.
public static FragmentRandomEventPoll newInstance(URandomEventListItem item) {
FragmentRandomEventPoll fragment = new FragmentRandomEventPoll();
headerItem = item.getHeader();
return fragment;
}
2) To get input events in activity or any other place, define an interface with necessary methods and create setter method for it's instance:
private PollButtonClickListener listener;
public void setListener(PollButtonClickListener listener) {
this.listener = listener;
}
public interface PollButtonClickListener {
void onAnyButtonClick(Object data)
}
And in the place you want to get your data ("dialog_event_poll" tag was specified in the layout):
FragmentRandomEventPoll poll = FragmentRandomEventPoll.newInstance(events.get(id));
poll.setListener(new FragmentRandomEventPoll.PollButtonClickListener() {
#Override
public void onAnyButtonClick(Object data) {
// Do what you want with your data
}
});
poll.show(getSupportFragmentManager(), "dialog_event_poll");
}
If there is anything unclear, my project files could be found on Github.
About handling events from DialogFragment/BottomSheetDialogFragment.
For applications with many activities, this method is great:
context as MyDialogFragmentListener
But I have a problem with an application with single activity and multiple fragments. Since there can be a lot of fragments, it seems like a very bad option to transfer all events to the necessary fragments through the main activity. Therefore, I decided to do this:
private inline fun <reified T> findListeners(): ArrayList<T> {
val listeners = ArrayList<T>()
context?.let {
if (it is T) listeners.add(it)
if (it is AppCompatActivity) {
it.supportFragmentManager.fragments.forEach { fragment ->
if (fragment is T) listeners.add(fragment)
fragment.childFragmentManager.fragments.forEach { childFragment ->
if (childFragment is T) listeners.add(childFragment)
}
}
}
}
return listeners
}
Code in DialogFragment:
private val listeners by lazy { findListeners<MyDialogFragmentListener>() }
Of course, fragments can contain as many other fragments as you like and probably need to be checked through recursion, but in my case this is superfluous.
Related
In my app I have one activity which has a RecyclerView adapter using a List of OrderItems
When a user clicks on an item, I pass the object from my RecyclerAdapter to my MainActivity to inflate a BottomSheetDialog
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
. . .
int position;
ViewHolder(#NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
. . .
adapterCallback.onOrderItemClick(orderItem);
}
}
public interface OrdersAdapterCallback {
void onOrderItemClick(OrderItem orderItem);
}
In the implemented interface callback method inside my MainActivity, I inflate the BottomSheetDialog using the object from my adapter (First time the object is passed)
MainActivity
#Override
public void onOrderItemClick(OrderItem orderItem) {
//Object passed from adapter
BottomSheetDialogFragment dialogFragment = BottomSheetOrderFragment.newInstance(orderItem);
dialogFragment.show(getSupportFragmentManager(), Constants.FRAGMENT_BOTTOMSHEET_ORDER);
}
When the BottomSheetDialog is inflated, there is a button that dismisses this current dialog, and inflates a FragmentDialog that passes that OrderItem (but after using some setters on it) object again (second time the object is passed).
Inside the BottomSheetDialog:
private BottomSheetOrderFragment(OrderItem orderItem){
this.orderItem = orderItem;
this.orderItem.setNewOrder("New order string here");
}
#OnClick(R.id.btn_edit_order)
void onEditOrderBtnClick(){
dismiss();
mainActivity.editOrder(this.orderItem);
}
So currently we have passed the OrderItem object to two different fragments inside the MainActivity. I then have to pass this object to the MainViewModel, and then to the MainRepo where the RoomDatabase class inserts that object into the local db.
This ends up having the OrderItem to be passed through 4 different classes through their constructors, and on the way a few setters are applied to that object. My question is, is this a bad practice in Android / OOP in general? Or is there a better way of doing what I'm trying to achieve?
My question is, is this a bad practice in Android / OOP in general?
Yes.
Or is there a better way of doing what I'm trying to achieve?
Probably.
What that is is hard to say since it's not fully clear what you're trying to achieve and how this code is structured.
But I would advise you check out this documentation on sharing data between fragments. With that in mind, consider keeping this OrderItem object in one place - the ViewModel - then accessing the one shared view model from each Fragment or Activity that needs to work with / on the order item.
Its not a bad practice but however a good one will be to use a shared viewmodel from which the fragments can access the data they need
Can someone explain the logic on how to handle this matter:
I have a fragment that after a WebSocket call inflates 2 Recyclerviews.
Child Recyclerview is nested to Parent Recyclerview and the parent adapter calls the child adapter.
I want to put an Interface for a click listener which handles the click in the Child Items in the Fragment.
Where should I put the interface and which class should implement it?
What you're trying to do has been done multiple times.
There are various approaches you can try, but in general, responsibilities would look something like this:
YourContext (Fragment/Activity)
Inflates a layout with a RecyclerView.
Instantiates YourAdapter
Subscribes, Requests, Waits, for your data and passes it onto YourAdapter.
Maintains an interface for click handling, like:
interface YourThingClickHandler {
fun onThingWasClicked(thing: Thing) // and any other thing you may need.
}
Can be YourContext: YourThingClickHandler or if you want, you can keep an anonymous/local instance of that. I usually do the former and then implement the fun onThingWasClicked(...) in the fragment/activity, it depends what you need to do when the item was clicked.
YourAdapter
Expects a list of Things and one YourThingClickHandler instance. So in your Fragment/Activity you'd do, something like (pseudo code):
// This is called once your ViewModel/Presenter/Repository/etc. makes the data available.
fun onThingsLoaded(things: List<Thing>) {
adapter.setClickHandler(this) // this can be passed when you construct your adapter too via constructor like adapter = YourAdapter(this)
adapter.submitList(things) // if the adapter extends a `ListAdapter` this is all you need.
}
Now that you've passed an outer click handler, you need to deal with the inner list. Now you have a few choices:
1. pass the same click handler all the way in and let the innerAdapter directly talk to this.
2. Have the outerAdapter act as an intermediate between the clicks happening in the innerAdapter and bubble them up via this click handler you just supplied.
Which one you chose, will depend largely on what you want to do with it, and how you want to handle it. There's no right or wrong in my opinion.
Regardless of what you do, you still need to get from the view holder to this click handler...
So in YourAdapter you should have another Interface:
interface InternalClickDelegate {
fun onItemTappedAt(position: Int)
}
This internal handler, will be used to talk from the viewHolder, back to your Adapter, and to bubble the tap up to the external click handler.
Now you can have a single instance of this, defined like so in your adapter class (remember this is Pseudo-Code):
private val internalClickHandler: InternalClickDelegate? = object : InternalClickDelegate {
override fun onItemTappedAt(position: Int) {
externalClickHandler?.run {
onThingWasClicked(getItem(position))
}
}
}
So if the external click handler (the YourThingClickHandler you supplied) is not null, then fetch the item from the adapter's data source, and pass it along.
How do you wire this internal handler with each view holder?
When you do onCreateViewHolder, have a ViewHolder that takes... you guessed, a InternalClickDelegate instance and so...
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// decide which viewHolder you're inflating... and...
return YourViewHolder(whateverViewYouInflate, internalClickHandler)
Now your ViewHolder(s) have a reference to this internal click handler...
so when you do onBindViewHolder(...) you probably call a common ViewHolder method of your choice, for example if your View holder can be of different types, you probably have an Abstract viewHolder with a fun bind(thing: Thing) method or similar that each concrete viewHolder subType will have to implement... in there, you'd do something like this:
override fun bind(thing: Thing) {
if (clickHandler != null) {
someViewYourViewHolderInflated.setOnClickListener(this) // this assumes your ViewHolder implements View.OnClickListener from the framework
}
}
Because your ViewHolder implements View.OnClickListener, you must implement the onClick method in it:
override fun onClick(v: View?) {
clickHandler?.onItemTappedAt(adapterPosition)
}
And this is how your ViewHolder, will receive the tap/click event from Android in the onClick method, if you supplied a click Handler (you did in the adapter onCreateViewHolder when you passed the internalClickHandler), it will simply bubble the tap, passing the position. adapterPosition is the Kotlin equivalent of calling getAdapterPosition() in a RecyclerView adapter.
TOO LONG, DIDN'T READ GRAPH
Fragment: ExternalClickListener -> passes an instance of it to the Adapter.
Adapter: Receives the ExternalClickListener, passes an InternalClickListener to each ViewHolder.
ViewHolder: Receives the internal Click Listener, sets itself as Clickable (either the entire itemView or just any widgets you want to make clickable, if you want the whole cell to be clickable, simply use itemView which is the "whole" view of the ViewHolder.
When the viewHolder's view is tapped, android calls the click listener's onClick method. In there, and because you are in a ViewHolder, you can do getAdapterPosition() and pass this to the internal click handler you received.
The Adapter then can transform that position back into data, and because you supplied an External clickListener, it can pass the actual item back to the external click listener.
Wait, but how about a NESTED RecyclerView.
There's nothing special about that, you simply need to provide the same mechanism, and keep passing things around. What you do or how many of these interfaces you have, depends entirely on what you're trying to achieve; like I said at the beginning, each solution is different and other factors must be taken into account when making architectural decisions.
In general, keep this thing in mind: Separation of Concerns: keep things small and to the point. For E.g.: it may seem crazy to have this double interface, but it's very clear what each does. The internal one, is simply concerned about a "tap" in a "view", and to provide the position in a list where said tap occurred.
This is "all" the adapter needs to fetch the data and make an informed guess at what item was truly tapped.
The fragment doesn't know (or care) about "positions", that's an Adapter's implementation detail; the fact that positions exist, is oblivious to the Fragment; but the Fragment is happy, because it receives the Thing in the callback, which is what most likely needs to know (if you needed to know the position for whatever reason, tailor and modify the externalCallback to have the signature of your choice.
Now replicate the "passing hands" from your OuterAdapter to your InnerAdapter, and you have done what you wanted to do.
Good luck!
1) You should put interface in child adapter and implement that in parent and then pass another one interface (long peocess)
2) Use local broadcast manager
you will add ClickListener in parent adapter and also add it in constructor of adapter
public interface HandleClickListener
{
void onItemClicked(int position, SurveysListModel surveysListModel);
}
Make an instance of your clicklistener and then on holderclick listner get the position of item and its value from your model list
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handleClickListener.onItemClicked(position, yourModelList.get(position));
});
and get in to your activity like this making an instance of you adapter
adapter = new InstrumentsSearchAdapter(yourModelsList, activity.this, new SearchAdapter.HandleClickListener() {
#Override
public void onItemClicked(int position, Model listModel) {
instumentChild = listModel.getInstrument_title();
Intent intent = new Intent(Activity.this, Listing.class);
intent.putExtra("selectedQuestions", listModel.getQuestions());
startActivityForResult(intent, 5);
}
});
And if you want to go to parent recyclerview class implement onActivityResutlt method and get data back from child through intent and get that intent in onActivityResutlt method
I'm trying to attach an onClickListener() method to an item which is inside a Recycler view. I know I can easily achive that by doing it from the RecyclerAdapter, but the goal of doing that is to show a custom dialog with some information that parent fragment contains, there are some ways to pass data, but I think that's better to attach the listener from fragment instead, and this way I can directly access the data.
I've tried to access from the fragment the way I use to do it from the adapter, with some modifications:
myRecyclerAdapter.myViewHolder.reportContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"Touch",Toast.LENGTH_SHORT).show();
}
});
But aparently the myViewHolder object it's not created yet by the time I try to use it, so I get the Java NullPointerException (F..$&##^$&^%, don't misunderstand me, I love it).
So, I need some help to do what I'm trying to, or some other good ideas to try, warning: I;m really trying to avoid passing data, except with maybe a ViewModel (don't know if I can), becouse it's a lot of fields to pass
This is fundamentally incorrect. The problem here is, there are multiple ViewHolders in the RecylerView. Which one do you want to attach it to? There would be n number of items and not all items will be rendered at the same time.
Instead of updating the ViewHolder, use a callback.
class MyAdapter extends RecyclerView.Adapter {
MyAdapterCallback callback = null;
....
#Override
public void onBindViewHolder(holder: ViewHolder, position: Int) {
holder.reportContainer.setOnClickListener { // You can set this in OnCreateViewHolder as well.
if (callback != null) {
callback.onClick();
}
}
}
}
interface MyAdapterCallback {
void onClick()
}
From your fragment,
myAdapter.callback = new MyAdapterCallback() {
#Override
public void onClick() {
// Access your fragment variables here.
}
}
On Android here is for example an excellent code fragment,
showing how to achieve five buttons on a dialog fragment...
android DialogFragment android:onClick="buttonCancel" causes IllegalStateException could not find a method
in your DialogFragment, you have to setOnClickListener(this) for all of your buttons/imageviews etc.
Then you implement View.OnClickListener and have a routine like this...
public void onClick(View v)
{
Utils.Log("Fucking 'A' sort of... ");
switch (v.getId())
{
case R.id.postfragment_send:
break;
etc etc etc
default:
break;
}
}
That's all fantastic. BUT.
Over in my main activity, where I have a ListView. The custom cells have five buttons. Very simply, in the main activity, I have five routines named whatever I like...
public void clickedComments(View v)
{
int position = feed.getPositionForView(v);
...etc etc
}
public void clickedExplosions(View v)
{
int position = feed.getPositionForView(v);
...etc etc
}
public void clickedTanks(View v)
{
int position = feed.getPositionForView(v);
...etc etc
}
Then you just do this which is unbelievably easy ("screw Xcode!") ...
Amazing!
My question, why can't I use the 'onClick system' in dialog fragments?
What am I doing wrong? Can an android expert explain what the fundamental difference is between the two? For the record my projects are 4.1+ only.
Thanks!!
Here I am pasting in a full example of a fragment, using the first method described above.
public class HappyPopupFragment extends DialogFragment implements View.OnClickListener
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.happy_popup, container);
_setupButtons(view);
return view;
}
public void onClick(View v)
{
Utils.Log("Fucking 'A' sort of... ");
switch (v.getId())
{
case R.id.button_a:
Utils.Log("tanks !!");
break;
case R.id.button_b:
Utils.Log("bombs !!");
break;
case R.id.button_c:
Utils.Log("guns !!");
break;
case R.id.button_d:
Utils.Log("ammo !!");
break;
default:
break;
}
}
private void _setupButtons(View view)
{
((ImageView)view.findViewById(R.id.button_a)).setOnClickListener(this);
((ImageView)view.findViewById(R.id.button_b)).setOnClickListener(this);
((ImageView)view.findViewById(R.id.button_c)).setOnClickListener(this);
((TextView)view.findViewById(R.id.button_d)).setOnClickListener(this);
}
}
that's actually a pretty simple answer, but you have to start it by remembering that Android 1.0 there were no Fragments.
First let's see what an activity really is:
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
An object that extend Context, that's what it is.
So, when you inflate the XML layout, that method inflate is doing stuff like creating and setting up the views like that:
View v = new View(context) // see the context here ?
then when you set on your XML onClick:commentsClick for example, what is happening when you click is:
getContext().commentsClick(View.this); // look, the context again
so let's analyse that:
The XML onClick tries to call back to the context, meaning, call back to the activity that inflated it. See that the IllegalStateException message says that it cannot find the method? Well, because it's not in the activity. Probably if you put commentsClick in the Activity that is creating the DialogFragment, it will work, but that's just bad O.O., right?
The thing with the XML onClick was a nice facilitator to avoid on the huge switch(int) case, but it is a solution that simply does not scale to other classes that might inflate layouts, such as Fragments.
It's just a fact of Android DialogFragment API. Callback methods defined in a fragment XML layout are called in the Activity which contains this fragment. It`s more simple than you mean because in previous Android API-s such "XML defined" callbacks were called in activities also.
(Transferred from my comments as it strikes me as an answer to the question of topic. Perhaps this will be more convenient to future readers of this topic.)
you can use onClickListener, on each View and its subclasses.
setOnClickListener takes as parameter an instance of the class that implements View.OnclickListener.
If you have an error on setOnClickListener(this) it means that the object this refers is an object of an class that does not implements View.OnClickListener
In other words...
Here's how to make onClickListener work for custom cells in custom list views in custom dialog fragments!
in the ADAPTER class (1) for your list view, you'll have code that sets the values for each cell. (Setting text and os on.) In fact, in that same code set onClickListener for each cell button:
v.nameTV.setText( "User Name" );
v.inviteIV.setOnClickListener( ourBoss ); // like this
the problem is what to set the listener to. In fact you want it to be your dialog fragment. "ourBoss" will be the DialogFragment. So (2) when the dialog fragment creates the adapter, pass it in:
in the dialog fragment creating the adaptor:
fosAdapter = new YourHappyAdapter(
getActivity(), getActivity().getLayoutInflater(),
otherStuff, otherStuff, this);
and in the adapter itself ...
public class YourHappyAdapter extends BaseAdapter
{
YourDialogFragmentClass ourBoss;
public FosAdapter(
Context context, LayoutInflater inflater,
blah, blah,
YourDialogFragmentClass ydfc)
{
blah
blah
ourBoss = ydfc;
}
Finally then (3) in the ordinary way, in YourDialogFragmentClass, you can have the usual onClick code!! Hooray, you're done!!
public void onClick(View v)
{
switch (v.getId())
{
case R.id.submit_button: // from the overall fragment screen
_searchNow();
break;
case R.id.cell_button: // that one's from a cell.
Utils.Log("IT WORKED !!!!!!!!!!!!!!!!! !!");
userWantsANewTank(v);
break;
default:
break;
}
}
It's (essentially) just not realistic to use the "old-fashioned" xml-handy-onClick method, when doing custom tables and custom dialog fragments!
Hope it helps someone!
DogActivity is using a custom View. The custom view handles some logic and so has fields. When a particular field reaches a certain value, I want to start a fragment whose parent is DogActivity. How would I do that?
Is it advisable to put a callback inside a custom view so that it calls its parent activity? Or is there a simpler way?
When programming you should always look for consistency, i.e. look around you and see how similar stuff to what you want to do is done. The Android SDK makes heavy use of callback listeners, so they are the way to go here.
In fact we don't even need to know what kind of View your CustomView really is, we can build a general purpose solution. Don't forget to adapt/optimize according to your specific surroundings however. And think about abstraction and generalisation once you get to a point where all your Views are spammed with listeners!
You will need 3 things:
A listener interface
public interface OnCountReachedListener {
public void onCountReached();
}
A place to accept the listener and a place to alert the listener in your CustomView
public class CustomView extends View {
private int theCount;
private OnCountReachedListener mListener;
public void setOnCountReachedListener(OnCountReachedListener listener) {
mListener = listener;
}
private void doSomething() {
while (theCount < 100) {
theCount++;
}
// The count is where we want it, alert the listener!
if (mListener != null) {
mListener.onCountReached();
}
}
An implementation of the interface in your Activity
public class DogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
View myView = new CustomView();
myView.setOnCountReachedListener(new OnCountReachedListener() {
#Override
public void onCountReached() {
Log.w("tag", "COUNT REACHED!");
// START YOUR FRAGMENT TRANSACTION HERE
}
});
}
}
For further information look at the source code of the View class and all the On**XY**Listener interfaces in the Android SDK. They should give you plenty to think about
What is the type of the field? Is it an EditText? SeekBar? Depending on the View, you'll be able to specify different listeners/callbacks to determine when they have changed and if they've reached a certain threshold. I would attach these listeners within onCreate of DogActivity. When the threshold is reached, use a FragmentTransaction to add your Fragment as the child of a container View in DogActivity.