增加垃圾上门回收基本布局

增加垃圾分类查询UI,以及数据管理
更新动态数据搜索框
0515
panqihua 5 years ago
parent 8ce7134a04
commit 4b0b118828
  1. 7
      app/build.gradle
  2. 48
      app/src/main/java/com/community/pocket/data/adapter/GarbageAdpter.java
  3. 42
      app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java
  4. 35
      app/src/main/java/com/community/pocket/data/model/GarbageSorting.java
  5. 4
      app/src/main/java/com/community/pocket/ui/main/MainFragment.java
  6. 62
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java
  7. 10
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java
  8. 36
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageFragment.java
  9. 116
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingFragment.java
  10. 9
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingResponse.java
  11. 31
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageSortingViewModel.java
  12. 10
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/GarbageWasteFragment.java
  13. 79
      app/src/main/java/com/community/pocket/ui/main/ui/share/MyAutoCompleteTextView.java
  14. 9
      app/src/main/java/com/community/pocket/ui/main/ui/share/SearchViewModel.java
  15. 63
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java
  16. 10
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java
  17. 24
      app/src/main/res/layout/main/layout/garbage/layout/garbage.xml
  18. 45
      app/src/main/res/layout/main/layout/garbage/layout/garbage_sorting.xml
  19. 31
      app/src/main/res/layout/main/layout/garbage/layout/garbage_titlebar.xml
  20. 4
      app/src/main/res/layout/main/layout/garbage/layout/garbage_waste.xml
  21. 15
      app/src/main/res/navigation/garbage_navigation.xml
  22. 2
      app/src/main/res/navigation/nav_mainmenu.xml
  23. 5
      app/src/main/res/values-en-rUS/strings.xml
  24. 5
      app/src/main/res/values-zh-rCN/strings.xml
  25. 5
      app/src/main/res/values/strings.xml

@ -23,7 +23,12 @@ android {
sourceSets { sourceSets {
main { main {
res { 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'
} }
} }
} }

@ -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<String> {
private List<GarbageSorting> dataList;
private List<String> list = new ArrayList<>();
private Map<String, GarbageSorting> map = new HashMap<>();
public GarbageAdpter(List<GarbageSorting> dataList, Context context) {
super(context, android.R.layout.simple_list_item_1);
addAll(dataList);
}
public void addAll(List<GarbageSorting> 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<String> getList() {
return list;
}
}

@ -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<GarbageSorting> 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;
}
}

@ -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;
}
}

@ -30,8 +30,8 @@ public abstract class MainFragment extends BaseFragment {
*/ */
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @RequiresApi(api = Build.VERSION_CODES.KITKAT)
protected NavController ctrl() { protected NavController ctrl() {
if (navController == null) { if (navController == null && getView() != null) {
navController = Navigation.findNavController(Objects.requireNonNull(getView()).findViewById(fragmentId())); navController = Navigation.findNavController(getView().findViewById(fragmentId()));
} }
return navController; return navController;
} }

@ -1,14 +1,9 @@
package com.community.pocket.ui.main.ui.forum.post; package com.community.pocket.ui.main.ui.forum.post;
import android.annotation.SuppressLint;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.Toast; import android.widget.Toast;
@ -22,13 +17,12 @@ import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.adapter.ForumSearchAdapter; import com.community.pocket.data.adapter.ForumSearchAdapter;
import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView;
import com.community.pocket.util.PropertiesUtil; import com.community.pocket.util.PropertiesUtil;
import org.jetbrains.annotations.NotNull;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject; import org.xutils.view.annotation.ViewInject;
import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -50,10 +44,7 @@ public class ForumPostComplainFragment extends ForumPostContent {
private ForumPostComplainViewModel viewModel; private ForumPostComplainViewModel viewModel;
//AutoCompleteTextView的doBeforeTextChanged方法 private MyAutoCompleteTextView myAutoCompleteTextView;
private Method doBeforeTextChanged;
//AutoCompleteTextView的doAfterTextChanged方法
private Method doAfterTextChanged;
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override @Override
@ -62,6 +53,8 @@ public class ForumPostComplainFragment extends ForumPostContent {
viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostComplainViewModel.class); viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostComplainViewModel.class);
myAutoCompleteTextView = new MyAutoCompleteTextView(searchName, viewModel);
//监听表单校验状态 //监听表单校验状态
viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState.Complain>() { viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState.Complain>() {
@Override @Override
@ -110,13 +103,7 @@ public class ForumPostComplainFragment extends ForumPostContent {
viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString()); viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString());
} }
long time = System.currentTimeMillis(); myAutoCompleteTextView.searchHandler();
//输入的字符间隔时间 小于700毫秒 移除以前的handler 延时600毫秒执行
if (searchName.getTag() != null && time - (Long) searchName.getTag() < 700) {
searchHandler.removeMessages(1);
}
searchHandler.sendEmptyMessageDelayed(1, 600);
searchName.setTag(time);
} }
}); });
@ -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 = 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 { } else {
nameList.addAll(stringList); nameList.addAll(stringList);
} }
refreshDropList(); myAutoCompleteTextView.refreshDropList();
} }
} }

@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumPostRequest; 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.Valid;
import com.community.pocket.util.ValidUtil; 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.Complain> forumPostFormState = new MutableLiveData<>(); private MutableLiveData<ForumPostFormState.Complain> forumPostFormState = new MutableLiveData<>();
@ -68,8 +69,13 @@ class ForumPostComplainViewModel extends ForumPostViewModel {
* *
* @param name 投诉人 * @param name 投诉人
*/ */
void searchPeople(String name) { private void searchPeople(String name) {
ForumPostResponse<List<String>> forumPostResponse = forumPostRequest.searchPeople(name); ForumPostResponse<List<String>> forumPostResponse = forumPostRequest.searchPeople(name);
searchPeople.setValue(forumPostResponse); searchPeople.setValue(forumPostResponse);
} }
@Override
public void search(String content) {
searchPeople(content);
}
} }

@ -1,16 +1,42 @@
package com.community.pocket.ui.main.ui.garbage; 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.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.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 @Override
protected int viewId() { protected int[] button_ids() {
return R.id.text_notifications; return new int[]{R.id.garbage_sorting, R.id.garbage_waste};
} }
} }

@ -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<GarbageSortingResponse>() {
@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<GarbageSorting> garbageSortings) {
// 设置适配器
if (garbageAdpter == null) {
garbageAdpter = new GarbageAdpter(garbageSortings, getContext());
// 将适配器与当前AutoCompleteTextView控件绑定
searchSorting.setAdapter(garbageAdpter);
} else {
garbageAdpter.addAll(garbageSortings);
}
myAutoCompleteTextView.refreshDropList();
}
}

@ -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<List<GarbageSorting>> {
}

@ -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<GarbageSortingRequest> implements SearchViewModel {
//垃圾分类信息请求状态
private MutableLiveData<GarbageSortingResponse> garbageSortingResponse = new MutableLiveData<>();
MutableLiveData<GarbageSortingResponse> getGarbageSortingResponse() {
return garbageSortingResponse;
}
@Override
protected GarbageSortingRequest getRequest() {
return GarbageSortingRequest.getInstance();
}
@Override
public void search(String content) {
GarbageSortingResponse response = getRequest().search();
garbageSortingResponse.setValue(response);
}
}

@ -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 {
}

@ -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 = 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);
}
}

@ -0,0 +1,9 @@
package com.community.pocket.ui.main.ui.share;
/**
* 搜索框请求数据源
*/
public interface SearchViewModel {
//请求方法
void search(String content);
}

@ -1,13 +1,8 @@
package com.community.pocket.ui.main.ui.visitor.appointment; package com.community.pocket.ui.main.ui.visitor.appointment;
import android.annotation.SuppressLint;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable; import android.text.Editable;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
@ -26,11 +21,11 @@ import com.community.pocket.R;
import com.community.pocket.data.adapter.VisitorAdpter; import com.community.pocket.data.adapter.VisitorAdpter;
import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.BaseFragment;
import com.community.pocket.ui.listener.MyTextChange; 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.share.ShowWordCount;
import com.community.pocket.ui.main.ui.visitor.VisitorFragment; import com.community.pocket.ui.main.ui.visitor.VisitorFragment;
import com.community.pocket.util.PropertiesUtil; import com.community.pocket.util.PropertiesUtil;
import org.jetbrains.annotations.NotNull;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event; import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject; import org.xutils.view.annotation.ViewInject;
@ -65,10 +60,8 @@ public class VisitorAppointmentFragment extends BaseFragment {
private VisitorViewModel appointmentViewModel; private VisitorViewModel appointmentViewModel;
//AutoCompleteTextView的doBeforeTextChanged方法 //动态更新搜索框数据源
private Method doBeforeTextChanged; private MyAutoCompleteTextView myAutoCompleteTextView;
//AutoCompleteTextView的doAfterTextChanged方法
private Method doAfterTextChanged;
//预约人数据呀 //预约人数据呀
private VisitorAdpter visitorAdpter; private VisitorAdpter visitorAdpter;
@ -84,6 +77,8 @@ public class VisitorAppointmentFragment extends BaseFragment {
appointmentViewModel = new ViewModelProvider(this, new VisitorAppointmentFactory()).get(VisitorViewModel.class); appointmentViewModel = new ViewModelProvider(this, new VisitorAppointmentFactory()).get(VisitorViewModel.class);
myAutoCompleteTextView = new MyAutoCompleteTextView(appointment, appointmentViewModel);
//监听访客预约表单校验状态 //监听访客预约表单校验状态
appointmentViewModel.getVisitorFormState().observe(getViewLifecycleOwner(), new Observer<VisitorFormState>() { appointmentViewModel.getVisitorFormState().observe(getViewLifecycleOwner(), new Observer<VisitorFormState>() {
@Override @Override
@ -168,13 +163,7 @@ public class VisitorAppointmentFragment extends BaseFragment {
} }
appointment.setTag(R.string.visitor_tag, s.toString()); appointment.setTag(R.string.visitor_tag, s.toString());
long time = System.currentTimeMillis(); myAutoCompleteTextView.searchHandler();
//输入的字符间隔时间 小于700毫秒 移除以前的handler 延时600毫秒执行
if (appointment.getTag() != null && time - (Long) appointment.getTag() < 700) {
searchHandler.removeMessages(1);
}
searchHandler.sendEmptyMessageDelayed(1, 600);
appointment.setTag(time);
} }
}); });
@ -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 = 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 { } else {
visitorAdpter.addAll(arrays); visitorAdpter.addAll(arrays);
} }
refreshDropList(); myAutoCompleteTextView.refreshDropList();
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @RequiresApi(api = Build.VERSION_CODES.KITKAT)

@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.visitor.VisitorRequest; 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.Valid;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
@ -16,7 +17,7 @@ import java.util.Map;
/** /**
* 管理访客UI数据 * 管理访客UI数据
*/ */
class VisitorViewModel extends ViewModel { class VisitorViewModel extends ViewModel implements SearchViewModel {
//访客预约表单校验信息 //访客预约表单校验信息
private MutableLiveData<VisitorFormState> visitorFormState = new MutableLiveData<>(); private MutableLiveData<VisitorFormState> visitorFormState = new MutableLiveData<>();
@ -57,7 +58,7 @@ class VisitorViewModel extends ViewModel {
} }
//检索预约人 //检索预约人
void searchPeople(String name) { private void searchPeople(String name) {
VisitorResponse<Map<String, List<String>>> result = visitorRequest.searchPeople(name); VisitorResponse<Map<String, List<String>>> result = visitorRequest.searchPeople(name);
searchPeopleReq.setValue(result); searchPeopleReq.setValue(result);
} }
@ -72,4 +73,9 @@ class VisitorViewModel extends ViewModel {
visitorFormState.setValue(new VisitorFormState(true)); visitorFormState.setValue(new VisitorFormState(true));
} }
} }
@Override
public void search(String content) {
searchPeople(content);
}
} }

@ -6,17 +6,19 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.main.ui.garbage.GarbageFragment"> tools:context=".ui.main.ui.garbage.GarbageFragment">
<TextView <include
android:id="@+id/text_notifications" android:id="@+id/titlebar"
android:layout_width="match_parent" layout="@layout/garbage_titlebar"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:navGraph="@navigation/garbage_navigation"
tools:layout_constraintTop_toBottomOf="@id/titlebar"
android:id="@+id/grabage_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,45 @@
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<AutoCompleteTextView
android:id="@+id/search_sorting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="@string/prompt_garbage_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/search_sorting" />
<TextView
android:id="@+id/sorting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/name" />
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/size_200"
android:layout_height="@dimen/size_200"
android:contentDescription="@string/garbage_img"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/sorting" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/garbage_sorting"
style="?android:attr/buttonBarStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorAccent"
android:text="@string/garbage_sorting"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/garbage_waste"
style="?android:attr/buttonBarStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/button_unchecked"
android:text="@string/garbage_waste"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/forum_hot"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/garbage_navigation"
app:startDestination="@id/garbageSortingFragment">
<fragment
android:id="@+id/garbageSortingFragment"
android:name="com.community.pocket.ui.main.ui.garbage.GarbageSortingFragment"
android:label="GarbageSortingFragment" />
<fragment
android:id="@+id/garbageWasteFragment"
android:name="com.community.pocket.ui.main.ui.garbage.GarbageWasteFragment"
android:label="GarbageWasteFragment" />
</navigation>

@ -21,7 +21,7 @@
android:id="@+id/navigation_notifications" android:id="@+id/navigation_notifications"
android:name="com.community.pocket.ui.main.ui.garbage.GarbageFragment" android:name="com.community.pocket.ui.main.ui.garbage.GarbageFragment"
android:label="@string/title_notifications" android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" /> tools:layout="@layout/garbage" />
<fragment <fragment
android:id="@+id/navigation_info" android:id="@+id/navigation_info"
android:name="com.community.pocket.ui.main.ui.info.InfoFragment" android:name="com.community.pocket.ui.main.ui.info.InfoFragment"

@ -152,4 +152,9 @@
<string name="load_visitor_reservation_ok">load reservation success</string> <string name="load_visitor_reservation_ok">load reservation success</string>
<string name="load_visitor_reservation_fail">load reservation fail</string> <string name="load_visitor_reservation_fail">load reservation fail</string>
<string name="login_ok">Welcome the %1s to log in this system</string> <string name="login_ok">Welcome the %1s to log in this system</string>
<string name="garbage_sorting">garbage sorting</string>
<string name="garbage_waste">waste</string>
<string name="prompt_garbage_name">please input name</string>
<string name="garbage_img">grabage image</string>
<string name="garbage_search_ok">search success</string>
</resources> </resources>

@ -152,4 +152,9 @@
<string name="load_visitor_reservation_ok">加载我的预约成功</string> <string name="load_visitor_reservation_ok">加载我的预约成功</string>
<string name="load_visitor_reservation_fail">加载我的预约失败</string> <string name="load_visitor_reservation_fail">加载我的预约失败</string>
<string name="login_ok">欢迎用户%1s登陆口袋社区</string> <string name="login_ok">欢迎用户%1s登陆口袋社区</string>
<string name="garbage_sorting">垃圾分类</string>
<string name="garbage_waste">" 废品回收"</string>
<string name="prompt_garbage_name">请输入垃圾名</string>
<string name="garbage_img">垃圾图片</string>
<string name="garbage_search_ok">检索成功</string>
</resources> </resources>

@ -153,6 +153,11 @@
<string name="load_visitor_reservation_ok">load reservation success</string> <string name="load_visitor_reservation_ok">load reservation success</string>
<string name="load_visitor_reservation_fail">load reservation fail</string> <string name="load_visitor_reservation_fail">load reservation fail</string>
<string name="login_ok">Welcome the %1s to log in this system</string> <string name="login_ok">Welcome the %1s to log in this system</string>
<string name="garbage_sorting">garbage sorting</string>
<string name="garbage_waste">waste</string>
<string name="prompt_garbage_name">please input name</string>
<string name="garbage_img">grabage image</string>
<string name="garbage_search_ok">search success</string>
<!-- Strings used for fragments for navigation --> <!-- Strings used for fragments for navigation -->
<!-- Strings used for fragments for navigation --> <!-- Strings used for fragments for navigation -->

Loading…
Cancel
Save