MVP+搜索+recyclerview+springview

图如下:


记得要导okhttp的原生包即可;

导依赖:

    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.github.liuguangqiang.SuperRecyclerView:super-recyclerview:0.1.2'
    compile 'com.liaoinstan.springview:library:1.2.6'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

代码如下:
首先是Mainactivity布局:
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="搜索商品"
            android:textSize="20dp"/>

        <ImageView
            android:id="@+id/image"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/san"/>
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:hint="请输入关键字"/>

        <Button
            android:id="@+id/sousuo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="搜索"/>
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"/>

    <com.liaoinstan.springview.widget.SpringView

        android:id="@+id/springview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"/>
    </com.liaoinstan.springview.widget.SpringView>

在是适配器的两个布局:
 <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ssdsdsddfffddffdfd"
            android:textSize="20dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/price"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="原价:的颠三倒四"/>

            <TextView
                android:id="@+id/youhui"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="大的司法改革"
                android:textColor="#ff0000"/>
        </LinearLayout>
    </LinearLayout>


//---------------------------------------------------------------------------------------------------------------------------------------------------------------------


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


    <TextView
        android:id="@+id/title1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ssdsdsddfffddffdfd"
        android:textSize="20dp"/>


    <TextView
        android:id="@+id/price1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="原价:的颠三倒四"/>

    <TextView
        android:id="@+id/youhui1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大的司法改革"
        android:textColor="#ff0000"/>

MVP的model传给mymolder的参数:
public interface SouModel {
    public void get(String name, String page, Callback callback);
}
Mymodel

public class MySouModel implements SouModel {
    @Override
    public void get(String name, String page, Callback callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("keywords",name);
        map.put("page",page);
        OkHttp3Utils.doPost("http://120.27.23.105/product/searchProducts?source=android",map,callback);

    }
}

MVP的view:

public interface SouView {
    public void success(Bean bean);

    public void failuer(Exception e);
}


MVP的persenter:

public class SouPersenter {
    SouView view;
    Context context;
    private final MySouModel model;

    public SouPersenter(SouView view, Context context) {
        this.view = view;
        this.context = context;
        model = new MySouModel();
    }
    public void getData(String name,String page){
        model.get(name, page, new OnUiCallback() {
            @Override
            public void onFailed(Call call, IOException e) {
                if (view != null){
                    view.failuer(e);
                }
            }

            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Bean bean = gson.fromJson(result, Bean.class);
                view.success(bean);
            }
        });
    }
    public void sasa(){
        this.view = null;
    }
}


MainActivity页面:

public class MainActivity extends AppCompatActivity implements SouView {

    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.sousuo)
    Button sousuo;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.springview)
    SpringView springview;
    SouPersenter persenter = new SouPersenter(this, this);
    List<Bean.DataBean> list = new ArrayList<>();
    private boolean flag = true;
    private MyAdapter adapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        springview.setHeader(new DefaultHeader(this));
        springview.setFooter(new DefaultFooter(this));
        //设置SpringView的刷新监听事件
        springview.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                springview.onFinishFreshAndLoad();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onLoadmore() {
                persenter.getData(name.getText().toString(), page + "");
                springview.onFinishFreshAndLoad();

            }
        });


    }

    @OnClick({R.id.image, R.id.sousuo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image:
                if (flag == false) {
                    flag = true;
                    image.setImageResource(R.drawable.san);
                    LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
                    recyclerview.setLayoutManager(manager);
                    adapter = new MyAdapter(this, list, flag);
                    recyclerview.setAdapter(adapter);
                    setadapter();
                } else if (flag == true) {
                    flag = false;
                    image.setImageResource(R.drawable.si);
                    GridLayoutManager manager = new GridLayoutManager(this, 2);
                    recyclerview.setLayoutManager(manager);
                    adapter = new MyAdapter(this, list, flag);
                    recyclerview.setAdapter(adapter);
                    setadapter();
                }
                break;
            case R.id.sousuo:

                list.clear();								
                if (flag == true) {								
                    LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
                    recyclerview.setLayoutManager(manager);
                    adapter = new MyAdapter(this, list, flag);
                    recyclerview.setAdapter(adapter);
                } else if (flag == false) {
                    GridLayoutManager manager = new GridLayoutManager(this, 2);
                    recyclerview.setLayoutManager(manager);
                    adapter = new MyAdapter(this, list, flag);
                    recyclerview.setAdapter(adapter);
                }
                persenter.getData(name.getText().toString(), "1");
                break;
        }
    }

    private void setadapter() {

        if (adapter != null) {
            adapter = new MyAdapter(this, list, flag);
            recyclerview.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void success(Bean bean) {
//        for (int i = 0; i <bean.getData().size() ; i++) {
//            list.add(bean.getData().get(i));
//        }
        list.addAll(bean.getData());
        adapter = new MyAdapter(this, list, flag);
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void failuer(Exception e) {
        Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        persenter.sasa();
    }
}



adapter适配器页面:

public class MyAdapter extends RecyclerView.Adapter {
    Context context;
    List<Bean.DataBean> list;
    boolean flag;


    public MyAdapter(Context context, List<Bean.DataBean> list, boolean flag) {
        this.flag = flag;
        this.context = context;
        this.list = list;
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (flag == true) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_adapter, null);
            return new MyViewHolder(view);
        } else if (flag == false) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_si, null);
            return new ViewHolder01(view);
        }
        return new MyViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder holder1 = (MyViewHolder) holder;
            holder1.title.setText(list.get(position).getTitle());
            holder1.youhui.setText("优惠价:" + list.get(position).getPrice());
            holder1.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线(删除线)
            holder1.price.setText("原价:¥" + list.get(position).getBargainPrice());
            String[] split = list.get(position).getImages().split("\\|");
            ImageLoader.getInstance().displayImage(split[0], holder1.image);
        }else if (holder instanceof ViewHolder01) {
            ViewHolder01 holder1 = (ViewHolder01) holder;
            holder1.title1.setText(list.get(position).getTitle());
            holder1.youhui1.setText("优惠价:" + list.get(position).getPrice());
            holder1.price1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线(删除线)
            holder1.price1.setText("原价:¥" + list.get(position).getBargainPrice());
            String[] split = list.get(position).getImages().split("\\|");
            ImageLoader.getInstance().displayImage(split[0], holder1.image1);
        }

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        ImageView image;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.youhui)
        TextView youhui;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    static class ViewHolder01 extends RecyclerView.ViewHolder {
        @BindView(R.id.image1)
        ImageView image1;
        @BindView(R.id.title1)
        TextView title1;
        @BindView(R.id.price1)
        TextView price1;
        @BindView(R.id.youhui1)
        TextView youhui1;

        ViewHolder01(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值