You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
5.9 KiB
169 lines
5.9 KiB
|
3 years ago
|
# Android Studio.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
官方默认的底部导航栏是采用观察者设计模式,结合LiveData和Fragment以及Activity融合一起所形成的。
|
||
|
|
|
||
|
|
所产生的效果则是感知型的一个组件。
|
||
|
|
|
||
|
|
```java
|
||
|
|
/**
|
||
|
|
* 业务处理类
|
||
|
|
*/
|
||
|
|
package com.example.navigationdemo.ui.home;
|
||
|
|
|
||
|
|
import android.os.Bundle;
|
||
|
|
import android.view.LayoutInflater;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.ViewGroup;
|
||
|
|
import android.widget.Button;
|
||
|
|
import android.widget.EditText;
|
||
|
|
import android.widget.LinearLayout;
|
||
|
|
import android.widget.TextView;
|
||
|
|
|
||
|
|
import androidx.annotation.NonNull;
|
||
|
|
import androidx.annotation.RequiresApi;
|
||
|
|
import androidx.fragment.app.Fragment;
|
||
|
|
import androidx.lifecycle.ViewModelProvider;
|
||
|
|
|
||
|
|
import com.example.navigationdemo.R;
|
||
|
|
import com.example.navigationdemo.databinding.FragmentHomeBinding;
|
||
|
|
|
||
|
|
import org.json.JSONException;
|
||
|
|
import org.json.JSONObject;
|
||
|
|
|
||
|
|
public class HomeFragment extends Fragment {
|
||
|
|
|
||
|
|
private FragmentHomeBinding binding;
|
||
|
|
private JSONObject jsonObject;
|
||
|
|
|
||
|
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||
|
|
ViewGroup container, Bundle savedInstanceState) {
|
||
|
|
HomeViewModel homeViewModel =
|
||
|
|
new ViewModelProvider(this).get(HomeViewModel.class);
|
||
|
|
|
||
|
|
binding = FragmentHomeBinding.inflate(inflater, container, false);
|
||
|
|
View root = binding.getRoot();
|
||
|
|
|
||
|
|
final TextView textView = binding.tv1;
|
||
|
|
final Button button = binding.btn1;
|
||
|
|
final EditText editText = binding.edit1;
|
||
|
|
|
||
|
|
|
||
|
|
button.setOnClickListener(new View.OnClickListener() {
|
||
|
|
@Override
|
||
|
|
public void onClick(View v) {
|
||
|
|
textView.setText(editText.getText());
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
|
||
|
|
return root;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onDestroyView() {
|
||
|
|
super.onDestroyView();
|
||
|
|
binding = null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
在这个代码块中,我们可以通过 final 来定义视图中的控件。
|
||
|
|
|
||
|
|
这样就和学习中那样,可以正常的对控件进行开发。如监听事件。
|
||
|
|
|
||
|
|
也就是说,在onCrateView中我们可以正常的像在MainActivity中对控件那样开发。
|
||
|
|
|
||
|
|
但定义的方式可能不一样, 并且定义的位置也是和在MainActivity中所定义的位置有着很大的区别。
|
||
|
|
|
||
|
|
在main中我们定义的位置可能是在
|
||
|
|
|
||
|
|
```java
|
||
|
|
日常所定义的控件的位置是在onCreate的方法外头作为声明的。也就是这样
|
||
|
|
|
||
|
|
private button btn;
|
||
|
|
....
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onCreate(Bundle savedInstanceState){
|
||
|
|
super.onCreate(savedInstanceState);
|
||
|
|
// code .....
|
||
|
|
|
||
|
|
这里则是调用控件。或者是对控件进行监听。所达到某个效果。
|
||
|
|
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
而在fragment 中则是:
|
||
|
|
|
||
|
|
```java
|
||
|
|
public View onCreateView(@NonNUll layoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
|
||
|
|
// 视图绑定
|
||
|
|
HomeViewModel homeviewModel = new ViewModelProvider(this).get(HomeViewModel.class);
|
||
|
|
binding = FragmentHomeBinding.inflate(inflate,container,false);
|
||
|
|
View root = binding.getRoot();
|
||
|
|
|
||
|
|
// 定义控件的位置
|
||
|
|
final 控件类型 控件名;
|
||
|
|
|
||
|
|
// 返回控件
|
||
|
|
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
|
||
|
|
|
||
|
|
retrun root;
|
||
|
|
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
这样使得让控件的使用的区域更加的明确。
|
||
|
|
|
||
|
|
在fragment的创建视图事务中调用
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
页面初始化
|
||
|
|
|
||
|
|
解决思路:
|
||
|
|
|
||
|
|
举一个例子:例如在打开A页面Activity时,在调用startActivity方法的时候,就去调用A页面的加载数据方法loadData(),此时开始加载网络数据,同时Acitivity开始初始化加载布局等,等Activity准备好UI需要数据的时候去检查loadData()是否加载完毕,如果完毕了就直接显示数据,如果没有完成就弹出loading开始等待它执行完成。
|
||
|
|
|
||
|
|
有了解决思路,那就先了解Activity和Fragment的生命周期,从生命周期中来确定在那个地方调用初始化方法。
|
||
|
|
|
||
|
|

|
||
|
|
|
||
|
|

|
||
|
|
|
||
|
|
可以看出在创建activity的中,用户是感受不到创建期间所调用的方法有哪些。只有在on start 的时候才会展现出界面,让用户观看。
|
||
|
|
|
||
|
|
由此可见,可以在创建时调用其他类中所写好的初始化方法。来获取数据并处理它让后将处理好的数据展示出来。
|
||
|
|
|
||
|
|
如果结合Fragment 的生命周期的话,就可以在on crate view中来对on create中所调用的请求方法得到的数据来进行判断和处理,然后将数据一一绑定到视图下的列表控件中,将它展示出来。
|
||
|
|
|
||
|
|
> 哦对了,还有一点,就是可以将请求的方法,或者请求的类或方法。写在Adapter(适配器)中。这样可以让fragment从中调取相关的操作。
|
||
|
|
>
|
||
|
|
> 而数据模型,我们可以通过一个POJO类来定义数据结构。让其传到Mu table Model中。【这个类是Live Data的一个子类,这个类就相当于是传入啥就会返回啥】。
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
新感悟:
|
||
|
|
|
||
|
|
安卓中提供了适配器,适配器的作用是连接UI和后端请求得到数据的一个中间件。
|
||
|
|
|
||
|
|
所以,将请求的方法放在适配器中实现是我们在开发中所经常采用的开发逻辑。
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
新感悟:
|
||
|
|
|
||
|
|
昨晚从B站观察到有关新版本到android开发相关的知识点。突然发现之前所看到的lifecycle 、view model 、fragment、live data、adapter等相关的知识的作用原来是和MVC模型的相似度蛮高,可以说,有些作用甚至和html的div一样。但还是多多少少的区别。
|
||
|
|
|
||
|
|
就比如fragment ,它这个的作用是可以在view中,也就是app界面中当作一个div 来看待。因为一个视图中可以有多个fragment。
|
||
|
|
|
||
|
|
而lifecycle 则是和mvc不同的作用。lifecycle具有感知功能。换句话说是可以自动检测app的生命周期,当达到某个生命周期,那么这个lifecycle则可以调用相关的方法和操作。
|
||
|
|
|
||
|
|
|