diff --git a/app/build.gradle b/app/build.gradle index 624d1fc..45de868 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,7 +23,12 @@ android { sourceSets { main { res { - srcDirs 'src/main/res', 'src/main/res/layout/resetpwd', 'src/main/res/layout/resetpwd/layout', 'src/main/res/layout/main', 'src/main/res/layout/main/layout', 'src/main/res/layout/main/layout/forum', 'src/main/res/layout/main/layout/forum/layout', 'src/main/res/layout/main/layout/visitor', 'src/main/res/layout/main/layout/visitor/layout','src/main/res/layout/main/layout/info','src/main/res/layout/main/layout/info/layout' + srcDirs 'src/main/res', 'src/main/res/layout/resetpwd', 'src/main/res/layout/resetpwd/layout', + 'src/main/res/layout/main', 'src/main/res/layout/main/layout', + 'src/main/res/layout/main/layout/forum', 'src/main/res/layout/main/layout/forum/layout', + 'src/main/res/layout/main/layout/visitor', 'src/main/res/layout/main/layout/visitor/layout', + 'src/main/res/layout/main/layout/info','src/main/res/layout/main/layout/info/layout', + 'src/main/res/layout/main/layout/garbage','src/main/res/layout/main/layout/garbage/layout' } } } diff --git a/app/src/main/java/com/community/pocket/data/adapter/GarbageAdpter.java b/app/src/main/java/com/community/pocket/data/adapter/GarbageAdpter.java new file mode 100644 index 0000000..faacc8b --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/adapter/GarbageAdpter.java @@ -0,0 +1,48 @@ +package com.community.pocket.data.adapter; + +import android.content.Context; +import android.widget.ArrayAdapter; + +import com.community.pocket.data.model.GarbageSorting; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class GarbageAdpter extends ArrayAdapter { + + private List dataList; + private List list = new ArrayList<>(); + private Map map = new HashMap<>(); + + + public GarbageAdpter(List dataList, Context context) { + super(context, android.R.layout.simple_list_item_1); + addAll(dataList); + } + + public void addAll(List dataList) { + this.dataList = dataList; + this.list.clear(); + this.map.clear(); + for (GarbageSorting garbageSorting : dataList) { + this.list.add(garbageSorting.getName()); + this.map.put(garbageSorting.getName(), garbageSorting); + } + clear(); + addAll(this.list); + } + + public GarbageSorting get(String name) { + return this.map.get(name); + } + + public boolean containsKey(String name) { + return this.list.contains(name); + } + + public List getList() { + return list; + } +} diff --git a/app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java b/app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java new file mode 100644 index 0000000..ffaac14 --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java @@ -0,0 +1,42 @@ +package com.community.pocket.data.main.garbage; + + +import com.community.pocket.R; +import com.community.pocket.data.model.GarbageSorting; +import com.community.pocket.ui.main.ui.garbage.GarbageSortingResponse; + +import java.util.ArrayList; +import java.util.List; + +/** + * 垃圾分类请求接口 + * TODO 完善逻辑 + */ +public class GarbageSortingRequest { + private static volatile GarbageSortingRequest instance; + + private GarbageSortingRequest() { + } + + public static GarbageSortingRequest getInstance() { + if (instance == null) { + instance = new GarbageSortingRequest(); + } + return instance; + } + + //检索垃圾 + public GarbageSortingResponse search() { + List garbageSortings = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + GarbageSorting garbageSorting = new GarbageSorting(); + garbageSorting.setName("test" + i); + garbageSorting.setSorting("有害垃圾"); + garbageSortings.add(garbageSorting); + } + GarbageSortingResponse garbageSortingResponse = new GarbageSortingResponse(); + garbageSortingResponse.setSuccess(R.string.garbage_search_ok); + garbageSortingResponse.setBody(garbageSortings); + return garbageSortingResponse; + } +} diff --git a/app/src/main/java/com/community/pocket/data/model/GarbageSorting.java b/app/src/main/java/com/community/pocket/data/model/GarbageSorting.java new file mode 100644 index 0000000..bb2fc41 --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/model/GarbageSorting.java @@ -0,0 +1,35 @@ +package com.community.pocket.data.model; + +//垃圾分类信息 +public class GarbageSorting { + //名称 + private String name; + //分类 + private String sorting; + //垃圾图片 + private String image; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSorting() { + return sorting; + } + + public void setSorting(String sorting) { + this.sorting = sorting; + } + + public String getImage() { + return image; + } + + public void setImage(String image) { + this.image = image; + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/MainFragment.java b/app/src/main/java/com/community/pocket/ui/main/MainFragment.java index ce2935d..eacf1b3 100644 --- a/app/src/main/java/com/community/pocket/ui/main/MainFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/MainFragment.java @@ -30,8 +30,8 @@ public abstract class MainFragment extends BaseFragment { */ @RequiresApi(api = Build.VERSION_CODES.KITKAT) protected NavController ctrl() { - if (navController == null) { - navController = Navigation.findNavController(Objects.requireNonNull(getView()).findViewById(fragmentId())); + if (navController == null && getView() != null) { + navController = Navigation.findNavController(getView().findViewById(fragmentId())); } return navController; } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java index e12acb1..a00a9dc 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java @@ -1,14 +1,9 @@ package com.community.pocket.ui.main.ui.forum.post; -import android.annotation.SuppressLint; import android.os.Build; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; import android.text.Editable; import android.text.TextWatcher; -import android.util.Log; import android.view.View; import android.widget.AutoCompleteTextView; import android.widget.Toast; @@ -22,13 +17,12 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.data.adapter.ForumSearchAdapter; import com.community.pocket.ui.listener.MyTextChange; +import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView; import com.community.pocket.util.PropertiesUtil; -import org.jetbrains.annotations.NotNull; import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ViewInject; -import java.lang.reflect.Method; import java.util.List; import java.util.Objects; @@ -50,10 +44,7 @@ public class ForumPostComplainFragment extends ForumPostContent { private ForumPostComplainViewModel viewModel; - //AutoCompleteTextView的doBeforeTextChanged方法 - private Method doBeforeTextChanged; - //AutoCompleteTextView的doAfterTextChanged方法 - private Method doAfterTextChanged; + private MyAutoCompleteTextView myAutoCompleteTextView; @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override @@ -62,6 +53,8 @@ public class ForumPostComplainFragment extends ForumPostContent { viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostComplainViewModel.class); + myAutoCompleteTextView = new MyAutoCompleteTextView(searchName, viewModel); + //监听表单校验状态 viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer() { @Override @@ -110,13 +103,7 @@ public class ForumPostComplainFragment extends ForumPostContent { viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString()); } - long time = System.currentTimeMillis(); - //输入的字符间隔时间 小于700毫秒 移除以前的handler 延时600毫秒执行 - if (searchName.getTag() != null && time - (Long) searchName.getTag() < 700) { - searchHandler.removeMessages(1); - } - searchHandler.sendEmptyMessageDelayed(1, 600); - searchName.setTag(time); + myAutoCompleteTextView.searchHandler(); } }); @@ -138,43 +125,6 @@ public class ForumPostComplainFragment extends ForumPostContent { }); } - //延时搜索的handler - private Handler searchHandler = new Handler(Looper.getMainLooper()) { - public void handleMessage(@NotNull Message msg) { - dealSearchHint(); - } - }; - - /** - * 根据用户输入的字符去调用接口查询投诉人 - */ - private void dealSearchHint() { - String searchContent = searchName.getText().toString(); - if (searchContent.isEmpty()) { - return; - } - viewModel.searchPeople(searchContent); - } - - @SuppressLint("SoonBlockedPrivateApi") - private void refreshDropList() { - try { - if (doAfterTextChanged == null) { - Class autoCompleteTextView = AutoCompleteTextView.class; - doBeforeTextChanged = autoCompleteTextView.getDeclaredMethod("doBeforeTextChanged"); - doBeforeTextChanged.setAccessible(true); - doAfterTextChanged = autoCompleteTextView.getDeclaredMethod("doAfterTextChanged"); - doAfterTextChanged.setAccessible(true); - } - -// doBeforeTextChanged.invoke(appointment); - doAfterTextChanged.invoke(searchName); - } catch (Exception e) { - Log.e("", e.toString()); - Toast.makeText(getContext(), R.string.load_data_err, Toast.LENGTH_LONG).show(); - } - } - /** * 搜索框 */ @@ -188,6 +138,6 @@ public class ForumPostComplainFragment extends ForumPostContent { } else { nameList.addAll(stringList); } - refreshDropList(); + myAutoCompleteTextView.refreshDropList(); } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java index 57bb11d..23b9c9a 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java @@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData; import com.community.pocket.R; import com.community.pocket.data.main.forum.ForumPostRequest; +import com.community.pocket.ui.main.ui.share.SearchViewModel; import com.community.pocket.util.Valid; import com.community.pocket.util.ValidUtil; @@ -12,7 +13,7 @@ import java.util.List; /** * 投诉贴数据管理 */ -class ForumPostComplainViewModel extends ForumPostViewModel { +class ForumPostComplainViewModel extends ForumPostViewModel implements SearchViewModel { //投诉帖表单状态 private MutableLiveData forumPostFormState = new MutableLiveData<>(); @@ -68,8 +69,13 @@ class ForumPostComplainViewModel extends ForumPostViewModel { * * @param name 投诉人 */ - void searchPeople(String name) { + private void searchPeople(String name) { ForumPostResponse> forumPostResponse = forumPostRequest.searchPeople(name); searchPeople.setValue(forumPostResponse); } + + @Override + public void search(String content) { + searchPeople(content); + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageFragment.java index e169225..68c77e4 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageFragment.java @@ -1,16 +1,42 @@ package com.community.pocket.ui.main.ui.garbage; +import android.os.Build; +import android.view.View; + +import androidx.annotation.RequiresApi; + import com.community.pocket.R; -import com.community.pocket.ui.main.TestMainFragment; +import com.community.pocket.ui.main.MainFragment; import org.xutils.view.annotation.ContentView; +import org.xutils.view.annotation.Event; + + +@ContentView(R.layout.garbage) +public class GarbageFragment extends MainFragment { + @Override + protected int fragmentId() { + return R.id.grabage_fragment; + } + //跳转到垃圾分类界面 + @RequiresApi(api = Build.VERSION_CODES.KITKAT) + @Event(value = R.id.garbage_sorting) + private void sorting(View view) { + ctrl().navigate(R.id.garbageSortingFragment); + changeColor(view); + } -@ContentView(R.layout.fragment_notifications) -public class GarbageFragment extends TestMainFragment { + //跳转到废品回收界面 + @RequiresApi(api = Build.VERSION_CODES.KITKAT) + @Event(value = R.id.garbage_waste) + private void waste(View view) { + ctrl().navigate(R.id.garbageWasteFragment); + changeColor(view); + } @Override - protected int viewId() { - return R.id.text_notifications; + protected int[] button_ids() { + return new int[]{R.id.garbage_sorting, R.id.garbage_waste}; } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingFragment.java new file mode 100644 index 0000000..361e06d --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingFragment.java @@ -0,0 +1,116 @@ +package com.community.pocket.ui.main.ui.garbage; + +import android.os.Bundle; +import android.text.Editable; +import android.view.View; +import android.widget.AutoCompleteTextView; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; + +import com.community.pocket.R; +import com.community.pocket.data.adapter.GarbageAdpter; +import com.community.pocket.data.model.GarbageSorting; +import com.community.pocket.ui.BaseFragment; +import com.community.pocket.ui.listener.MyTextChange; +import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView; + +import org.xutils.view.annotation.ContentView; +import org.xutils.view.annotation.ViewInject; + +import java.util.List; + +@ContentView(R.layout.garbage_sorting) +public class GarbageSortingFragment extends BaseFragment { + + @ViewInject(R.id.search_sorting) + private AutoCompleteTextView searchSorting; + + //垃圾名 + @ViewInject(R.id.name) + private TextView name; + + //垃圾分类 + @ViewInject(R.id.sorting) + private TextView sorting; + + //垃圾图片 + @ViewInject(R.id.image) + private ImageView image; + + private MyAutoCompleteTextView myAutoCompleteTextView; + + private GarbageAdpter garbageAdpter; + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + GarbageSortingViewModel viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(GarbageSortingViewModel.class); + + myAutoCompleteTextView = new MyAutoCompleteTextView(searchSorting, viewModel); + + //监听垃圾分类检索状态 + viewModel.getGarbageSortingResponse().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(GarbageSortingResponse garbageSortingResponse) { + if (garbageSortingResponse == null) { + return; + } + + if (garbageSortingResponse.getSuccess() != null) { + initSearch(garbageSortingResponse.getBody()); + } + + if (garbageSortingResponse.getError() != null) { + Toast.makeText(getContext(), garbageSortingResponse.ok(getContext()), Toast.LENGTH_SHORT).show(); + } + } + }); + + //添加监听触发搜索 + searchSorting.addTextChangedListener(new MyTextChange() { + @Override + public void afterTextChanged(Editable s) { + if (garbageAdpter != null && garbageAdpter.containsKey(s.toString())) { + initView(garbageAdpter.get(s.toString())); + } + + myAutoCompleteTextView.searchHandler(); + } + }); + + } + + /** + * 初始化垃圾分类信息 + * + * @param garbageSorting 垃圾分类 + */ + private void initView(GarbageSorting garbageSorting) { + name.setText(garbageSorting.getName()); + sorting.setText(garbageSorting.getSorting()); + } + + /** + * 检索垃圾信息 + * + * @param garbageSortings 垃圾分类信息 + */ + private void initSearch(List garbageSortings) { + // 设置适配器 + if (garbageAdpter == null) { + garbageAdpter = new GarbageAdpter(garbageSortings, getContext()); + // 将适配器与当前AutoCompleteTextView控件绑定 + searchSorting.setAdapter(garbageAdpter); + } else { + garbageAdpter.addAll(garbageSortings); + } + myAutoCompleteTextView.refreshDropList(); + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingResponse.java new file mode 100644 index 0000000..ff1db60 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingResponse.java @@ -0,0 +1,9 @@ +package com.community.pocket.ui.main.ui.garbage; + +import com.community.pocket.data.model.GarbageSorting; +import com.community.pocket.ui.main.ui.share.Response; + +import java.util.List; + +public class GarbageSortingResponse extends Response> { +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingViewModel.java new file mode 100644 index 0000000..bc340c8 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingViewModel.java @@ -0,0 +1,31 @@ +package com.community.pocket.ui.main.ui.garbage; + +import androidx.lifecycle.MutableLiveData; + +import com.community.pocket.data.main.garbage.GarbageSortingRequest; +import com.community.pocket.ui.main.ui.share.BaseViewModel; +import com.community.pocket.ui.main.ui.share.SearchViewModel; + +/** + * 垃圾分类数据管理 + */ +public class GarbageSortingViewModel extends BaseViewModel implements SearchViewModel { + + //垃圾分类信息请求状态 + private MutableLiveData garbageSortingResponse = new MutableLiveData<>(); + + MutableLiveData getGarbageSortingResponse() { + return garbageSortingResponse; + } + + @Override + protected GarbageSortingRequest getRequest() { + return GarbageSortingRequest.getInstance(); + } + + @Override + public void search(String content) { + GarbageSortingResponse response = getRequest().search(); + garbageSortingResponse.setValue(response); + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageWasteFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageWasteFragment.java new file mode 100644 index 0000000..0a75c17 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageWasteFragment.java @@ -0,0 +1,10 @@ +package com.community.pocket.ui.main.ui.garbage; + +import com.community.pocket.R; +import com.community.pocket.ui.BaseFragment; + +import org.xutils.view.annotation.ContentView; + +@ContentView(R.layout.garbage_waste) +public class GarbageWasteFragment extends BaseFragment { +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/share/MyAutoCompleteTextView.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/MyAutoCompleteTextView.java new file mode 100644 index 0000000..c6cdb91 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/MyAutoCompleteTextView.java @@ -0,0 +1,79 @@ +package com.community.pocket.ui.main.ui.share; + +import android.annotation.SuppressLint; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; +import android.util.Log; +import android.widget.AutoCompleteTextView; + +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Method; + +/** + * 动态更新自动搜索框数据 + */ +public class MyAutoCompleteTextView { + //AutoCompleteTextView的doBeforeTextChanged方法 + private Method doBeforeTextChanged; + //AutoCompleteTextView的doAfterTextChanged方法 + private Method doAfterTextChanged; + + private AutoCompleteTextView view; + + private SearchViewModel searchViewModel; + + public MyAutoCompleteTextView(AutoCompleteTextView view, SearchViewModel searchViewModel) { + this.view = view; + this.searchViewModel = searchViewModel; + } + + //延时搜索的handler + private Handler searchHandler = new Handler(Looper.getMainLooper()) { + public void handleMessage(@NotNull Message msg) { + dealSearchHint(); + } + }; + + + /** + * 根据用户输入的字符去调用接口查询 + */ + private void dealSearchHint() { + String searchContent = view.getText().toString(); + if (searchContent.isEmpty()) { + return; + } +// appointmentViewModel.searchPeople(searchContent); + searchViewModel.search(searchContent); + } + + @SuppressLint("SoonBlockedPrivateApi") + public void refreshDropList() { + try { + if (doAfterTextChanged == null) { + Class autoCompleteTextView = AutoCompleteTextView.class; + doBeforeTextChanged = autoCompleteTextView.getDeclaredMethod("doBeforeTextChanged"); + doBeforeTextChanged.setAccessible(true); + doAfterTextChanged = autoCompleteTextView.getDeclaredMethod("doAfterTextChanged"); + doAfterTextChanged.setAccessible(true); + } + +// doBeforeTextChanged.invoke(appointment); + doAfterTextChanged.invoke(view); + } catch (Exception e) { + Log.e("", e.toString()); + } + } + + public void searchHandler() { + long time = System.currentTimeMillis(); + //输入的字符间隔时间 小于700毫秒 移除以前的handler 延时600毫秒执行 + if (view.getTag() != null && time - (Long) view.getTag() < 700) { + searchHandler.removeMessages(1); + } + searchHandler.sendEmptyMessageDelayed(1, 600); + view.setTag(time); + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/share/SearchViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/SearchViewModel.java new file mode 100644 index 0000000..84612f7 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/SearchViewModel.java @@ -0,0 +1,9 @@ +package com.community.pocket.ui.main.ui.share; + +/** + * 搜索框请求数据源 + */ +public interface SearchViewModel { + //请求方法 + void search(String content); +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java index d5845e4..37c964f 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java @@ -1,13 +1,8 @@ package com.community.pocket.ui.main.ui.visitor.appointment; -import android.annotation.SuppressLint; import android.os.Build; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; import android.text.Editable; -import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; @@ -26,11 +21,11 @@ import com.community.pocket.R; import com.community.pocket.data.adapter.VisitorAdpter; import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.listener.MyTextChange; +import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView; import com.community.pocket.ui.main.ui.share.ShowWordCount; import com.community.pocket.ui.main.ui.visitor.VisitorFragment; import com.community.pocket.util.PropertiesUtil; -import org.jetbrains.annotations.NotNull; import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.Event; import org.xutils.view.annotation.ViewInject; @@ -65,10 +60,8 @@ public class VisitorAppointmentFragment extends BaseFragment { private VisitorViewModel appointmentViewModel; - //AutoCompleteTextView的doBeforeTextChanged方法 - private Method doBeforeTextChanged; - //AutoCompleteTextView的doAfterTextChanged方法 - private Method doAfterTextChanged; + //动态更新搜索框数据源 + private MyAutoCompleteTextView myAutoCompleteTextView; //预约人数据呀 private VisitorAdpter visitorAdpter; @@ -84,6 +77,8 @@ public class VisitorAppointmentFragment extends BaseFragment { appointmentViewModel = new ViewModelProvider(this, new VisitorAppointmentFactory()).get(VisitorViewModel.class); + myAutoCompleteTextView = new MyAutoCompleteTextView(appointment, appointmentViewModel); + //监听访客预约表单校验状态 appointmentViewModel.getVisitorFormState().observe(getViewLifecycleOwner(), new Observer() { @Override @@ -168,13 +163,7 @@ public class VisitorAppointmentFragment extends BaseFragment { } appointment.setTag(R.string.visitor_tag, s.toString()); - long time = System.currentTimeMillis(); - //输入的字符间隔时间 小于700毫秒 移除以前的handler 延时600毫秒执行 - if (appointment.getTag() != null && time - (Long) appointment.getTag() < 700) { - searchHandler.removeMessages(1); - } - searchHandler.sendEmptyMessageDelayed(1, 600); - appointment.setTag(time); + myAutoCompleteTextView.searchHandler(); } }); @@ -188,44 +177,6 @@ public class VisitorAppointmentFragment extends BaseFragment { } - //延时搜索的handler - private Handler searchHandler = new Handler(Looper.getMainLooper()) { - public void handleMessage(@NotNull Message msg) { - dealSearchHint(); - } - }; - - - /** - * 根据用户输入的字符去调用接口查询预约人 - */ - private void dealSearchHint() { - String searchContent = appointment.getText().toString(); - if (searchContent.isEmpty()) { - return; - } - appointmentViewModel.searchPeople(searchContent); - } - - @SuppressLint("SoonBlockedPrivateApi") - private void refreshDropList() { - try { - if (doAfterTextChanged == null) { - Class autoCompleteTextView = AutoCompleteTextView.class; - doBeforeTextChanged = autoCompleteTextView.getDeclaredMethod("doBeforeTextChanged"); - doBeforeTextChanged.setAccessible(true); - doAfterTextChanged = autoCompleteTextView.getDeclaredMethod("doAfterTextChanged"); - doAfterTextChanged.setAccessible(true); - } - -// doBeforeTextChanged.invoke(appointment); - doAfterTextChanged.invoke(appointment); - } catch (Exception e) { - Log.e("", e.toString()); - Toast.makeText(getContext(), R.string.load_data_err, Toast.LENGTH_LONG).show(); - } - } - /** * 搜索框 */ @@ -239,7 +190,7 @@ public class VisitorAppointmentFragment extends BaseFragment { } else { visitorAdpter.addAll(arrays); } - refreshDropList(); + myAutoCompleteTextView.refreshDropList(); } @RequiresApi(api = Build.VERSION_CODES.KITKAT) diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java index 82700a3..6fdf447 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java @@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel; import com.community.pocket.R; import com.community.pocket.data.main.visitor.VisitorRequest; +import com.community.pocket.ui.main.ui.share.SearchViewModel; import com.community.pocket.util.Valid; import com.community.pocket.util.ValidUtil; @@ -16,7 +17,7 @@ import java.util.Map; /** * 管理访客UI数据 */ -class VisitorViewModel extends ViewModel { +class VisitorViewModel extends ViewModel implements SearchViewModel { //访客预约表单校验信息 private MutableLiveData visitorFormState = new MutableLiveData<>(); @@ -57,7 +58,7 @@ class VisitorViewModel extends ViewModel { } //检索预约人 - void searchPeople(String name) { + private void searchPeople(String name) { VisitorResponse>> result = visitorRequest.searchPeople(name); searchPeopleReq.setValue(result); } @@ -72,4 +73,9 @@ class VisitorViewModel extends ViewModel { visitorFormState.setValue(new VisitorFormState(true)); } } + + @Override + public void search(String content) { + searchPeople(content); + } } diff --git a/app/src/main/res/layout/main/layout/fragment_notifications.xml b/app/src/main/res/layout/main/layout/garbage/layout/garbage.xml similarity index 55% rename from app/src/main/res/layout/main/layout/fragment_notifications.xml rename to app/src/main/res/layout/main/layout/garbage/layout/garbage.xml index 6e3bc65..e9827ef 100644 --- a/app/src/main/res/layout/main/layout/fragment_notifications.xml +++ b/app/src/main/res/layout/main/layout/garbage/layout/garbage.xml @@ -6,17 +6,19 @@ android:layout_height="match_parent" tools:context=".ui.main.ui.garbage.GarbageFragment"> - + + \ No newline at end of file diff --git a/app/src/main/res/layout/main/layout/garbage/layout/garbage_sorting.xml b/app/src/main/res/layout/main/layout/garbage/layout/garbage_sorting.xml new file mode 100644 index 0000000..acba582 --- /dev/null +++ b/app/src/main/res/layout/main/layout/garbage/layout/garbage_sorting.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/main/layout/garbage/layout/garbage_titlebar.xml b/app/src/main/res/layout/main/layout/garbage/layout/garbage_titlebar.xml new file mode 100644 index 0000000..151fb90 --- /dev/null +++ b/app/src/main/res/layout/main/layout/garbage/layout/garbage_titlebar.xml @@ -0,0 +1,31 @@ + + + +