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)));
}
@Override
public int getItemCount() {
return 100;
}
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);
mRvMain.setLayoutManager(new LinearLayoutManager(getContext()));
mRvMain.setAdapter(new LinearAdapter(getContext()));
return inflate;
}
}
XML1 Main
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rv_main"
android:background="#1234"/>
layout_linear_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:layout_marginRight="16dp"
android:background="@drawable/drawable02"
android:orientation="vertical">
<TextView
android:id="@+id/tv_main"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="常用功能"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="关注列表" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="监控列表" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="数据中心" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="开票管理" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="赠送VIP" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="活动兑换" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="VIP团队版" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="联系客服" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="邀请好友" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="版本更新" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:text="推荐启信宝" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom|center"
android:paddingBottom="5dp" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="5dp" />
</LinearLayout>
</LinearLayout>
适配器模板
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHoder> {
private Context mContext;
public MyAdapter(Context context){
mContext=context;
}
@NonNull
@Override
public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
public class MyViewHoder extends RecyclerView.ViewHolder{
public MyViewHoder(View view){
super(view);
}
}
}
6、ViewPager+Fragment 实现左右滑动
主Fragment Fragment2.java
public class Fragment2 extends Fragment {
private List<Fragment> fragmentList;
private List<TextView> textViewList;
private int mDefaultColor = Color.BLACK;
private int mActiveColor = Color.RED;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment02, container, false);
fragmentList=new ArrayList<Fragment>();
fragmentList.add(new Fragment2_1());
fragmentList.add(new Fragment2_2());
fragmentList.add(new Fragment2_3());
TextView mcHome = inflate.findViewById(R.id.bottom_b1);
TextView mcCalculator = inflate.findViewById(R.id.bottom_b2);
TextView mcSetting = inflate.findViewById(R.id.bottom_b3);
textViewList=new ArrayList<TextView>();
textViewList.add(mcHome);
textViewList.add(mcCalculator);
textViewList.add(mcSetting);
textViewList.get(0).setTextColor(mActiveColor);
ViewPager mcContainer=inflate.findViewById(R.id.vp_container);
mcContainer.setAdapter(new AppFragmentPageAdapter(getActivity().getSupportFragmentManager(), fragmentList));
mcContainer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (TextView viewer :
textViewList) {
viewer.setTextColor(mDefaultColor);
}
textViewList.get(position).setTextColor(mActiveColor);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return inflate;
}
}
fragment2.xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
>
<include layout="@layout/layout_bottom"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="40dp"
android:layout_width="match_parent"
android:id="@+id/include" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/include"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
layout_bottom.xml 上边导航栏
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FFBCBC"
>
<TextView
android:id="@+id/bottom_b1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="全部"
android:gravity="center"/>
<TextView
android:id="@+id/bottom_b2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="动态"
android:gravity="center"/>
<TextView
android:id="@+id/bottom_b3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="关注"
android:gravity="center"/>
</LinearLayout>
Fragment2_1 、Fragment2_2、Fragment2_3
public class Fragment2_1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_text, container, false);
TextView view = inflate.findViewById(R.id.fragment_text);
view.setText("第一页");
view.setBackgroundColor(Color.YELLOW);
return inflate;
}
}
fragment_text.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#FF0000"
android:textSize="48sp"
android:gravity="center"/>
</androidx.constraintlayout.widget.ConstraintLayout>
|