搜索显示

1.MainActivity

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private RadioGroup rg;
    private ArrayList<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = findViewById(R.id.vp);
        rg = findViewById(R.id.rg);
        RadioButton rb1 = findViewById(R.id.rb);
        RadioButton rb2 = findViewById(R.id.rb2);
        RadioButton rb3 = findViewById(R.id.rb3);
        //创建一个Fragment集合
        list = new ArrayList<>();
        Frag01 frag01 = new Frag01();
        Frag02 frag02 = new Frag02();
        Frag03 frag03 = new Frag03();
        list.add(frag01);
        list.add(frag02);
        list.add(frag03);
        rg.check(list.get(0).getId());
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                rg.check(rg.getChildAt(i).getId());

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        vp.setCurrentItem(2);
                        break;
                }
            }
        });
    }
}

3.OKHttpUtils

public class OkHttpUtils {
    private static OkHttpUtils okHttpUtils  = null;
    private HttpLoggingInterceptor loggingInterceptor;

    private OkHttpUtils(){

    }
    public static OkHttpUtils getInstance(){
     //同步锁
     if(okHttpUtils==null){
         synchronized (OkHttpUtils.class){
             if(okHttpUtils==null){
                 okHttpUtils = new OkHttpUtils();
             }
         }
     }
     return okHttpUtils;
    }
    //创建拦截器
    public Interceptor getintercepter(){
        loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("mmm",message);
            }
        });
        return loggingInterceptor;
    }
    public void doGet(String url, Callback callback){
        //解析
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(getintercepter())
                .build();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }
}

2.Z_View

public class Z_View extends RelativeLayout {
    public Z_View(Context context) {
        super(context);
    }

    public Z_View(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.custom,this);

    }


    public Z_View(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

3.Adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    final static int TYPE_ONE = 0;
    final static int TYPE_TWO = 1;

    Context context;
    ArrayList<Result> result;

    public MyAdapter(Context context, ArrayList<Result> result) {
        this.context = context;
        this.result = result;
    }

//    获取
    @Override
    public int getItemViewType(int position) {
        return position % 2;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

//        使用ONE 等于i
        if (TYPE_ONE == i){
            //        找布局
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item1,null,false);

            MyViewHolder myViewHolder = new MyViewHolder(view);

            return myViewHolder;
        }else {
            //        找布局
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item2,null,false);

            MyViewHolder myViewHolder = new MyViewHolder(view);

            return myViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

        int itemViewType = getItemViewType(i);

        Result result = this.result.get(i);


        if (itemViewType==TYPE_ONE){
            myViewHolder.item1_title1.setText(result.getCommodityName());
            Glide.with(context).load(result.getMasterPic()).into(myViewHolder.item1_image1);
        }else {
            myViewHolder.item2_title1.setText(result.getCommodityName());
            Glide.with(context).load(result.getMasterPic()).into(myViewHolder.item2_image1);
            Glide.with(context).load(result.getMasterPic()).into(myViewHolder.item2_image2);
        }
    }

    @Override
    public int getItemCount() {
        return result.size();
    }

    public  class MyViewHolder extends RecyclerView.ViewHolder{

        private final TextView item1_title1;
        private final ImageView item1_image1;

        private final TextView item2_title1;
        private final ImageView item2_image1;
        private final ImageView item2_image2;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

//            1
            item1_title1 = itemView.findViewById(R.id.item1_title1);
            item1_image1 = itemView.findViewById(R.id.item1_image1);
//            2
            item2_title1 = itemView.findViewById(R.id.item2_title1);
            item2_image1 = itemView.findViewById(R.id.item2_image1);
            item2_image2 = itemView.findViewById(R.id.item2_image2);

        }
    }
}

4.ShowModel

public class ShowModel {
    private String url = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=";
    private String path="&count=10&page=";
    private String xie = "";
    private String page1 = "";

    public interface onShowLisenter{

        void onResult(String json);
    }

    public onShowLisenter showLisenter;

    public void setShowLisenter(onShowLisenter onShowLisenter) {
        this.showLisenter = onShowLisenter;
    }


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String json = (String) msg.obj;
                    showLisenter.onResult(json);
                    break;
            }
        }
    };
    public void show(String trim,int page){
        xie=trim;
        page1=page+"";
        OkHttpUtils.getInstance().doGet(url + xie + path + page1, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Log.i("xxxc",json);
                Message message = new Message();
                message.what=0;
                message.obj=json;
                handler.sendMessage(message);
            }
        });

    }
}

5.ShowView

public interface ShowView {
    void getViewData(String json);
}

6.ShowPresenter

public class ShowPresenter {

    private final ShowModel showModel;
    private final ShowView showView;

    public ShowPresenter(ShowView view){
        showModel = new ShowModel();
        showView = view;
    }

    public void show(String trim, int page) {
        showModel.show(trim,page);
        showModel.setShowLisenter(new ShowModel.onShowLisenter() {
            @Override
            public void onResult(String json) {
                showView.getViewData(json);
            }
        });
    }
}

7.Frag01

public class Frag01 extends Fragment implements ShowView {

    private RecyclerView rlv;
    private SwipyRefreshLayout src;
    private EditText seek;
    private Button button;
    private ShowPresenter presenter;
    private String title = "板鞋";
    private int page = 1;
    private Handler handler = new Handler();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_01,null,false);
        //找控件
        rlv = view.findViewById(R.id.rlv);
        //设置上拉下拉
        src = view.findViewById(R.id.frag01_src);
        //搜索控件
        seek = view.findViewById(R.id.seek);
        button = view.findViewById(R.id.sou);
        //布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rlv.setLayoutManager(linearLayoutManager);

        presenter = new ShowPresenter(this);
        initData();

        //设置颜色
        src.setColorSchemeColors(R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark);
        //设置模式
        src.setDirection(SwipyRefreshLayoutDirection.BOTH);
        //设置sw 监听
        src.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(int index) {
                page = 1;
                presenter.show(title,page);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "刷新成功", Toast.LENGTH_SHORT).show();
                        src.setRefreshing(false);
                    }
                },2000);
            }

            @Override
            public void onLoad(int index) {
                page++;
                presenter.show(title,page);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Toast.makeText(getActivity(), "加载成功", Toast.LENGTH_SHORT).show();
                        src.setRefreshing(false);
                    }
                },2000);
            }
        });
        return view;
    }
    private void initData() {

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                title = seek.getText().toString();

                if (TextUtils.isEmpty(title)){
                    Toast.makeText(getActivity(), "请输入要搜索的内容", Toast.LENGTH_SHORT).show();
                }else {
                    presenter.show(title,page);
                }
            }
        });
    }

    @Override
    public void getViewData(String json) {

        Gson gson = new Gson();
        JsonBean fromJson = gson.fromJson(json, JsonBean.class);
        ArrayList<Result> result = fromJson.getResult();
        MyAdapter myAdapter = new MyAdapter(getContext(), result);
        rlv.setAdapter(myAdapter);
    }
}

8.Layout

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        ></android.support.v4.view.ViewPager>
    <RadioGroup
        android:id="@+id/rg"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb"
            android:button="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="首页"
            android:layout_weight="1"
            />
        <RadioButton
            android:id="@+id/rb2"
            android:button="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二页"
            android:layout_weight="1"
            />
        <RadioButton
            android:id="@+id/rb3"
            android:button="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我的"
            android:layout_weight="1"
            />
    </RadioGroup>
</LinearLayout>

frag01

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

   <com.bawei.weeklianxi.Z_View
       android:layout_width="match_parent"
       android:layout_height="50dp" />

    <com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout

        android:id="@+id/frag01_src"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rlv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </android.support.v7.widget.RecyclerView>

    </com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout>


</LinearLayout>

custom

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="50dp">

    <ImageView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        />

    <EditText
        android:id="@+id/seek"
        android:layout_width="0dp"
        android:hint="请输入要搜索的商品"
        android:gravity="center"
        android:layout_weight="1"
        android:layout_height="50dp" />

    <Button
        android:id="@+id/sou"
        android:text="搜索"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="50dp" />
</LinearLayout>

item1

<?xml version="1.0" encoding="utf-8"?>`在这里插入代码片`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/item1_image1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher_round"
        />
    <TextView
        android:id="@+id/item1_title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈"
        />


</LinearLayout>

item2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">



    <ImageView
        android:id="@+id/item2_image1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher_round"
        />

    <ImageView
        android:id="@+id/item2_image2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher_round"
        />

    <TextView
        android:id="@+id/item2_title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈"
        />

</LinearLayout>

1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值