android 点击后颜色没有变化是为什么?
如题,android 点击后颜色没有变化是为什么? 以下是具体代码
public class HomeFragmentVM extends ViewModel { @SuppressLint("StaticFieldLeak")
private HomeFragment fragment;
public static final String SORT_COLUMN_AUTO = "auto";
public static final String SORT_COLUMN_ACTIVE = "active";
public static final String SORT_COLUMN_DISTANCE = "distance";
public static final String SORT_COLUMN_MATCH = "match";
public static final String SORT_ICON_DOWN = "sort:icon:down";
public static final String SORT_ICON_UP = "sort:icon:up";
public final MutableLiveData<SearchForm> searchForm = new MutableLiveData<>();
@SuppressLint("StaticFieldLeak")
public View rootView;
private static class SearchForm {
public String sortType = "asc";
public String sortColumn = SORT_COLUMN_ACTIVE;
}
public HomeFragmentVM(HomeFragment fragment, View rootView) {
this.fragment = fragment;
this.rootView = rootView;
this.init();
}
private void init() {
this.searchForm.setValue(new SearchForm());
}
public int getSortTextColor(String sortColumn) {
SearchForm searchForm = this.searchForm.getValue();
int color = R.color.color_333;
int colorActive = R.color.color_red_1;
if (searchForm == null) {
return color;
}
if (searchForm.sortColumn.equals(sortColumn)) {
return colorActive;
}
return color;
}
public void handleSort(String column) {
SearchForm searchForm = this.searchForm.getValue();
if (searchForm.sortColumn.equals(column) && column.equals(SORT_COLUMN_AUTO)) {
return ;
}
if (searchForm.sortColumn.equals(column)) {
// 重复点击搜索项 - 取消排序
searchForm.sortColumn = null;
searchForm.sortType = null;
} else {
searchForm.sortColumn = column;
if (column.equals(SORT_COLUMN_AUTO)) {
searchForm.sortType = null;
} else {
if (searchForm.sortType == null) {
searchForm.sortType = "asc";
}
if (searchForm.sortType.equals("asc")) {
searchForm.sortType = "desc";
}
if (searchForm.sortType.equals("desc")) {
searchForm.sortType = null;
}
}
}
// 更新数据
this.searchForm.setValue(searchForm);
}
视图代码如下:
<?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>
<import type="com.gv.yinyuan.app.android.consts.AppConsts"/>
<variable name="vm" type="com.gv.yinyuan.app.android.vm.HomeFragmentVM"/>
</data>
<TextView
style="@style/fragment_home_sort_text"
android:text="综合"
android:textColor='@{vm.getSortTextColor(vm.SORT_COLUMN_AUTO)}'
android:layout_marginEnd="10dp"
android:onClick='@{(view) -> vm.handleSort(vm.SORT_COLUMN_AUTO)}'
/>
</layout>
回答:
这边没有将数据绑定视图的代码贴出来,主要问题在绑定部分。是因为没有设置viewmodel 的生命周期所有者导致视图不更新。
public class HomeFragment extends Fragment { private FragmentHomeBinding binding;
private HomeFragmentVM vm;
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
// 就这句代码之前没有设置导致UI不更新。
this.binding.setLifecycleOwner(this.getActivity());
return this.binding.getRoot();
}
@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.vm = new HomeFragmentVM(this, view);
this.binding.setVm(this.vm);
}
}
回答:
Data Binding 设置是这样吗
HomeFragmentBinding binding = DataBindingUtil.setContentView(this, R.layout.fragment_home);HomeFragmentVM viewModel = new HomeFragmentVM(fragment, rootView);
binding.setVm(viewModel);
用 ObservableField:
public final ObservableField<SearchForm> searchForm = new ObservableField<>();// ...
public void handleSort(String column) {
SearchForm searchForm = this.searchForm.get();
// 更新 searchForm
// ...
this.searchForm.set(searchForm); // 触发绑定更新
}
回答:
layout 文件写错了android:textColor="@={model.xxx}"
应该采用双向绑定的方式,然后这个model.xxx
应该是一个 Observable
,
然后点击 TextView
后执行操作,过程中更新改变 model.xxx
,这样就可以实现点击变化颜色
以上是 android 点击后颜色没有变化是为什么? 的全部内容, 来源链接: utcz.com/p/945305.html