You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.8 KiB
116 lines
3.8 KiB
package com.community.pocket.ui.main.ui.garbage.sorting;
|
|
|
|
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.GarbageSortingAdpter;
|
|
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 GarbageSortingAdpter garbageSortingAdpter;
|
|
|
|
@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 (garbageSortingAdpter != null && garbageSortingAdpter.containsKey(s.toString())) {
|
|
initView(garbageSortingAdpter.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 (garbageSortingAdpter == null) {
|
|
garbageSortingAdpter = new GarbageSortingAdpter(garbageSortings, getContext());
|
|
// 将适配器与当前AutoCompleteTextView控件绑定
|
|
searchSorting.setAdapter(garbageSortingAdpter);
|
|
} else {
|
|
garbageSortingAdpter.addAll(garbageSortings);
|
|
}
|
|
myAutoCompleteTextView.refreshDropList();
|
|
}
|
|
}
|
|
|