I got a parent layout, activity_main.xml, with multiple views inside it, one of which is the RecyclerView view.
Within the recycler view, there are buttons that, once the user deals with them, I would like things to happen in the parent layout that is activity_main.xml.
For example, if I press a button on a ViewHolder item inside the child RecyclerView, in some cases, I would like changes to happen in the parent activity_main.xml layout.
However, my problem is that I don't know how to allow for this communication to happen.
What might be the best practices for such cases?
You need to create an interface callback or use a higher-order function.
This Stack Overflow answer is an example of an interface callback:
How to access component of MainActivity in recycler view to make onclick listener?.
Kotlin Lambda Functions For RecyclerView Adapter Callbacks in Android is for higher-order functions.
Related
I'm trying to design my LoginActivity to look like my LoginController in iOS. Is there a way to make an activity transparent, or do I need to use a fragment? Thank you!
// My Design
You can achieve this through multiple ways
Create an activity and make its background as transparent in the layout.(Not recommended)
Create an alert dialog within the activity and make the alert dialog background as transparent
Create a dialog fragment make its layout transparent and open it from the activity.
Create a view stub within the same activity layout and inflate the view when required. (Handling back press events might be a difficult task here).
Although the right way would be to create an alert dialog within the activity or creating a dialog fragment or create a view stub. Create an alert dialog if you don't have much events or elements within the dialog since its easy and efficient than creating a dialog fragment for a little dialog. Creating a view stub would be the most efficient way since it simply inflates the view which takes less amount of resource. But don't go with creating an activity for this dialog which is resource intensive and not the correct way.
I am using 'com.android.support.constraint:constraint-layout:2.0.0-alpha5'.
I have a fragment using MotionLayout inside an activity that also carries a BottomNavigationView inside a CoordinatorLayout with a custom behaviour allowing to show and hide when the content is scrolled.
With all fragments, where there is no MotionLayout, this behaviour works perfectly. Only with the MotionLayout, which has a NestedScrollView as a child, and makes use of an onSwipe transition, this swipe seems to be consumed, and does not arrive at the activity's coordinator layout.
Is this a current limitation of how MotionLayout works, and is it possible to extend it to support my use case?
How to implement Nested recyclerview in android. I have to implement one recyclerview containing two buttons and inside that recyclerview again in second recyclerview two images have to be there how to implement this can anyone suggest me i am new to android
You cannot place a RecyclerView inside another RecyclerView within a layout xml file. But you can programatically attach a RecyclerView into another one. For example by attaching Layouts programatically. Then you would have two scrollable RecyclerViews. This may sometimes be a thing you don't want to have. For example because of CollapsingToolbarLayouts. Then you can deactivate nested scrolling of the inner RecyclerView by
mRecyclerView.setNestedScrollingEnabled(false);
But if you disable scrolling you will lose the performance advantage of the RecyclerView because the inner one does not recycle anymore if it is not scrollable. But maybe you just need something like this SectionedRecyclerViewAdapter.
Yes we can implement Nested RecyclerView in Android Studio
Just make a Child Adapter and its xml layout and set that adapter inside the Parent Adapter. You can refer to our blog if your have any doubt.
How to Implement Nested RecyclerView in Android
I'm trying to accomplish switching views without using a navigation controller, tab bar controller etc. I am currently accomplishing this using Cocos2d director class replaceScene method. My application will need to have around 40 view controllers, each with a few UIButtons that could take them to any other view controller.
For instance View controller 1 may have buttons that take you to view controller 2
View Controller 2 may have buttons that link to 3,4,5,12
view controller 4 may need to link to view controller 17, 5 and 3
Every tutorial and bit of documentation I've read only discusses using Navigation Controllers, Tab bars or pushing views modally. None of these solutions fits my particular requirements.
Cocos2d has the "replaceScene" method which does exactly what I need, but mixing the many UIKit controls that I need makes developing this entire project in Cocos2d a nightmare.
I'm looking for something where I can have the user tap a button which will load a specified view controller/view transition to that view, and unload the previous view controller from memory. Any ideas?
Have a root view controller which has references of your view controllers. Also make a weak reference to the root view controller in each view controller, as in a delegate pattern. If one of the view controllers wants to make a view transition, send a message to the root view controller. Let the root view controller hide the current view and unhide the next view, using an animation if you want.
Basically you are implementing a view container much simpler than UINavigationController and UITabBarController. You could probably achieve the same thing using the tab bar controller and hide the tab bar view, but I would implement a custom one.
my question is about view controllers, delegates and all that in general. I feel perfectly comfortable with UIView, UIViewController, Delegates and Sources, like UITableView does for instance. It all makes sense.
Now I have implemented my first real custom view. No XIBs involved. It is an autocomplete address picker very much like in the Mail application. It creates those blue buttons whenever a recipient is added and has all the keyboard support like the original.
It subclasses UIView. There is no controller, no delegate, no source. I wonder if I should have either one of those? Or all, to make it a clean implementation.
I just cannot put my finger on the sense a view controller would make in my case. My custom view acts much like a control and a UIButton doesn't have a controller either.
What would it control in my view's case?
Some of my thoughts:
For the source: currently the view has a property "PossibleAutocompleteRecipients" which contains the addresses it autocompletes. I guess this would be a candidate for a "source" implementation. But is that really worth it? I would rather pass the controller to the view and put the property into the controller.
The selected recipients can be retrieved using a "SelectedRecipients" property. But views should not store values, I learned. Where would that go? Into the controller?
What about all the properties like "AllowSelectionFromAddressBook"? Again, if I compare with UIButton, these properties are similar to the button's "Secure" property. So they are allowed to be in the view.
The delegate could have methods like "WillAddRecipient", "WillRemoveRecipient" and so on and the user could return TRUE/FALSE to prevent the action from happening. Correct?
Should I maybe inherit from UIControl in the first place and not from UIView?
And last but not least: my custom view rotates perfectly if the device is rotated. Why don't all views? Why do some need a controller which implements ShouldAutoRotateToDeviceOrientation()?
Does it make sense what I wrote above? In the end I will provide the source on my website because it took me some time to implement it and I would like to share it as I have not found a similar implementaion of the Mail-App-like autocomplete control in MonoTouch.
I just want to learn and understand as much as possible and include it in the source.
René
I can answer part of your question.
I just cannot put my finger on the
sense a view controller would make in
my case
The ViewController is responsible for handling the View's state transitions (load, appear, rotate, etc) These transitions are used mainly when you use a navigation component (UINavigationViewController, UITabBarController). These components needs to received a ViewController that will handles the view's transitions.
For exemple, when you push a ViewController on a UINavigationViewController, it will cause the ViewDidLoad, ViewWillAppear, ViewDidAppear. It will also cause the ViewWillDisappear, ViewDidDisappear of the current ViewController.
So, if your application has only one portrait view, you don't need a ViewController. You can add your custom view as a subview of the main window.