Android学习之Fragment的各种实现功能(头部折叠,循环列表RecyclerView,左右滑动ViewPager)
Posted wangweizu99
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习之Fragment的各种实现功能(头部折叠,循环列表RecyclerView,左右滑动ViewPager)相关的知识,希望对你有一定的参考价值。
android学习杂记
四大组件之Service
1、服务开启的两种方式
-
startService:开启服务
开启服务后 服务就会长期的后台运行,即使调用者退出了.服务仍然在后台继续运行.服务和调用者没有什么关系, 调用者是不可以访问服务里面的方法.
-
bindService:绑定服务
服务开启后,生命周期与调用者相关联.调用者挂了,服务也会跟着挂掉.不求同时生,但求同时死.调用者和服务绑定在一起,调用者可以间接的调用到服务里面的方法.
2、滑动控件
NestedScrollView
折叠头部
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/abc_vector_test"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/view_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="新闻详情" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="hello world" />
...
...
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="170dp"
app:contentScrim="#999999"
app:expandedTitleMarginBottom="100dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="我是collapsebar的标题">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一个固定(pin)"
android:textSize="40sp"
app:layout_collapseMode="pin" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="不设置,跟随滑动"
android:textSize="40sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="视察效果(parallax)"
android:textSize="40sp"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="top"
android:background="#600f"
app:layout_collapseMode="pin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是toolbar" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="appbar之内,collap之外"
android:textColor="#0f0" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/n_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="哈"
android:textColor="#0f0"
android:textSize="200sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="哈"
android:textColor="#0f0"
android:textSize="200sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="哈"
android:textColor="#0f0"
android:textSize="200sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="哈"
android:textColor="#0f0"
android:textSize="200sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
解释文章
3、常用属性大全
1、实现圆角矩形背景
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#534F46" />
<!-- 圆角的半径 -->
<corners android:radius="10dp" />
</shape>
4、Fragment之间的通信之ViewModel
示例:
public class SharedViewModel extends ViewModel {
private final MutableLiveData<Item> selected = new MutableLiveData<Item>();
public void select(Item item) {
selected.setValue(item);
}
public LiveData<Item> getSelected() {
return selected;
}
}
public class MasterFragment extends Fragment {
private SharedViewModel model;
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
model = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
itemSelector.setOnClickListener(item -> {
model.select(item);
});
}
}
public class DetailFragment extends Fragment {
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedViewModel model = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
model.getSelected().observe(getViewLifecycleOwner(), item -> {
// Update the UI.
});
}
}
模仿:
public class SharedViewModel extends ViewModel {
private final MutableLiveData<String> strs = new MutableLiveData<String>();
public void add(String str){
strs.setValue(str);
}
public LiveData<String> getStrs(){
return strs;
}
}
public class Fragment1 extends Fragment {
private SharedViewModel model;
public Fragment1() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View inflate = inflater.inflate(R.layout.fragment01, container, false);
model = new ViewModelProvider(requireActivity(),new ViewModelProvider.NewInstanceFactory()).get(SharedViewModel.class);
Button bt1=inflate.findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et1 = inflate.findViewById(R.id.et1);
model.add(et1.getText().toString());
}
});
return inflate;
}
public class Fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment02, container, false);
SharedViewModel model = new ViewModelProvider(requireActivity(),new ViewModelProvider.NewInstanceFactory()).get(SharedViewModel.class);
model.getStrs().observe(this, item -> {
TextView t1 = inflate.findViewById(R.id.fragment2_t1);
t1.setText(item);
});
return inflate;
}
}
5、实现循环列表
建立RecyclerView适配器
public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
private Context mContext;
private List<Integer> index=new ArrayList<Integer>();
//展示的数据
public LinearAdapter(Context context){
this.mContext=context;
for(int i=0;i<100;i++){
index.add(i+1);
}
}
//添加循环布局
@Override
public LinearAdapter.LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false));
}
//可以为每个布局添加消息
@Override
public void onBindViewHolder(LinearAdapter.LinearViewHolder holder, int position) {
holder.textView.setText(String.format("常用功能 %s", index.get(position)));
}
//item的总长度
@Override
public int getItemCount() {
return 100;
}
//LinearViewHolder可以看作一个item的展示和内部逻辑处理,故而如果需要绑定事件,获取控件的时候要在itemView中获取
//itemView由onCreateViewHolder方法LayoutInflater.inflatec创建
class LinearViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
public LinearViewHolder(View itemView){
super(itemView);
textView=(TextView) itemView.findViewById(R.id.tv_main);
//简单事件处理
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
调用适配器
public class Fragment4 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment04, container, false);
RecyclerView mRvMain = inflate.findViewById(R.id.rv_main);
mRvMainAndroid学习之Fragment的各种实现功能(头部折叠,循环列表RecyclerView,左右滑动ViewPager)