安卓开发

最近几天还算是有一点收获,学会关于ViewPager2,和fragment的联动实现页面的滑动。上代码:

fragment1 

package com.example.myapplication3.activity.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication3.R;
import com.example.myapplication3.activity.activity.MainActivity2;
import com.example.myapplication3.activity.bean.State;
import com.example.myapplication3.activity.dao.Dao;

public class Fragment1 extends Fragment implements View.OnClickListener{
    private String name;
    private State st;
    private Dao dao ;
    private Button yes;
    private Button no;

    private RadioGroup rg_tab_bar;
    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        yes = view.findViewById(R.id.yes);
        yes.setOnClickListener(this);
        no = view.findViewById(R.id.no);
        no.setOnClickListener(this);
        st = new State();
        dao = new Dao();
        return view;
    }



    @Override
    public void onClick(View view) {


        switch (view.getId())
        {
            case R.id.yes:
                st.setName(name);
                st.setState("是");
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dao.insertSubmit(st);
                    }
                });thread.start();
                Toast toast = Toast.makeText(getContext(),"您已经完成订餐!",Toast.LENGTH_SHORT);
                toast.show();
                disableAllButton();
                break;

            case R.id.no:
                st.setName(name);
                st.setState("否");
                Thread thread1 = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dao.insertSubmit(st);
                    }
                });thread1.start();
                disableAllButton();
                Toast toast1 = Toast.makeText(getContext(),"您没有订餐,记得按时就餐!",Toast.LENGTH_SHORT);
                toast1.show();
                break;

        }
    }

//    更新传过来的数据
    public void upData(String data)
    {
        name = data;
    }

    private void disableAllButton()
    {
        yes.setEnabled(false);
        no.setEnabled(false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 设置点击事件监听
        view.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("WrongViewCast")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 获取点击事件的坐标
                float x = event.getX();
                float y = event.getY();
                MainActivity2 mainActivity2 = (MainActivity2) getActivity();
                if(mainActivity2 != null)
                {
                     rg_tab_bar = mainActivity2.findViewById(R.id.rg_tab_bar);
                }
                // 判断点击事件的类型
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:// 当按下时执行的逻辑
                        rg_tab_bar.setVisibility(View.VISIBLE);
                        break;
                    case MotionEvent.ACTION_MOVE:// 当移动时执行的逻辑
                        rg_tab_bar.setVisibility(View.VISIBLE);
                        break;
                    case MotionEvent.ACTION_UP:// 当抬起时执行的逻辑
                        // 当松开时执行延迟操作
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                // 在这里将 rg_tab_bar 设置为不可见
                                rg_tab_bar.setVisibility(View.GONE);
                            }
                        }, 3000);
                        break;
                }

                // 返回 true 表示已经处理了点击事件,否则返回 false
                return true;
            }
        });
    }

}

  myFragmentPagerAdapter(适配器处理)

package com.example.myapplication3.activity.fragmentpageradapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.example.myapplication3.activity.activity.MainActivity2;
import com.example.myapplication3.activity.fragment.Fragment1;
import com.example.myapplication3.activity.fragment.Fragment2;
import com.example.myapplication3.activity.fragment.Fragment3;
import com.example.myapplication3.activity.fragment.Fragment4;

public class myFragmentPagerAdapter extends FragmentStateAdapter {

    private final int PAGER_COUNT = 4;
    private Fragment1 fragment1 = null;
    private Fragment2 fragment2 = null;
    private Fragment3 fragment3 = null;
    private Fragment4 fragment4 = null;

    public myFragmentPagerAdapter( FragmentManager fragmentManager,  Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
    }



    @Override
    public Fragment createFragment(int position) {

        Fragment fragment = null;
        switch (position) {
            case MainActivity2.PAGE_ONE:
                fragment = fragment1;
                break;
            case MainActivity2.PAGE_TWO:
                fragment = fragment2;
                break;
            case MainActivity2.PAGE_THREE:
                fragment = fragment3;
                break;
            case MainActivity2.PAGE_FOUR:
                fragment = fragment4;
                break;
        }
        return fragment;
    }

    @Override
    public int getItemCount() {
        return PAGER_COUNT;
    }


}

  MainActivity2(主页面用于安排fragment)

package com.example.myapplication3.activity.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication3.R;
import com.example.myapplication3.activity.fragmentpageradapter.myFragmentPagerAdapter;
import com.example.myapplication3.activity.fragment.Fragment1;

public class MainActivity2 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private String name;
    private RadioButton a;
    private RadioButton b;
    private RadioButton c;
    private RadioButton d;
    private RadioGroup rg_tab_bar;
    private ViewPager2 viewpage;
    private myFragmentPagerAdapter myFragmentPagerAdapter;
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindView();
        a.setChecked(true);
//        rg_tab_bar.setVisibility(View.GONE);
    }

    private void bindView()
    {
        Bundle bundle = getIntent().getExtras();
        if(bundle != null)
        {
            name = bundle.getString("username");
        }
        a = (RadioButton) findViewById(R.id.a);
        b = (RadioButton) findViewById(R.id.b);
        c = (RadioButton) findViewById(R.id.b);
        d = (RadioButton) findViewById(R.id.d);
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        viewpage = (ViewPager2) findViewById(R.id.viewpager);

        rg_tab_bar.setOnCheckedChangeListener(this);
        myFragmentPagerAdapter = new myFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle());
        viewpage.setAdapter(myFragmentPagerAdapter);
        viewpage.setCurrentItem(0);
        viewpage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case PAGE_ONE:
                        if (myFragmentPagerAdapter.createFragment(PAGE_ONE) != null)
                        {
                            Fragment1 fragment1 = (Fragment1) myFragmentPagerAdapter.createFragment(PAGE_ONE);
                            fragment1.upData(name);
                        }
                        rg_tab_bar.check(R.id.a);
//                        rg_tab_bar.setVisibility(View.GONE);
                        break;

  activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.activity.MainActivity2">

    <RelativeLayout
        android:id="@+id/ly_top_bar"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:id="@+id/txt_topbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="信息"
            android:textSize="18sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="2px"
            android:layout_alignParentBottom="true"/>

    </RelativeLayout>

    <RadioGroup
        android:id="@+id/rg_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/a"
            style="@style/tab_menu_item"
            android:text="每日营养餐"/>

        <RadioButton
            android:id="@+id/b"
            style="@style/tab_menu_item"
            android:text="拍照上传"/>

        <RadioButton
            android:id="@+id/c"
            style="@style/tab_menu_item"
            android:text="聊天"/>

        <RadioButton
            android:id="@+id/d"
            style="@style/tab_menu_item"
            android:text="我的主页"/>
    </RadioGroup>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/rg_tab_bar"
        android:layout_below="@id/ly_top_bar"/>





</RelativeLayout>

  themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.MyApplication3" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.MyApplication3" parent="Base.Theme.MyApplication3" />
    
    <style name="tab_menu_item">

        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:drawableTop">@drawable/tab_menu_all</item>
        <item name="android:paddingTop">3dp</item>
        <item name="android:textSize">18sp</item>
    </style>
</resources>

  tab_menu_all.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/tab_all_pressed" android:state_checked="true"/>
    <item android:drawable="@mipmap/tab_all_normal"/>
</selector>

  

 

 

tab_all_normal

 

tab_all_pressed

 

posted @ 2024-05-08 00:29  *太¥^白%  阅读(8)  评论(0编辑  收藏  举报