搜索切换布局

Bean

ShopBean


https://www.zhaoapi.cn/product/searchProducts

搜索 主页 V层SousuoActivity##

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="weilin.bwei.com.what_is_interface.xiangqing.Xiangqing">
   <android.support.v4.view.ViewPager
       android:layout_width="match_parent"
       android:layout_height="300dp"
       android:id="@+id/vp"
       ></android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp"
        android:layout_below="@+id/vp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/xiangqing_tv1"
            android:textSize="18dp"
            android:text="标题"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/xiangqing_tv2"
            android:textColor="#f00"
            android:textSize="18dp"
            android:text="原价"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/xiangqing_tv3"
            android:textSize="18dp"
            android:text="现价"
            />
    </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >
        <TextView
            android:onClick="addCart"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="加入购物车"
            android:textSize="20sp"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:background="@android:color/holo_red_dark"
            />
    </LinearLayout>
</RelativeLayout>

public class SousuoActivity extends AppCompatActivity implements ISousuoActivity{

    private EditText et_sousuo;
    private XRecyclerView xrecy;
    private XRecyclerView xrecy2;
    private SousuoPersenter sousuoPersenter;
    private SousuoAdapter adapter;
    private Sousuo2Adapter adapter2;

    private int page = 1;
    private List<ShopBean.DataBean> list;
    private List<ShopBean.DataBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sousuo);
        et_sousuo = (EditText) findViewById(R.id.et_sousuo);
        xrecy = (XRecyclerView) findViewById(R.id.xrecy);
        xrecy2 = (XRecyclerView) findViewById(R.id.xrecy2);
        sousuoPersenter = new SousuoPersenter(this);
        xrecy.setLayoutManager(new GridLayoutManager(SousuoActivity.this, 2, LinearLayoutManager.VERTICAL, false));
        xrecy2.setLayoutManager(new LinearLayoutManager(SousuoActivity.this, LinearLayoutManager.VERTICAL, false));

    }
    public void sousuo(View view){
        sousuoPersenter.getSou(et_sousuo.getText().toString(),page);
    }
    boolean flag = false;
    public void huan(View view){
        if (flag){
            flag = false;
            xrecy.setLayoutManager(new LinearLayoutManager(SousuoActivity.this,LinearLayoutManager.VERTICAL,false ));
            //刷新
            adapter.notifyDataSetChanged();
        }else {
            xrecy.setLayoutManager(new GridLayoutManager(SousuoActivity.this, 2, LinearLayoutManager.VERTICAL, false));
            adapter.notifyDataSetChanged();
            flag = true;
        }
    }
    @Override
    public void onSuccess(final ShopBean shopBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                list = new ArrayList<ShopBean.DataBean>();
                data = shopBean.getData();

                list.addAll(data);
                setAdapter();
                xrecy.setLoadingListener(new XRecyclerView.LoadingListener() {
                    @Override
                    public void onRefresh() {
                        list.clear();
                        setAdapter();
                        page = 1;
                        sousuoPersenter.getSou(et_sousuo.getText().toString(),page);

                    }

                    @Override
                    public void onLoadMore() {
                        Toast.makeText(SousuoActivity.this, "加载了更多", Toast.LENGTH_SHORT).show();

                        page++;
                        sousuoPersenter.getSou(et_sousuo.getText().toString(),page);

                    }
                });
                xrecy2.setLoadingListener(new XRecyclerView.LoadingListener() {
                    @Override
                    public void onRefresh() {
                        list.clear();
                        setAdapter();
                        page = 1;
                        sousuoPersenter.getSou(et_sousuo.getText().toString(),page);

                    }
                    @Override
                    public void onLoadMore() {
                        Toast.makeText(SousuoActivity.this, "加载了更多", Toast.LENGTH_SHORT).show();
                        page++;
                        sousuoPersenter.getSou(et_sousuo.getText().toString(),page);
                    }
                });
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onClick(int position) {
                        Intent intent = new Intent(SousuoActivity.this, Xiangqing.class);
                        intent.putExtra("pid",data.get(position).getPid());
                        startActivity(intent);

                    }
                });
                adapter2.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onClick(int position) {
                        Intent intent = new Intent(SousuoActivity.this, Xiangqing.class);
                        intent.putExtra("pid",data.get(position).getPid());
                        startActivity(intent);
                    }
                });
            }
        });
    }
    public void setAdapter(){

        if (adapter == null){
            adapter = new SousuoAdapter(SousuoActivity.this, list);
            //分割线
            xrecy.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }

        if (adapter2 == null){
            adapter2 = new Sousuo2Adapter(SousuoActivity.this, list);
            //分割线
            xrecy2.setAdapter(adapter2);
        }else{
            adapter2.notifyDataSetChanged();
        }

        xrecy.refreshComplete();
        xrecy2.refreshComplete();
    }
}

接口


public interface ISousuoActivity {
    void onSuccess(ShopBean shopBean);
}

P层 SousuoPersenter##

package weilin.bwei.com.what_is_interface.Search.persenter;

import weilin.bwei.com.what_is_interface.Search.view.ISousuoActivity;
import weilin.bwei.com.what_is_interface.Search.bean.ShopBean;
import weilin.bwei.com.what_is_interface.Search.model.SousuoModel;

/**
 * Created by lenovo on 2018/1/18.
 */

public class SousuoPersenter implements ISousuoPersenter{

    private SousuoModel sousuoModel;
    ISousuoActivity iSousuoActivity;
    public SousuoPersenter(ISousuoActivity iSousuoActivity) {
        this.iSousuoActivity = iSousuoActivity;
        sousuoModel = new SousuoModel(this);
    }

    public void getSou(String name, int page) {
        sousuoModel.getSou(name,page);
    }

    @Override
    public void onSuccess(ShopBean shopBean) {
        iSousuoActivity.onSuccess(shopBean);
    }
}

接口


public interface ISousuoPersenter {
    void onSuccess(ShopBean shopBean);
}

M层 SousuoModel

public class SousuoModel {
    ISousuoPersenter iSousuoPersenter;

    public SousuoModel(ISousuoPersenter iSousuoPersenter) {
        this.iSousuoPersenter = iSousuoPersenter;
    }

    public void getSou(String name, int page) {

        OkHttp3Util_02.doGet("https://www.zhaoapi.cn/product/searchProducts?keywords="+name+"&page="+page+"&source=android", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    //得到请求下来的数据
                    String string = response.body().string();
                    Gson gson = new Gson();
                    //解析
                    ShopBean shopBean = gson.fromJson(string, ShopBean.class);
                    iSousuoPersenter.onSuccess(shopBean);
                }
            }
        });
    }
}

适配器 第一个recylerview的适配器
========================

public class SousuoAdapter extends XRecyclerView.Adapter<SousuoAdapter.vieholder>{

    Context context;
    List<ShopBean.DataBean> data;
    private OnItemClickListener mOnItemClickListener;
    public SousuoAdapter(Context context, List<ShopBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public SousuoAdapter.vieholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_sousuo,parent,false);

        vieholder vieholder=new vieholder(view);
        return vieholder;
    }

    @Override
    public void onBindViewHolder(SousuoAdapter.vieholder holder, final int position) {
        holder.tv.setText(data.get(position).getTitle());
        holder.tv2.setText("$"+data.get(position).getBargainPrice());
        String[] split = data.get(position).getImages().split("\\|");
        Glide.with(context).load(split[0]).into(holder.img);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class vieholder extends XRecyclerView.ViewHolder{
        ImageView img;
        TextView tv, tv2;
        public vieholder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.item_img);
            tv = itemView.findViewById(R.id.item_title);
            tv2 = itemView.findViewById(R.id.item_price);
        }
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this. mOnItemClickListener=onItemClickListener;
    }
}

点击 接口

public interface OnItemClickListener {
    void onClick( int position);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值