当前位置:网站首页>My gadget - card learning app is complete

My gadget - card learning app is complete

2022-06-21 17:32:00 nahfang

Disclosure time

In the past ten days , What have I done ? It is agreed to prepare the autumn move , Why is it gone again ? In fact, in the past ten days , I am also working hard for Qiu Zhao ( Improve your own small projects ), I'm glad to be here today APP The first draft of has been completed ,《 Card learning 》 Is a Android System tools APP, Due to audit material problems , It is not on the shelves at present , Just uploaded the beta version in dandelion ( Don't post links here ). Open source in GitHub - CardStudy. If you want to practice your hand, you can go and get it , But don't forget star once Hee hee . Take a look at the following parameters before taking them

Analysis of project structure and knowledge points involved

Tools platform : AndroidStudio、Windows10

Language : Java

Architecture design pattern : MVVM

Application building : use single Activity + many Fragment The pattern of 、 there Fragment The management of is to use the official recommendation Navigation programme .

In fact, the above parameters are listed so that the students who want to practice this project can have at least the above knowledge reserves . Under this premise , Just go to get the source code of the project .
Then I will introduce , Implementation details of modules in the project :

View binding 、 Data caching and binding

View binding , I use DataBinding、 Data caching and binding are Livedata(Kotlin recommend Flow). But the author did not bind in the layout file , But in Java The code is updated through data-driven UI, I think it depends on personal habits . And control the data flow to a single direction , It's also officially recommended .

Data persistence

Here for APP The generated data is stored locally and persisted , According to the classification of data ( The author is divided into two categories ) The data is stored locally in the form of database and file respectively , So the database used here is be based on Sqlite Encapsulated ROOM frame , Files are used SharedPrefrences(xml file , It can also be regarded as file storage ha-ha ~)

asynchronous

Java Multithreading used asynchronously in , In this project, the author adopts AlibabaJava Recommended in the developer manual , Do not use an off the shelf thread pool implementation class , Instead, a constructor is provided to create .( There are many pits here , When used with databases , Before using Kotlin Coordination ROOM In development , There is no such thing as , sigh Kotlin It's still cool ). A pit guide

Volume optimization

Of course, this is also to reduce the package size , We reuse the code as much as possible ( Understood as a small optimization ), For resources in the project , It can be distributed dynamically ,( For example, picture resources , You can put it in the gallery , Then pull down from the remote end when the user is using , But here the author is short of money Ignore ), So the author can only optimize the code as much as possible , We pull out the bottom components and pop-up components that may appear in multiple places . The pop-up window uses the builder mode ha-ha ~ This must be put forward .

Base class

Because it's Shan Activity + many Fragment Pattern , So the base class is only for Fragment It was packaged , It's mainly about ViewModel Reflection acquisition of , Common methods and basic environment are configured . In fact, make good use of base classes , In the subsequent development process, redundant code and unnecessary errors can be avoided .

Problems encountered in the development process

Sliding conflict problem

This problem often occurs , Therefore, we should try to avoid the same direction sliding scene in the design and development process , Of course, if it is necessary to do so , We can also solve it , But you'll never know , How many big pits are waiting for you behind a pit , So try not to take the risk . Now let's talk about the sliding conflict problem encountered by the author in the project , For the three masters at the same level Fragment Realize sliding page cutting on , And the need to provide a container for it , Used ViewPager, But the author is the first Fragment I used a DrawerLayout( Drawer ), Because they slide left and right , Then there is a sliding conflict , In fact, the author thought of it before writing , At that time, I felt a little sliding conflict , My backhand event distribution mechanism will solve it for you in minutes . Yes , There is no conflict , When I touch drawer when , The incident was handed over to drawer Handle , When I feel “ Everything is all right !” 了 , But then there was a big problem , When you turn the page and then come back through the logic call open() Cannot be opened drawer 了 , And will not call back any methods , But you can pull it out by hand , Then the author enters the debugging mode ~ It is found that all parameters are normal after a thorough investigation , But I just don't draw drawer. The conclusion drawn from the exchange group and the Internet is ViewPager A pit in itself , And I did this after I changed the plan bug Vanished .

RecyclerView May be unexpected

In the use of Recyclerview It is comfortable to display data as a list , But in Fragment There are also some unexpected pits in . We usually work for adapter After setting or updating the data source, call notifyDatasetChanged() Method to notify and refresh view. Here we know in adapter in , We have to prove that the observed object List The invariance of , To make this method work . But we may not know it by most novices , stay Fragment There is more than just a guarantee List unchanged , Also make sure that the view object does not change . Some students may think that the view will change , In fact, I understand Fragment Life cycle of , And when it will be called , We'll see ,onCreateView Method is actually called again after each switch . So the view will be recreated . Because we are onCreateView The view bound in the method also needs to remain unchanged . Then we can take some measures to create it only once , For example, set it as a global variable , Before each creation, judge whether it is null, Not for null Do not create or in onActivityCreated Method set and bind. Obviously, it will be better to use this method .

Finally, it is recommended not to use it casually in order to write less lines of code “ ?:” , Make up all the conditions honestly .

Why do you say that ? This is because the author is implementing APP When an algorithm in , The ternary operator is used , Pre test , The result is ok Of , But when I get through the data , Very strange things happen when testing in a real environment bug. And it's hard to reproduce ( This is the case debug Extremely difficult ), It turns out that index Transboundary , In fact, if you are guaranteeing index Without problems , There is no problem using the ternary operator , But you can't guarantee that nothing will go wrong , Add an extra layer of insurance at this time , It can really avoid the disaster caused by problems .

ArrayList<**> arr // This parameter is passed in by the method , And it must not be null
int arrSize =  arr.size();
if(arrSize == 0) return null;
... //  After some column operations , I will get a greater than or equal to 0  Less than  arrSize() Number of numbers 

int index  // The number from the above 

index > 0 ? arr.get(index) : arr.get(0);  //  strange bug There is  index :1 size :0 ;

// Note that I have judged if size = 0  Exit ahead of time . But it could be because index Across the border , However, according to the procedural logic, there is no out of bounds problem . So I changed the ternary operator to conditional judgment .bug Vanished ! Yeah, really index Across the border .

原网站

版权声明
本文为[nahfang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211515571421.html