android 点击后颜色没有变化是为什么?

作者站长头像
站长
· 阅读数 7

如题,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>
回复
1个回答
avatar
test
2024-06-28

这边没有将数据绑定视图的代码贴出来,主要问题在绑定部分。是因为没有设置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);
    }

}
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容