P17 Navigation组件,让页面切换无比简单 1
(1)
(2)
(3)
拖动seekbar改变Textview的值,通过button传递到detail的TextView,而+也可以改变这个值,其实这个值是同一个ViewModel的数。
package com.test;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.test.databinding.FragmentMasterBinding;
public class MasterFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// return inflater.inflate(R.layout.fragment_master, container, false);
final DataBindingVD dataBindingVD = ViewModelProviders.of(getActivity()).get(DataBindingVD.class);
final FragmentMasterBinding binding= DataBindingUtil.inflate(inflater,
R.layout.fragment_master,container,false);
binding.setData(dataBindingVD);
binding.setLifecycleOwner(getActivity());
Button button=binding.button6;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavController controller= Navigation.findNavController(v);
controller.navigate(R.id.action_masterFragment_to_detailFragment3);
}
});
SeekBar seekBar=binding.seekBar;
seekBar.setProgress(dataBindingVD.num.getValue());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
dataBindingVD.num.setValue(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return binding.getRoot();
}
}
package com.test;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.test.databinding.FragmentDetail3Binding;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DataBindingVD dataBindingVD= ViewModelProviders.of(getActivity()).get(DataBindingVD.class);
FragmentDetail3Binding binding= DataBindingUtil.inflate(inflater,
R.layout.fragment_detail3, container, false);
binding.setData(dataBindingVD);
binding.setLifecycleOwner(getActivity());
binding.button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavController controller= Navigation.findNavController(v);
controller.navigate(R.id.action_detailFragment3_to_masterFragment);
}
});
return binding.getRoot();
}
}