切り替えレイアウトの検索

23464 ワード

Bean


> ShopBean

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

ホームV層の検索SousuoActivity


レイアウト

<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 list;
    private List 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();
                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 data;
    private OnItemClickListener mOnItemClickListener;
    public SousuoAdapter(Context context, List 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);
}