在我们的这个位置的话就是编写我们的xml文本布局的代码:
然后的话就是我们的这个布局界面的详细的代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.notifications.NotificationsFragment">
<!--在我们的这个位置的话就是设置我们的相关的方法然后的话编写我们的布局文件的代码-->
<AutoCompleteTextView
android:background="@drawable/border_tv"
android:hint="搜索服务"
android:layout_margin="8dp"
android:padding="8dp"
android:id="@+id/my_search_view"
android:completionThreshold="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownVerticalOffset="8dp"
android:imeOptions="actionSearch"
android:singleLine="true"
android:drawableLeft="@drawable/ic_baseline_search_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ry_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="8dp"
android:layout_weight="2"
android:background="#CBC7C7"
app:layout_constraintTop_toBottomOf="@+id/my_search_view" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/btn_selectir"
android:text="全部服务"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ry_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toBottomOf="@+id/my_search_view" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
然后的话我们的java相对应的代码的编写:
接下来的话就是我们的界面布局的详细的代码进行编写:
package com.example.model_okhttp_and_yingdao.ui.notifications;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.model_okhttp_and_yingdao.R;
import com.example.model_okhttp_and_yingdao.ui.Bean.ServiceListBean;
import com.example.model_okhttp_and_yingdao.ui.https.HttpUtil;
import com.example.model_okhttp_and_yingdao.ui.https.MyApplication;
import com.example.model_okhttp_and_yingdao.ui.https.OkhttpCallBack;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;
import kotlin.contracts.ReturnsNotNull;
public class NotificationsFragment extends Fragment {
private AutoCompleteTextView mySearchView;
private RecyclerView ryLeft;
private TextView tvTitleContent;
private RecyclerView ryRight;
// 在我们的这个位置的话就是创建我们的hashmap的相关的方法
private HashMap<String, List<SecondBean>> map;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
map = new HashMap<>();
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// 在我们的这个位置的话就是设置我们的相关的方法:
initView(view);
// 然后的话在我们的这个位置的话就是设置我们的相关的数据
initData();
}
private void initData() {
HttpUtil.getInstance().doGet("/system/dict/data/type/sys_service", new OkhttpCallBack() {
@Override
public void successful(String successString) throws IOException {
// 在我们的这个位置的话就是编写我们的相关的方法
ArrayList<FirstBean> firstBeans = new ArrayList<>();
FirstServiceBean firstServiceBean = MyApplication.getGson().fromJson(successString, FirstServiceBean.class);
for (FirstServiceBean.DataBean datum : firstServiceBean.getData()) {
FirstBean firstRecyclerBean = new FirstBean(datum.getDictValue(), datum.getDictLabel());
firstBeans.add(firstRecyclerBean);
}
ryLeft.post(new Runnable() {
@Override
public void run() {
// 在我们的这个位置的话设置我们的相对应的方法
((MyLeftRecyclerAdapter) ryLeft.getAdapter()).setFirstData(firstBeans);
ryLeft.getAdapter().notifyDataSetChanged();
}
});
// 在我们的这个位置的话还缺少一个东西
getServiceDetail(firstBeans);
}
@Override
public void failuer(String errorString) {
}
});
}
// 在我们的这个位置的话就是创建我们的getServiceDetail方法
private void getServiceDetail(ArrayList<FirstBean> firstBeans) {
HttpUtil.getInstance().doGet("/service/service/list", new OkhttpCallBack() {
@Override
public void successful(String successString) throws IOException {
// 在我们的这个位置的话就是编写我们的相关的方法
ServiceListBean serviceListBean = MyApplication.getGson().fromJson(successString, ServiceListBean.class);
List<ServiceListBean.RowsBean> rows = serviceListBean.getRows();
ArrayList<String> textViewBeans = new ArrayList<>();
//
rows.forEach((e) -> {
firstBeans.forEach((t) -> {
if (e.getServiceType().equals(t.getDictType())) {
if (map.containsKey(t.getServiceName())) {
List<SecondBean> secondBeans = map.get(t.getServiceName());
secondBeans.add(new SecondBean(e.getServiceName(), e.getImgUrl()));
} else {
ArrayList<SecondBean> data = new ArrayList<>();
data.add(new SecondBean(e.getServiceName(), e.getImgUrl()));
map.put(t.getServiceName(), data);
}
}
});
textViewBeans.add(e.getServiceName());
});
String serviceName = ((MyLeftRecyclerAdapter) ryLeft.getAdapter()).getFirstData().get(0).getServiceName();
List<SecondBean> secondBeans = map.get(serviceName);
ryRight.post(new Runnable() {
@Override
public void run() {
tvTitleContent.setText(serviceName);
MyRightRecyclerAdapter adapter = (MyRightRecyclerAdapter) ryRight.getAdapter();
adapter.setList(secondBeans);
adapter.notifyDataSetChanged();
}
});
// 在我们的这个位置的话设置我们的相对应的方法
mySearchView.post(new Runnable() {
@Override
public void run() {
ArrayAdapter<String> adapter =
new ArrayAdapter<>(getActivity(), android.R.layout.simple_dropdown_item_1line, textViewBeans);
mySearchView.setAdapter(adapter);
mySearchView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
// 在我们的这个位置的话就是设置我们的相对应的方法
if (i == EditorInfo.IME_FLAG_FORCE_ASCII) {
hideSoft(textView);
String inputString = textView.getText().toString().trim() ;
boolean hasString = false;
if(!TextUtils.isEmpty(inputString)){
for(String textViewBean : textViewBeans){
if(inputString.equals(textViewBean)){
hasString = true;
}
}
if(hasString){
ArrayList<SecondBean> secondBeans1 = new ArrayList<>();
rows.forEach(new Consumer<ServiceListBean.RowsBean>() {
@Override
public void accept(ServiceListBean.RowsBean rowsBean) {
if(rowsBean.getServiceName().equals(inputString)){
secondBeans1.add(new SecondBean(rowsBean.getServiceName(),rowsBean.getImgUrl()));
}
}
});
showdialog(secondBeans1);
}else {
Toast.makeText(textView.getContext(), "无搜索服务", Toast.LENGTH_SHORT).show();
}
}else {
ArrayList<SecondBean> data = new ArrayList<>();
rows.forEach(new Consumer<ServiceListBean.RowsBean>() {
@Override
public void accept(ServiceListBean.RowsBean rowsBean) {
data.add(new SecondBean(rowsBean.getServiceName(),rowsBean.getImgUrl()));
}
});
showdialog(data);
}
return true;
}
return false;
}
});
}
});
}
@Override
public void failuer(String errorString) {
}
});
}
// 这个的话就是隐藏我们的输入法
private void hideSoft(TextView textView) {
InputMethodManager systemService = (InputMethodManager)
textView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
systemService.hideSoftInputFromWindow(textView.getWindowToken(), 0);
}
// 这个的话就是显示我们的输入法
private void showdialog(ArrayList<SecondBean> data) {
// 在我们的这个位置的话就是展示我们的相关的输入法
View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.popwindow, null, false);
RecyclerView viewById = (RecyclerView) inflate.findViewById(R.id.my_recycler_bottom);
TextView mTvCncel = (TextView) inflate.findViewById(R.id.tv_cancel);
Dialog dialog = new Dialog(getActivity(), R.style.mydialog);
dialog.setContentView(inflate);
dialog.setCanceledOnTouchOutside(true);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mTvCncel.setOnClickListener(v -> {
if (dialog.isShowing()) {
dialog.dismiss();
}
});
viewById.setLayoutManager(new GridLayoutManager(getActivity(), 3));
viewById.setAdapter(new MyRightRecyclerAdapter(getActivity(), data));
dialog.show();
}
private void initView(View view) {
// 在我们的这个位置的话找到我们的id相关的方法
mySearchView = (AutoCompleteTextView) view.findViewById(R.id.my_search_view);
ryLeft = (RecyclerView) view.findViewById(R.id.ry_left);
tvTitleContent = (TextView) view.findViewById(R.id.tv_title_content);
ryRight = (RecyclerView) view.findViewById(R.id.ry_right);
initRecyclerView();
}
private void initRecyclerView() {
// todo 第十五步的话就是编写我们的initRecyclerView() 方法中的代码
ryLeft.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
ArrayList<FirstBean> firstBeans = new ArrayList<>();
ryLeft.setAdapter(new MyLeftRecyclerAdapter(firstBeans, getActivity()));
ryRight.setLayoutManager(new GridLayoutManager(getActivity(), 3));
ryRight.setAdapter(new MyRightRecyclerAdapter(getActivity(), new ArrayList<SecondBean>()));
((MyLeftRecyclerAdapter) ryLeft.getAdapter()).setLeftClick(new MyLeftRecyclerAdapter.LeftClick() {
@Override
public void onLeftClick(int pos, FirstBean bean) {
// 然后的话就是编写我们的方法
for (int i = 0; i < ryLeft.getChildCount(); i++) {
MyLeftRecyclerAdapter.MyLeftViewHolder holder = (MyLeftRecyclerAdapter.MyLeftViewHolder)
ryLeft.findViewHolderForAdapterPosition(i);
TextView textView = holder.getTextName();
if (pos == i) {
textView.setBackgroundColor(Color.WHITE);
} else {
textView.setBackgroundColor(Color.parseColor("#CBC7C7"));
}
}
// 数据刷新完成
if (ryLeft.getChildCount() != 0 && ryRight.getChildCount() != 0) {
tvTitleContent.setText(bean.getServiceName());
List<SecondBean> secondBeans = map.get(bean.getServiceName());
((MyRightRecyclerAdapter) ryRight.getAdapter()).setList(secondBeans);
ryRight.getAdapter().notifyDataSetChanged();
}
}
});
}
}
在我们的这个位置的话就是我们的相对应的适配器进行我们的相关的适配器的设置MyRightRecyclerAdapter:
然后的话就是我们的详细的代码:
package com.example.model_okhttp_and_yingdao.ui.notifications;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.model_okhttp_and_yingdao.R;
import com.example.model_okhttp_and_yingdao.ui.https.HttpUtil;
import java.util.List;
public class MyRightRecyclerAdapter extends RecyclerView.Adapter<MyRightRecyclerAdapter.MyViewHolder> {
private Context context;
private List<SecondBean> list;
public MyRightRecyclerAdapter(Context context,List<SecondBean> list){
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyRightRecyclerAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.recylerhot_service_model,parent,false);
// todo 在我们的这个位置的话就是检查出我们的相关的错误然后的话在我们的这个位置的话对应的索引的话是不正确的
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull MyRightRecyclerAdapter.MyViewHolder holder, int position) {
// 在我们的这个位置的话就是找到我们的相关的id这里的话就是我们的id的话就是设置相关的id
Glide.with(context).load(HttpUtil.BASE_URL + list.get(position).getImgUrl()).into(holder.requrireimages);
holder.textHot.setText(list.get(position).getTypeName());
}
@Override
public int getItemCount() {
return list.size();
}
public void setList(List<SecondBean> list) {
this.list = list;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView requrireimages;
private TextView textHot;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
requrireimages = (ImageView) itemView.findViewById(R.id.requrireimages);
textHot = (TextView) itemView.findViewById(R.id.text_hot);
}
}
}
然后的话就是我们的左边的适配器的设置,MyLeftViewHolder:
package com.example.model_okhttp_and_yingdao.ui.notifications;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.model_okhttp_and_yingdao.R;
import java.util.List;
// todo 在我们的这个位置的话可能存在一个bug
public class MyLeftRecyclerAdapter extends RecyclerView.Adapter<MyLeftRecyclerAdapter.MyLeftViewHolder> {
private List<FirstBean> firstData;
private Context context;
private LeftClick leftClick;
public List<FirstBean> getFirstData() {
return firstData;
}
public void setFirstData(List<FirstBean> firstData) {
this.firstData = firstData;
}
// 创建我们的构造方法
public MyLeftRecyclerAdapter(List<FirstBean> firstData,Context context){
this.firstData = firstData;
this.context = context;
}
@NonNull
@Override
public MyLeftRecyclerAdapter.MyLeftViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.servicename_item_model,parent,false);
return new MyLeftRecyclerAdapter.MyLeftViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull MyLeftRecyclerAdapter.MyLeftViewHolder holder, int position) {
if(position == 0){
holder.textName.setBackgroundColor(Color.WHITE);
}
holder.textName.setText(firstData.get(position).getServiceName());
holder.textName.setOnClickListener(view -> {
leftClick.onLeftClick(position,firstData.get(position));
});
}
@Override
public int getItemCount() {
// 这个位置的话就是还有我们的相关的返回的item然后的恶化就是设置相关的方法
return firstData.size();
}
public class MyLeftViewHolder extends RecyclerView.ViewHolder {
private TextView textName;
public TextView getTextName() {
return textName;
}
public void setTextName(TextView textName) {
this.textName = textName;
}
public MyLeftViewHolder(@NonNull View itemView) {
super(itemView);
// 在我们的这个位置的话就是设置我们的相对应的方法
textName = (TextView) itemView.findViewById(R.id.textName);
}
}
// 在我们的这个位置的话就是设置和创建我们的相对应的回调的接口
public interface LeftClick{
void onLeftClick(int pos,FirstBean bean);
}
public void setLeftClick(LeftClick leftClick){
this.leftClick = leftClick;
}
}
然后的话就是我们相对应的bean文件第一个bean文件名称:firstBean:
第二个bean文件SecondBean:
package com.example.model_okhttp_and_yingdao.ui.notifications;
public class SecondBean {
// 在我们的这个位置的话就是创建我们的相关的secondbean
private String typeName;
private String imgUrl;
// 然后的话就是创建我们的构造方法
public SecondBean(String typeName,String imgUrl){
this.typeName = typeName;
this.imgUrl = imgUrl;
}
@Override
public String toString() {
return "SecondBean{" +
"typeName='" + typeName + '\'' +
", imgUrl='" + imgUrl + '\'' +
'}';
}
// 然后的话就是创建我们的相关的方法
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
}
然后的话就是我们的对应服务名称的bean文件:
这个的话是我们的对应名称的bean文件的主要的代码:
package com.example.model_okhttp_and_yingdao.ui.notifications;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class FirstServiceBean {
/**
* msg : 操作成功
* code : 200
* data : [{"searchValue":null,"createBy":"admin","createTime":"2020-10-12 13:09:01","updateBy":null,"updateTime":null,"remark":null,"params":{},"dictCode":29,"dictSort":0,"dictLabel":"生活服务","dictValue":"1","dictType":"sys_service","cssClass":null,"listClass":null,"isDefault":"N","status":"0","default":false},{"searchValue":null,"createBy":"admin","createTime":"2020-10-12 13:09:08","updateBy":null,"updateTime":null,"remark":null,"params":{},"dictCode":30,"dictSort":0,"dictLabel":"便民服务","dictValue":"2","dictType":"sys_service","cssClass":null,"listClass":null,"isDefault":"N","status":"0","default":false},{"searchValue":null,"createBy":"admin","createTime":"2020-10-12 18:16:47","updateBy":null,"updateTime":null,"remark":null,"params":{},"dictCode":44,"dictSort":3,"dictLabel":"车主服务","dictValue":"3","dictType":"sys_service","cssClass":null,"listClass":null,"isDefault":"N","status":"0","default":false}]
*/
private String msg;
private int code;
private List<DataBean> data;
public static FirstServiceBean objectFromData(String str) {
return new Gson().fromJson(str, FirstServiceBean.class);
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* searchValue : null
* createBy : admin
* createTime : 2020-10-12 13:09:01
* updateBy : null
* updateTime : null
* remark : null
* params : {}
* dictCode : 29
* dictSort : 0
* dictLabel : 生活服务
* dictValue : 1
* dictType : sys_service
* cssClass : null
* listClass : null
* isDefault : N
* status : 0
* default : false
*/
private Object searchValue;
private String createBy;
private String createTime;
private Object updateBy;
private Object updateTime;
private Object remark;
private ParamsBean params;
private int dictCode;
private int dictSort;
private String dictLabel;
private String dictValue;
private String dictType;
private Object cssClass;
private Object listClass;
private String isDefault;
private String status;
@SerializedName("default")
private boolean defaultX;
public static DataBean objectFromData(String str) {
return new Gson().fromJson(str, DataBean.class);
}
public Object getSearchValue() {
return searchValue;
}
public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public Object getUpdateBy() {
return updateBy;
}
public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}
public Object getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Object updateTime) {
this.updateTime = updateTime;
}
public Object getRemark() {
return remark;
}
public void setRemark(Object remark) {
this.remark = remark;
}
public ParamsBean getParams() {
return params;
}
public void setParams(ParamsBean params) {
this.params = params;
}
public int getDictCode() {
return dictCode;
}
public void setDictCode(int dictCode) {
this.dictCode = dictCode;
}
public int getDictSort() {
return dictSort;
}
public void setDictSort(int dictSort) {
this.dictSort = dictSort;
}
public String getDictLabel() {
return dictLabel;
}
public void setDictLabel(String dictLabel) {
this.dictLabel = dictLabel;
}
public String getDictValue() {
return dictValue;
}
public void setDictValue(String dictValue) {
this.dictValue = dictValue;
}
public String getDictType() {
return dictType;
}
public void setDictType(String dictType) {
this.dictType = dictType;
}
public Object getCssClass() {
return cssClass;
}
public void setCssClass(Object cssClass) {
this.cssClass = cssClass;
}
public Object getListClass() {
return listClass;
}
public void setListClass(Object listClass) {
this.listClass = listClass;
}
public String getIsDefault() {
return isDefault;
}
public void setIsDefault(String isDefault) {
this.isDefault = isDefault;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public boolean isDefaultX() {
return defaultX;
}
public void setDefaultX(boolean defaultX) {
this.defaultX = defaultX;
}
public static class ParamsBean {
public static ParamsBean objectFromData(String str) {
return new Gson().fromJson(str, ParamsBean.class);
}
}
}
}
然后的恶化就是我们的相关的服务的代码和我们的相关服务的图片: