在线咨询
微信咨询
服务热线
服务热线:15639912513
TOP
当前位置:
首页 > 新闻中心> 安卓课堂>Android使用Navigation的小例子

Android使用Navigation的小例子

发布时间:2020-02-14 浏览:120次

郑州app开发使用Navigation的小例子,也正是通过用Navigation来进行ViewModel的操作。下面是java代码:

MainActivity.java

package cn.xhhkj.cao;


import androidx.appcompat.app.AppCompatActivity;

import androidx.databinding.DataBindingUtil;

import androidx.lifecycle.ViewModelProviders;


import android.os.Bundle;



public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

}

可以看出,基本没有变化。

MyViewModel.java

package cn.xhhkj.cao;


import androidx.lifecycle.MutableLiveData;

import androidx.lifecycle.ViewModel;


public class MyViewModel extends ViewModel {

   private MutableLiveData<Integer> number;

   public MutableLiveData<Integer>getNumber(){

       if (number == null){

           number = new MutableLiveData<>();

           number.setValue(0);

       }

       return number;

   }

     public void add(int x){

       number.setValue(number.getValue()+x);

       if (number.getValue()<0){

           number.setValue(0);

       }

     }

}

这里就能看出,数据处理都在这里了。

MasterFragment.java

package cn.xhhkj.cao;



import android.os.Bundle;


import androidx.databinding.DataBindingUtil;

import androidx.fragment.app.Fragment;

import androidx.lifecycle.ViewModelProvider;

import androidx.lifecycle.ViewModelProviders;

import androidx.navigation.NavController;

import androidx.navigation.Navigation;


import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.SeekBar;


import cn.xhhkj.cao.databinding.FragmentMasterBinding;



/**

 * A simple {@link Fragment} subclass.

 */

public class MasterFragment extends Fragment {



    public MasterFragment() {

        // Required empty public constructor

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        final MyViewModel myViewModel;

        myViewModel= ViewModelProviders.of(getActivity()).get(MyViewModel.class);

        FragmentMasterBinding binding;

        binding= DataBindingUtil.inflate(inflater,R.layout.fragment_master,container,false);

        binding.setData(myViewModel);

        binding.setLifecycleOwner(getActivity());

        binding.button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                NavController controller= Navigation.findNavController(v);

                controller.navigate(R.id.action_masterFragment_to_detailFragment);

            }

        });

//        binding.seekBar.setProgress(myViewModel.getNumber().getValue());

        binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                myViewModel.getNumber().setValue(progress);

            }


            @Override

            public void onStartTrackingTouch(SeekBar seekBar) {


            }


            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {


            }

        });

        return binding.getRoot();

    }


}

DetailFragment.java

package cn.xhhkj.cao;



import android.os.Bundle;


import androidx.databinding.DataBindingUtil;

import androidx.fragment.app.Fragment;

import androidx.lifecycle.ViewModelProvider;

import androidx.lifecycle.ViewModelProviders;

import androidx.navigation.NavController;

import androidx.navigation.Navigation;


import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;


import cn.xhhkj.cao.databinding.FragmentDetailBinding;



/**

 * A simple {@link Fragment} subclass.

 */

public class DetailFragment extends Fragment {



    public DetailFragment() {

        // Required empty public constructor

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        MyViewModel myViewModel;

        myViewModel= ViewModelProviders.of(getActivity()).get(MyViewModel.class);

        FragmentDetailBinding binding;

        binding= DataBindingUtil.inflate(inflater,R.layout.fragment_detail,container,false);

        binding.setData(myViewModel);

        binding.setLifecycleOwner(getActivity());

        binding.button4.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                NavController controller= Navigation.findNavController(v);

                controller.navigate(R.id.action_detailFragment_to_masterFragment);

            }

        });

        return binding.getRoot();

        // Inflate the layout for this fragment

        //return inflater.inflate(R.layout.fragment_detail, container, false);

    }


}

下面是布局代码

activity_main.xml

{?xml version="1.0" encoding="utf-8"?}

{androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

                                                   xmlns:app="http://schemas.android.com/apk/res-auto"

                                                   android:orientation="vertical" android:layout_width="match_parent"

                                                   android:layout_height="match_parent"}


    {fragment

            android:id="@+id/fragment2"

            android:name="androidx.navigation.fragment.NavHostFragment"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            app:defaultNavHost="true"

            app:navGraph="@navigation/nav" /}

{/androidx.constraintlayout.widget.ConstraintLayout}

 fragment_master.xml

{?xml version="1.0" encoding="utf-8"?}

{layout 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"}


    {data}

        {variable

                name="data"

                type="cn.xhhkj.cao.MyViewModel" /}

    {/data}


    {androidx.constraintlayout.widget.ConstraintLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            tools:context=".MasterFragment"}



        {TextView

                android:id="@+id/textView"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:maxLength="10"

                android:text="@{String.valueOf(data.number)}"

                android:textSize="30sp"

                app:layout_constraintBottom_toBottomOf="parent"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintHorizontal_bias="0.538"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toTopOf="parent"

                app:layout_constraintVertical_bias="0.255" /}


        {SeekBar

                android:id="@+id/seekBar"

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:max="30"

                app:layout_constraintBottom_toBottomOf="parent"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintHorizontal_bias="0.0"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toTopOf="parent"

                app:layout_constraintVertical_bias="0.431" /}


        {Button

                android:id="@+id/button"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginStart="132dp"

                android:text="进入"

                app:layout_constraintBottom_toBottomOf="parent"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintHorizontal_bias="0.219"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toTopOf="parent"

                app:layout_constraintVertical_bias="0.579" /}

    {/androidx.constraintlayout.widget.ConstraintLayout}

{/layout}

  fragment_detail.xml

{?xml version="1.0" encoding="utf-8"?}

{layout 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"}


    {data}

        {variable

                name="data"

                type="cn.xhhkj.cao.MyViewModel" /}

    {/data}


    {androidx.constraintlayout.widget.ConstraintLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            tools:context=".DetailFragment"}


        {TextView

                android:id="@+id/textView2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@{String.valueOf(data.number)}"

                android:textSize="30sp"

                app:layout_constraintBottom_toBottomOf="parent"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toTopOf="parent"

                app:layout_constraintVertical_bias="0.21" /}


        {Button

                android:id="@+id/button2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="+"

                android:textSize="30sp"

                android:onClick="@{()-}data.add(+1)}"

                app:layout_constraintBottom_toBottomOf="parent"

                app:layout_constraintEnd_toStartOf="@+id/button3"

                app:layout_constraintHorizontal_bias="0.5"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toTopOf="parent"

                app:layout_constraintVertical_bias="0.43" /}


        {Button

                android:id="@+id/button3"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="-"

                android:textSize="30sp"

                android:onClick="@{()-}data.add(-1)}"

                app:layout_constraintBottom_toBottomOf="@+id/button2"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintHorizontal_bias="0.5"

                app:layout_constraintStart_toEndOf="@+id/button2"

                app:layout_constraintTop_toTopOf="@+id/button2"

                app:layout_constraintVertical_bias="0.0" /}


        {Button

                android:id="@+id/button4"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="返回"

                app:layout_constraintBottom_toBottomOf="parent"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintHorizontal_bias="0.513"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toTopOf="parent"

                app:layout_constraintVertical_bias="0.67" /}

    {/androidx.constraintlayout.widget.ConstraintLayout}

{/layout}

以上就是全部代码了。

补充下,需要在gradle scripts这个文件夹里面build.gradle(module:app)这个文件里面添加这行代码

dataBinding {

            enabled true

        }


TAG
120
该内容对我有帮助