IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android学习之Fragment的各种实现功能(头部折叠,循环列表RecyclerView,左右滑动ViewPager) -> 正文阅读

[移动开发]Android学习之Fragment的各种实现功能(头部折叠,循环列表RecyclerView,左右滑动ViewPager)

Android学习杂记

四大组件之Service

1、服务开启的两种方式

  1. startService:开启服务

    开启服务后 服务就会长期的后台运行,即使调用者退出了.服务仍然在后台继续运行.服务和调用者没有什么关系, 调用者是不可以访问服务里面的方法.
    
  2. 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);
        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;
    }

    //可以当做一个Activity进行控件设置
    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) {
                //Do Nothing
            }
            @Override
            public void onPageSelected(int position) {
                for (TextView viewer :
                        textViewList) {
                    viewer.setTextColor(mDefaultColor);
                }
                textViewList.get(position).setTextColor(mActiveColor);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //Do Nothing
            }
        });

        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>
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-31 16:45:30  更:2021-07-31 16:47:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/6 22:08:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码