<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#F00"
>
<ImageView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/common_btn_menu"
android:paddingTop="17dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
/>
<EditText
android:id="@+id/ed_seach"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/btn_seach"
android:background="@drawable/login1"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:hint="请输入搜索商品"
android:layout_toRightOf="@+id/tab1"
android:paddingLeft="10dp" />
<Button
android:id="@+id/btn_seach"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:text="搜索"
android:background="@drawable/login1"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.jcodecraeer.xrecyclerview.XRecyclerView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/layouts"
>
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@mipmap/sousong"
/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
public class FragMentShouye extends Fragment implements MainView {
private MianPresenters mianPresenters;
private XRecyclerView recyclerView;
private EditText mEdSeach;
private int page=1;
private MyAdapter myAdapter;
private RelativeLayout layouts;
private String contentSeach;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shoufragment,container,false);
layouts = view.findViewById(R.id.layouts);
recyclerView = view.findViewById(R.id.recycler);
mEdSeach = view.findViewById(R.id.ed_seach);
//设置布局管理器
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
recyclerView.setLayoutManager(gridLayoutManager);
//设置适配器
myAdapter = new MyAdapter(getActivity());
recyclerView.setAdapter(myAdapter);
//上拉加载
recyclerView.setLoadingMoreEnabled(true);
recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
//下拉刷新
mianPresenters.doCommodity(contentSeach,page);
}
@Override
public void onLoadMore() {
//上拉加载
mianPresenters.doCommodity(contentSeach,page);
}
});
mianPresenters = new MianPresenters(new MainModels(),this);
//点击搜索商品
view.findViewById(R.id.btn_seach).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//搜索商品
seach();
}
});
//默认情况下展示数据
// mianPresenters.doCommodity("皮鞋",page);
return view;
}
//搜索商品
private void seach() {
//获取用户输入的信息
contentSeach = mEdSeach.getText().toString().trim();
Log.e("message",contentSeach);
// // //判断是否为空
if (TextUtils.isEmpty(contentSeach)){
Toast.makeText(getActivity(),"请输入要搜索的商品",Toast.LENGTH_LONG).show();
return;
}
mianPresenters.doCommodity(contentSeach,page);
}
@Override
public void success(String json) {
Beans beans = new Gson().fromJson(json, Beans.class);
//关闭上拉
recyclerView.refreshComplete();
//关闭下拉
recyclerView.loadMoreComplete();
//如果搜索没有数据,显示小图片,否则显示数据
if (beans.getResult().size()==0){
//显示
layouts.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(),"抱歉,没有找到商品额~~",Toast.LENGTH_LONG).show();
}else {
//不显示
layouts.setVisibility(View.GONE);
myAdapter.setList(beans.getResult());
Toast.makeText(getActivity(),"有您要的商品哦~~",Toast.LENGTH_LONG).show();
}
//传递数据
myAdapter.setList(beans.getResult());
}
@Override
public void fail() {
//失败
recyclerView.refreshComplete();
recyclerView.loadMoreComplete();
}
}