搜索框

<?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();

    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章