RecyclerView inside DialogFragment inside Activity - android

When I press a button in my Activity, a DialogFragment pops up. Inside the dialog fragment, there is a RecyclerView that looks a like a normal list view.
The behaviour I want is that when I press on row n of the RecyclerView, the DialogFragment closes and the Activity does something based on the value associated with row n.
It seems that the ViewHolder has to implement the OnClickListener interface. When a row is clicked, the ViewHolder's delegate should do something. The delegate should be the DialogFragment. And the DialogFragment in turn talks with the Activity to which it is attached.
If this is the case, the ViewHolder has to ask the DialogFragment to do something, and the DialogFragment asks the Activity to do something. Is this the right approach? How do I pass the reference of the DialogFragment to onCreateViewHolder()? Should the Adapter keep a reference to the DialogFragment?

Yes, you are moving in the right direction. Pass the DialogFragment's reference in the constructor of the adapter. Once you have the reference and the desired click event fires, call getActivity() on the dialog's reference to get a reference to the activity. Then you can do whatever you want in the activity. Also, I suggest you implement listeners using interfaces. What you want to do is keep the DialogFragment invisible to the underlying activity and your adapter loosely coupled to the DialogFragment, and interfaces will help in that case.

Related

How can I pass a listener from a Fragment to a DialogFragment?

I'm currently working in a single activity application that is using fragments for each screen. I'm also using MVP design pattern.
Context
I have a Fragment (Fragment-A) in which I have a List of Items.
This Fragment handles the actions for each of the items. Because it has access to the presenter.
I have a DialogFragment (Fragment-B) in which you can fill some checkboxes and complete an action (This is action is handled in the Fragment-A which implements an interface for this)
I'm using a bundle to create DialogFragment. (I can't pass the listener as an argument)
What I want?
How can I pass Fragment-A as a Listener to the DialogFragment (Fragment-B), so I can call the actions from the DialogFragment?
Assuming your DialogFragment is a child fragment of the other Fragment (you're passing in getChildFragmentManager() to show()) as it should be, then your FragmentA will get a callback to onAttachFragment():
Called when a fragment is attached as a child of this fragment.
This gives you a reference to the child DialogFragment, where you can then set any listener you want.

Handling click event for custom view inside fragment's listview

I have a listview inside fragment in android. What I need is to handle click event in custom view. I have custom BaseAdapter.
What I solved
I created a Interface inside adapter and implement in fragment. And in onClickListener for button, I cast fragment that is passed from constructor and call the method.
My Question is How can I get Parent Fragment from my Adapter. I don't want to pass fragment from constructor. I searched a lot and I don't see anything. Any reference can help me too. Thanks.
If you don't want to pass fragment through constructor then you will pass an instance of activity for inflating the custom view. Add a method in the activity that will return your fragment.
I don't know why you do not like to pass the fragment as listener to adapter.
But I assume in your BaseAdapter you should have the Activity context. If assumption is correct, you may do:
((Activity)context).getFragmentManager().findFragmentById(R.id.container);
to get the corresponding fragment.

Change fragment from clickable textview

I have fragments, in them I want to place custom textview, so I have not to define them everytime.
So when I define the custom textview class I'd like to define a private onClickListener. When the user click on the textview the fragment will bu substituted. In the method onClick inside onClickLister i cannot invoke getFragmentManager. How can i do it?
You have to do it manually as it's a really bad design - to bind item behaviour to objects it does not have to know about.
Just define a class, that implements OnClickListener the interface, getting the fragmentManager as a parameter of a constructor and perform all needed actions in this class.
Then simply bind a listener's instance to the TextView in any object wich has a reference to a FragmentManager.

How to use dialogs in a MasterDetail android application

I've been following the android developer guide, (specifically the Passing events back to the dialog's host) to create a DialogFragment that will contain an EditText and pass back that text.
I want my DetailFragment to call that Dialog with a button press, but implementing my DialogListener on the fragment is not enough, it must be implemented on the activity, but from the DetailActivity I can't call my button's OnClick (button is on the fragment).
So my DialogListener must be implemented on the activity, but from there I can't get to my buttons, which are on the fragment.
Do my buttons just have to be on the DetailActivity instead of on the fragment for this to work? Or is there another way to go about this?
I think this is what you are looking for
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
you need to send a message to the activity when the button is clicked, then let the activity popup the dialog

Android: Firing intents from a view

I have an activity with a custom view which extends EditText in it. Usually when someone longclicks an EditView a menu pops up asking the user to choose input method.
I have overridden the onLongClick to make this menu NOT appear, since
I use my own methods of input. However, i want a ListActivity to start when user longclicks the EditText (or rather again my view which extends EditText). But it seems a view cannot fire intents, only activities, am i right?
Fine, so i try to capture the longclick from the activity that spawned my EditText-like view. But inside my EditText-like view i already consume the longclick, either in the overridden onLongClick() by setting it true to avoid the "Please choose input method" menu to appear, or if returning false, then that very menu appears and consumes my longclick. In neither of the cases the intent fires...
In short:
A user longclicks an EditText, and i want a custom ListActivity to start. How do i accomplish this?
EDIT:
So i found startActivity in the Context object. But i will have the user pick an item from a list. That item would then appear in the EditText, so i thought i had to use startActivityForResult, which incidentally does not appear in the Context object :(
EDIT 2:
How do i get a result back from an activity started with startActivity?
You don't need an Activity to start another Activity, but a Context object.
You got two options here. You can either pass an context into you custom view by using a custom constructor or a setter method. Or the OnLongClickListener has to be implemented in the Activity and set to the custom EditText from there.

Categories

Resources