第一步的话就是我们的android相关的布局文件的制作:
我们的详细的代码:
<?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"
android:layout_marginTop="?attr/actionBarSize"
tools:context=".ui.dashboard.DashboardFragment">
<!--在我们1的这个位置的话就是设置我们的更多服务的界面设置-->
<AutoCompleteTextView
android:id="@+id/my_search_view"
android:layout_margin="8dp"
android:padding="8dp"
android:drawableLeft="@drawable/ic_baseline_search_24"
android:completionThreshold="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</AutoCompleteTextView>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ry_left"
android:layout_marginTop="8dp"
android:layout_weight="2"
android:layout_marginEnd="8dp"
android:layout_width="0dp"
android:background="#CBC7C7"
android:layout_height="match_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_title"
android:layout_marginTop="8dp"
android:textStyle="bold"
android:text="全部服务"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--在我们的这个位置的话设置我们的recylerview-->
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_marginEnd="8dp"
android:id="@+id/ry_right"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
然后的话就是我们的java的代码部分的编写:
package com.example.tetst2.ui.dashboard;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
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.tetst2.R;
import com.example.tetst2.ui.Bean.FirstRecylerBean;
import com.example.tetst2.ui.Bean.ServiceBean;
import com.example.tetst2.ui.Bean.ServiceListBean;
import com.example.tetst2.ui.Bean.firstBean;
import com.example.tetst2.ui.Util.HttpUtil;
import com.example.tetst2.ui.Util.MyApplication;
import com.example.tetst2.ui.Util.OkhttpCallBack;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DashboardFragment extends Fragment {
private AutoCompleteTextView mySearchView;
private RecyclerView ryLeft;
private TextView tvTitle;
private RecyclerView ryRight;
private HashMap<String,List<ServiceListBean>> 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_dashboard, container, false);
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
}
private void initView(View view) {
mySearchView = (AutoCompleteTextView) view.findViewById(R.id.my_search_view);
ryLeft = (RecyclerView) view.findViewById(R.id.ry_left);
tvTitle = (TextView) view.findViewById(R.id.tv_title);
ryRight = (RecyclerView) view.findViewById(R.id.ry_right);
initData();
initRecylerview();
}
private void initRecylerview() {
ryLeft.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
ArrayList<firstBean> firstBeans = new ArrayList<>();
ryLeft.setAdapter(new RyLeftAdapter(firstBeans,getContext()));
ryRight.setLayoutManager(new GridLayoutManager(getContext(),3));
ryRight.setAdapter(new MyRightRecyclerAdapter(getContext(),new ArrayList<ServiceListBean>()));
((RyLeftAdapter) ryLeft.getAdapter()).setLeftClick(new RyLeftAdapter.LeftClick() {
@Override
public void onLeftClick(int pos, firstBean bean) {
for(int i =0; i< ryLeft.getChildCount();i++){
RyLeftAdapter.MyLeftViewHolder holder
= (RyLeftAdapter.MyLeftViewHolder) ryLeft.findViewHolderForAdapterPosition(i);
TextView textView = holder.getOtherService();
if(pos == i){
textView.setBackgroundColor(Color.WHITE);
}else {
textView.setBackgroundColor(Color.parseColor("#CBC7C7"));
}
}if(ryLeft.getChildCount() != 0 && ryRight.getChildCount() != 0){
tvTitle.setText(bean.getDictLabel());
List<ServiceListBean> serviceListBeans = map.get(bean.getDictLabel());
((MyRightRecyclerAdapter) ryRight.getAdapter()).setMlist(serviceListBeans);
ryRight.getAdapter().notifyDataSetChanged();
}
}
});
}
private void initData() {
/*
* 在我们的这个 位置的话就是设置我们的相关的方法
* */
HttpUtil.getInstance().doGet("/system/dict/data/type/sys_service", new OkhttpCallBack() {
@Override
public void success(String successString) throws IOException {
ArrayList<firstBean> firstBeans = new ArrayList<>();
FirstRecylerBean firstRecylerBean = MyApplication.getGson().fromJson(successString,FirstRecylerBean.class);
for(FirstRecylerBean.DataBean data : firstRecylerBean.getData()){
firstBeans.add(new firstBean(data.getDictValue(),data.getDictLabel()));
}
/*
* 线程的切换更新我们的数据
* */
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
/*
* 更新我们的相关的数据
* */
((RyLeftAdapter) ryLeft.getAdapter()).setFirstData(firstBeans);
ryLeft.getAdapter().notifyDataSetChanged();
}
});
getServiceDetial(firstBeans);
}
private void getServiceDetial(ArrayList<firstBean> firstBeans) {
/*
* 在我们的这个位置的话就是创建我们的相关的firstDetial方法
* */
HttpUtil.getInstance().doGet("/service/service/list", new OkhttpCallBack() {
@Override
public void success(String successString) throws IOException {
ServiceBean serviceBean = MyApplication.getGson().fromJson(successString,ServiceBean.class);
List<ServiceBean.RowsBean> rows = serviceBean.getRows();
ArrayList<String> textViewBeans = new ArrayList<>();
rows.forEach((e)->{
firstBeans.forEach((t)->{
if(e.getServiceType().equals(t.getDictValue())){
if(map.containsKey(t.getDictLabel())){
List<ServiceListBean> serviceListBeans = map.get(t.getDictLabel());
serviceListBeans.add(new ServiceListBean(e.getServiceName(),e.getImgUrl()));
}else {
ArrayList<ServiceListBean> datas = new ArrayList<>();
datas.add(new ServiceListBean(e.getServiceName(),e.getImgUrl()));
map.put(t.getDictLabel(),datas);
}
}
});
textViewBeans.add(e.getServiceName());
});
/*
* 设置我们的相关的方法
* */
String serviceName = ((RyLeftAdapter) ryLeft.getAdapter()).getFirstData().get(0).getDictLabel();
List<ServiceListBean> serviceListBeans = map.get(serviceName);
ryRight.post(new Runnable() {
@Override
public void run() {
tvTitle.setText(serviceName);
MyRightRecyclerAdapter adapter = (MyRightRecyclerAdapter) ryRight.getAdapter();
adapter.setMlist(serviceListBeans);
adapter.notifyDataSetChanged();
}
});
}
@Override
public void failure(String errorString) {
}
});
}
@Override
public void failure(String errorString) {
}
});
}
}
然后的话就是我们的相关的两个适配器的编写:
代码部分:
package com.example.tetst2.ui.dashboard;
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.tetst2.R;
import com.example.tetst2.ui.Bean.FirstRecylerBean;
import com.example.tetst2.ui.Bean.firstBean;
import java.util.List;
public class RyLeftAdapter extends RecyclerView.Adapter<RyLeftAdapter.MyLeftViewHolder> {
private List<firstBean> firstData;
private Context context;
public List<firstBean> getFirstData() {
return firstData;
}
public void setFirstData(List<firstBean> firstData) {
this.firstData = firstData;
}
public RyLeftAdapter(List<firstBean> firstData, Context context) {
this.firstData = firstData;
this.context = context;
}
@NonNull
@Override
public RyLeftAdapter.MyLeftViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.model_left,parent,false);
return new MyLeftViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RyLeftAdapter.MyLeftViewHolder holder, int position) {
if(position == 0){
holder.otherService.setBackgroundColor(Color.WHITE);
}
// todo 在我们的这个位置的else 不能要不然的话回退不了
holder.otherService.setText(firstData.get(position).getDictLabel());
holder.otherService.setOnClickListener(view -> {
leftClick.onLeftClick(position,firstData.get(position));
});
}
@Override
public int getItemCount() {
return firstData.size();
}
public class MyLeftViewHolder extends RecyclerView.ViewHolder {
private TextView otherService;
public MyLeftViewHolder(@NonNull View itemView) {
super(itemView);
otherService = (TextView) itemView.findViewById(R.id.otherService);
}
public TextView getOtherService() {
return otherService;
}
public void setOtherService(TextView otherService) {
this.otherService = otherService;
}
}
public interface LeftClick{
void onLeftClick(int pos,firstBean bean);
}
private LeftClick leftClick;
public void setLeftClick(LeftClick leftClick) {
this.leftClick = leftClick;
}
}
另外一个适配器的代码:
package com.example.tetst2.ui.dashboard;
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.tetst2.R;
import com.example.tetst2.ui.Bean.ServiceListBean;
import com.example.tetst2.ui.Util.HttpUtil;
import com.example.tetst2.ui.Util.MyApplication;
import java.util.List;
public class MyRightRecyclerAdapter extends RecyclerView.Adapter<MyRightRecyclerAdapter.MyViewHolder> {
private Context context;
private List<ServiceListBean> mlist;
public MyRightRecyclerAdapter(Context context, List<ServiceListBean> mlist) {
this.context = context;
this.mlist = mlist;
}
@NonNull
@Override
public MyRightRecyclerAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
/*
* 在我们的这个位置的话就是设置我们的相关的方法
* */
View inflate = LayoutInflater.from(context).inflate(R.layout.require_model,parent,false);
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull MyRightRecyclerAdapter.MyViewHolder holder, int position) {
Glide.with(context).load(HttpUtil.BASE_URL+mlist.get(position).getImgUrl()).into(holder.imagesRequire);
holder.textsRequire.setText(mlist.get(position).getServiceName());
}
@Override
public int getItemCount() {
return mlist.size();
}
public void setMlist(List<ServiceListBean> mlist) {
this.mlist = mlist;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView imagesRequire;
private TextView textsRequire;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
/*
* 在我们的这个位置的话就是设置我们的相关的模板
* */
imagesRequire = (ImageView) itemView.findViewById(R.id.images_require);
textsRequire = (TextView) itemView.findViewById(R.id.texts_require);
}
}
}
然后的话就是我们的需要补充的代码的相关部分:
package com.example.tetst2.ui.Bean;
public class firstBean {
private String DictValue;
private String DictLabel;
public firstBean(String dictValue, String dictLabel) {
DictValue = dictValue;
DictLabel = dictLabel;
}
public String getDictValue() {
return DictValue;
}
public void setDictValue(String dictValue) {
DictValue = dictValue;
}
public String getDictLabel() {
return DictLabel;
}
public void setDictLabel(String dictLabel) {
DictLabel = dictLabel;
}
@Override
public String toString() {
return "firstBean{" +
"DictValue='" + DictValue + '\'' +
", DictLabel='" + DictLabel + '\'' +
'}';
}
}