Change fragment from clickable textview - android

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.

Related

Can we call onClick event of a button inside a fragment layout in MainActivity, instead of in that class of fragment?

I have created an onClick event of a button inside a fragment layout, and write it in main activity class. It works !. However, I tried create same onCLick event of the button in the fragment layout in the fragment class. It also works !
My question is what difference if we write an event and call it in main activity (button is inside of fragment layout) VS write it and call it in fragment class where the button belong to. Both are same ? Any pros and cons ?
The main difference is where you call the 'onClick()'.
If you are calling the onclick after replacing the fragment, then it will work as expected.
But, If you are calling it before initializing fragment, then the app will crash.
So never ever do that.

How to add an item to a listview via custom dialog?

I created a custom dialog box android activity in a separate class.
The dialog box has a button which adds an item to listview whenever it is clicked.
The list view is declared in the base activity.
I suggest you use a combination of DialogFragment and Callback for this issue.
Implement like this answer in this, and make some adjustments. in your button onClick(), call YourInterface.method().
This will trigger the method you override on your BaseActivity, which you do add a item into your ListView.

RecyclerView inside DialogFragment inside Activity

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.

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.

GlobalLayoutLIstener behavior for Fragment?

I am using GlobalLayoutLIstener in an fragment that is associate with an Activity, and this activity has other associative fragment as well, what I am observing is global layout listener calling for every fragment, even I didn't set it in those fragment.
any one have idea how to use GlobalLayoutLIstener using single context in different-2 fragment?

Categories

Resources