- Essential Guide For Designing Your Android App Architecture: Designing Your Android App Architecture Part 3 (Dialog, ViewPager, RecyclerView, and Adapters)
- By Janishar Ali
- Translation from: The Gold Project
- This article is permalink: github.com/xitu/gold-m…
- Translator: woitaylor
- Proofreader: Hanliuxin5 Leviding
Android MVP Architecture Essentials: Part 3 (Dialog, ViewPager, RecyclerView and Adapters)
I’m pleased that Parts 1 and 2 of the Android MVP Architecture series have been well received since their release. But also because of your suggestions and contributions, the project is better.
During this development process, many people have asked how to use dialogs and Adapter-based views in this architecture. So I’ll add to both in this blog post.
If you haven’t read the first two blogs, I highly recommend reading them before reading this one. Here is a link to the blog:
- Building Essentials for Android MVP: Part 1
- Building Essentials for Android MVP: Part 2
- MindorksOpenSource/android-mvp-architectureWarehouse has a complete implementation of the framework sample code
In this article, I’ll extend the framework by adding a rating dialog and a Feed interface.
Translator: Feed refers to an RSS Feed, an encyclopaedia of feeds, and in the following translation I use Feed or RSS directly.
God is in the details
Let’s take a look at the renderings:
Rating dialog box
- The rating dialog box shows 5 stars, and users can choose the number of stars according to their satisfaction.
- If the number of stars is less than 5, we will modify the dialog to show a feedback form that asks the user for suggestions for improvement.
- If I have five stars. We’ll show a jump to the app store in the dialog
google play
). Users can make comments there. - The scoring information is sent to the backend server of the application.
Note: The rating dialog is not necessary from the user’s point of view, but it is important for us as developers. Therefore, the application needs to design this execution process very cleverly.
I suggest increasing the spacing between adjacent controls in the dialog box.
Feed interface
- This interface is going to have two subinterfaces.
- Subinterface 1: Blog
RSS
List interface. - Subinterface 2: Open source code
RSS
List interface.
blogRSS
The child interface
- Get data from the server.
- Fill with data
RecyclerView
In theCardView
.
The open source projectRSS
The child interface
- Get warehouse data from the server.
- This warehouse data is used to populate
RecyclerView
The inside of theCardView
.
Now that we have identified the business requirements, the next step is to extend the existing architecture based on those requirements.
I’m not going to post the whole code snippet here because it’s too long. Instead, open the MVP project in a new TAB in your browser. Then we’ll switch back and forth between the two tags.
Summary:
Add the following classes
(in the project com. Mindorks. Framework. MVP. The UI. The base package inside view code)
- BaseDialog: in this class we add
Dialog
Template code, as well as some general methods. For actual projectsDialog
This can be done by extending the base class. - DialogMvpView: This interface is defined
Presenter
与Dialogs
Interactive APIS. - BaseViewHolder: It defines
RecyclerView
Binding framework and implementationViewHolder
The ability to automatically clean up views when reused.
public abstract class BaseDialog extends DialogFragment implements DialogMvpView
Copy the code
One note about the framework.
All related functions should be grouped together in what I call the encapsulation of function points so that they are independent of each other.
Rating dialog box:
- This dialog can be opened through the menu list in the left drawer.
- Its implementation sumThe second articleFrom the blog
MVP
The components are similar.
Open Project Repo in a new TAB in your browser and thoroughly explore the implementation of the scoring dialog section in your project
A little note about dialog boxes
Some applications may use a lot of small dialog boxes, in this case we can create generic MVPView, MVPPresenter and Presenter for these dialog boxes to use.
The Feed part:
- It’s included in this package
FeedActivity
And it’sMVP
Components,FeedPagerAdapter
.blog
Packages andopensource
The package. - blogB: It’s in this bag
BlogFragment
And it’sMVP
The component andRecyclerView
的BlogAdapter
. - opensourceB: It’s in this bag
OpenSourceFragment
And it’sMVP
The component andRecyclerView
的OpenSourceAdapter
. FragmentStatePagerAdapter
Used to createBlogFragment
和OpenSourceFragment
.
Never instantiate any object in any Adapter class or use the new operator to generate an object. Fetch them with the dagger injection.
OpenSourceAdapter and BlogAdapter are implementations of recyclerView. Adapter
. In this project, an empty view is displayed when no data is available. The user can click the RETRY button to retrieve the data and delete the empty view when the data is retrieved.
API data paging and network state processing is left to you as an exercise.
Now explore the code through the project, taking a closer look at the layout in the XML and how to manipulate the view through the code.
If you find it difficult or need any help or improvement, please post your question in the Mindorks community: click here to join the Mindorks Android community where we can learn from each other.
Thanks for reading this article, and if you found it helpful, don’t forget to click on ❤ below. This will help more people learn from this article.
If you want to learn more about programming, follow Me and Mindorks on Medium so you can be notified when new articles are published.
Check out all the Mindorks best articles here.
You can also friend me on Twitter, Linkedin, Github, and Facebook.
Coder’s Rock 🙂
Diggings translation project is a community for translating quality Internet technical articles from diggings English sharing articles. The content covers the fields of Android, iOS, front end, back end, blockchain, products, design, artificial intelligence and so on. For more high-quality translations, please keep paying attention to The Translation Project, official weibo and zhihu column.