重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
郑州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
public MutableLiveData
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
}