Adding view to RecyclerView from another fragment: Interface of static? - java

I have one activity and 2 fragments that never run simultaneously, A and B. Fragment A has a RecyclerView, and from fragment B I want to add views to that ReyclerView.
I know the documentation says two fragments should never communicate directly, but I'm not sure how far that goes and if my case is really considered communication.
Should I use a static method or implement an interface in the activity?

The best thing to do is use EventBus library. I have a demo app in which you can add items to RecyclerView from anywhere within the app using EventBus. Here is the link to the repo:
https://github.com/code-crusher/android-demos/tree/master/EventBusDemo
And if you want to understand how it works you can refer to my article:
https://medium.com/#code_crusher/eventbus-for-android
Hope it helps. Happy coding :)

Related

How can I turn 40 similar activities into one that holds different data?

I want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.
The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.
I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".
There's must be a way of doing it, although I'm not experienced enough
Here is an example layout
It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.
This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.
You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time.
Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.
Check this question to get how to pass data between activities
Click here to see how to create recycler views.

Develop app with multiple activities or one activity and multiple fragments?

Hi everyone as the title say I am wondering whats the best practice to design an android app should I make multiple activitites for each interface in my app or one activity that has multiple fragments(note:I am using a navigation buttombar)
Depending upon your need you can choose between activities and fragements. Activity is more like a complete separate page and fragment is a small view that can be called in an activity, multiple fragments can be used to display activities UI .
Fragments can be used to make dynamic views check this link https://developer.android.com/training/basics/fragments/index.html. Hope this helps :)

Android swipe between fragments

In my Android APP I have this ListView where I wish that each item is divided in two parts and it could be swiped. I wonder what should be the best way of doing that since the SlidingDrawer is now deprecated (and is not swippable). I need it to be an easy way to implement and few stress to mobile processor.
The content would be different in each item so I need to be able to access the TextViews inside them.
This is the example I wish to the item od the ListView:
The user could Swipe between both fragments and check its contents.
Any idea, advice or tutorial on this matter? Thank you very much for your attention
Use a ViewPager. Really easy to use, just extend a FragmentPagerAdapter to create the fragments to swipe between.

How to create multitasking video activity like new youtube App?

Anyone has any idea how can i create a multitask video activity ot fragment like new youtube.
Can any body tell me the correct procedure to do this.
How do I achieve this type of feature with visual effect.
Everything happens in single activity, logic is separated by fragments.
This article:
http://flavienlaurent.com/blog/2013/08/28/each-navigation-drawer-hides-a-viewdraghelper/
Literally describes how to do this
What do you mean with multitask? With two activities one above the other?
If that's what you are asking for you can use SlidingMenu library, this allows you tu insert another layout on the same activity that can be hidden, a sliding menu.
SlidingMenu Lib
If you are asking for running a process outside UI thread you must use AsyncTask class. It provides an easy way of doing some work in a different thread.
AsyncTask Android
All what i need is ViewDragHelper Class.
Here is the documentation.enter link description here

How to use Android Fragments?

I'm looking at some demo code that shows how to use a Fragment Adapter (Tab Adapter in this case). I'm curious as to what exactly the instantiate() method does. I see it used in the following demo code on this page:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
(see the getItem() method within the TabsAdapter class)
If I'm reading the demo code correctly, every time a user clicks on one of the tabs, a new Fragment is created? And thus the fragment starts the entire life-cycle again (onAttach()...onCreate()... etc)? This sounds awfully inefficient. I would think that the fragment that will represent the content for each tab should be instantiated only once (perhaps in the addTab() method), and then saved into some collection where it can be fetched when getItem() is called.
Please correct me if I'm mistaken in any of this. I'm trying to better understand how to manage fragments.
My money would be on that the setCurrentItem() function doesn't actually destroy the existing Fragment being shown in that tab. Otherwise there's really not much of a reason for the adapter to have a List of available tabs. Likely, when you switch from one tab to another, setCurrentItem() just detaches the UI from the currently active Fragment (or calls its onPause() method) and then re-attaches the UI for the newly selected Fragment (or calls its onResume() method).
But, if you're in doubt, you could read the source :)
Hope it helps,
David
I was able to find an explanation for my question here

Categories

Resources