搜索详情

来源:互联网 发布:单片机控制方案 编辑:程序博客网 时间:2024/05/24 01:50



搜索第一个页面


public class MainActivity extends AppCompatActivity {    private Liushi mFlowLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mFlowLayout = (Liushi) findViewById(R.id.liu);        initChildViews();    }    private String mNames[] = {            "welcome","android","TextView",            "apple","jamy","kobe bryant",            "jordan","layout","viewgroup",            "margin","padding","text",            "name","type","search","logcat",            "shanshan","woaini1314","tiangchangdijiu"            ,"玫瑰花","珊珊","ssddssdsdsdsdsdsdsadsadsadsdsad",            "ssddssdsdsdsdsdsdsadsadsadsdsad"    };    private void initChildViews() {        // TODO Auto-generated method stub        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        lp.leftMargin = 20;        lp.rightMargin = 20;        lp.topMargin = 10;        lp.bottomMargin = 10;        for(int i = 0; i < mNames.length; i ++){            TextView view = new TextView(this);            view.setText(mNames[i]);            view.setTextColor(Color.WHITE);            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));            mFlowLayout.addView(view,lp);        }    }    public void sousuo(View view) {        Intent intent=new Intent(MainActivity.this,SouSuoActivity.class);        startActivity(intent);    }}
布局

<LinearLayout    android:orientation="vertical"    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="bwie.com.zhoukao3.view.activity.MainActivity">    <LinearLayout        android:background="#fff"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:background="#fff"            android:text="返回"            android:layout_weight="2"            android:layout_width="0dp"            android:layout_height="wrap_content" />        <EditText            android:hint="多功能插座"            android:layout_weight="6"            android:layout_width="0dp"            android:layout_height="wrap_content" />        <Button            android:onClick="sousuo"            android:background="#fff"            android:text="搜索"            android:layout_weight="2"            android:layout_width="0dp"            android:layout_height="wrap_content" />    </LinearLayout>    <TextView        android:layout_marginLeft="10dp"        android:textSize="25dp"        android:text="热搜"    android:layout_width="match_parent"    android:layout_height="wrap_content" />    <bwie.com.zhoukao3.view.liushi.Liushi        android:id="@+id/liu"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </bwie.com.zhoukao3.view.liushi.Liushi>    <TextView        android:layout_marginTop="5dp"        android:layout_marginBottom="5dp"        android:background="#3333"        android:layout_width="match_parent"        android:layout_height="5dp" />    <TextView        android:layout_marginLeft="10dp"        android:textSize="25dp"        android:text="历史搜索"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <ListView        android:layout_width="match_parent"        android:layout_height="wrap_content"></ListView>    <Button        android:layout_marginTop="20dp"        android:text="清除历史记录"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

IMainacivity

public interface IMainactivity {    void onSuccess(ShangPinBean shangPinBean);}
Clicklistner

public interface ClickListner {    public void onItemClick(int position);}
PMainactivt

public interface PMainactivity {    void onSuccess(ShangPinBean shangPinBean);}
ShangPinPresenter 

public class ShangPinPresenter implements PMainactivity {    private  IMainactivity iMainactivity;    private final ShangPinModel shangPinModel;    public ShangPinPresenter(IMainactivity iMainactivity) {        this.iMainactivity=iMainactivity;        shangPinModel = new ShangPinModel(this);    }    public void userpresenter(String keywords,String page,String url){        shangPinModel.usermodel(keywords,page,url);    }    @Override    public void onSuccess(ShangPinBean shangPinBean) {        iMainactivity.onSuccess(shangPinBean);    }}
ShangPinModel 

public class ShangPinModel {    private  PMainactivity pMainactivity;    public ShangPinModel(PMainactivity pMainactivity) {        this.pMainactivity=pMainactivity;    }    public void usermodel(String keywords,String page,String url){        Map<String, String> params = new HashMap<>();        params.put("keywords",keywords);        params.put("page",page);        OkHttp3Util.doPost(url, params, 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();                ShangPinBean shangPinBean = new Gson().fromJson(json, ShangPinBean.class);                pMainactivity.onSuccess(shangPinBean);            }        });    }}
SouSuoActivity 

public class SouSuoActivity extends AppCompatActivity implements IMainactivity{    private List<ShangPinBean.DataBean> list;    private RadioButton cb;    private EditText name;    private boolean flag=false;    private RecyclerView rv;    private ShangPinPresenter shangPinPresenter;    private int num=1;    private TextView sou;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_sou_suo);        cb = (RadioButton) findViewById(R.id.cb);        name = (EditText) findViewById(R.id.name);        sou = (TextView) findViewById(R.id.sou);        rv = (RecyclerView) findViewById(R.id.rv);            cb.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    if(flag)                    {                        flag=false;                        cb.setChecked(true);                        shurv();                    }else                    {                        cb.setChecked(false);                        flag=true;                        shurv();                    }                }            });    shangPinPresenter = new ShangPinPresenter(this);        sou.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                shurv();            }        });}    public void shurv(){        String data = name.getText().toString();        list=new ArrayList<>();        if(!"".equals(data))        {            shangPinPresenter.userpresenter(data,num+"","https://www.zhaoapi.cn/product/searchProducts");        }    }    @Override    public void onSuccess(final ShangPinBean shangPinBean) {        runOnUiThread(new Runnable() {            @Override            public void run() {                String code = shangPinBean.getCode();                if("0".equals(code))                {                    List<ShangPinBean.DataBean> data = shangPinBean.getData();                    list.addAll(data);                    boolean checked = cb.isChecked();                    if(checked)                    {                        shipei();                    }else {                        sjip();                    }                }            }        });    }    public void shipei()    {        rv.setLayoutManager(new LinearLayoutManager(SouSuoActivity.this, LinearLayoutManager.VERTICAL,false));        //设置适配器        ShuAdapter simpleRecycleAdapter = new ShuAdapter(SouSuoActivity.this,list);        rv.setAdapter(simpleRecycleAdapter);        simpleRecycleAdapter.setonitemclicklistner(new ClickListner() {            @Override            public void onItemClick(int position) {                int pid = list.get(position).getPid();                tiaozhuan("4084",pid+"");            }        });    }    public void sjip(){        rv.setLayoutManager(new GridLayoutManager(SouSuoActivity.this,2));        //设置适配器        GridviewAdapter simpleRecycleAdapter = new GridviewAdapter(SouSuoActivity.this,list);        rv.setAdapter(simpleRecycleAdapter);        simpleRecycleAdapter.setonitemclicklistner(new ClickListner() {            @Override            public void onItemClick(int position) {                int pid = list.get(position).getPid();                tiaozhuan("4084",pid+"");            }        });    }    public void tiaozhuan(String uid,String pid){        Intent intent=new Intent(SouSuoActivity.this,XiangQingActivity.class);        intent.putExtra("uid",uid);        intent.putExtra("pid",pid);        startActivity(intent);    }}
XiangQingActivity 

public class XiangQingActivity extends AppCompatActivity implements XBanner.XBannerAdapter {    private TextView price;    private TextView bargainPrice;    private TextView name;    private ShangPinBean2 shangPinbean;    private ImageView vp;    private LinearLayout linearLayout;    private List<ImageView> images;    private String uid;    private String pid;    private XBanner banner_1;    private List<String> list;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_xiang_qing);        price = (TextView) findViewById(R.id.price);        bargainPrice = (TextView) findViewById(R.id.bargainPrice);        name = (TextView) findViewById(R.id.name);        linearLayout= (LinearLayout) findViewById(R.id.linear_layout);       // vp = (ImageView) findViewById(R.id.vp);        banner_1 = (XBanner) findViewById(R.id.banner_1);        xingqing();    }    //获取网络商品    private void xingqing() {        Intent intent = getIntent();        uid = intent.getStringExtra("uid");        pid = intent.getStringExtra("pid");        Log.d("AAAAAAAAAAAAAA",pid);        Map<String,String> map=new HashMap<>();        map.put("pid", pid);        OkHttp3Util.doPost("https://www.zhaoapi.cn/product/getProductDetail", map, 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();                shangPinbean = new Gson().fromJson(json, ShangPinBean2.class);                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        String code = shangPinbean.getCode();                        if("0".equals(code))                        {                            price.setText(shangPinbean.getData().getPrice()+"");                            bargainPrice.setText(shangPinbean.getData().getBargainPrice()+"");                            name.setText(shangPinbean.getData().getTitle());                            String[] split = shangPinbean.getData().getImages().split("\\|");                            list = new ArrayList<String>();                            for (String a:                                    split) {                                list.add(a);                            }                            banner_1.setData(list);                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    banner_1.setmAdapter(XiangQingActivity.this);                                }                            });                           // ImageLoader.getInstance().displayImage(shangPinbean.getData().getImages().split("\\|")[0],vp, ImageLoaderUtil.getDefaultOption());                        }else                        {                            Toast.makeText(XiangQingActivity.this, shangPinbean.getMsg(),Toast.LENGTH_SHORT).show();                        }                    }                });            }        });    }    //加入购物车    public void addcar(View view) {        Map<String,String> map=new HashMap<>();        map.put("uid",uid);        map.put("pid",pid);        OkHttp3Util.doPost("https://www.zhaoapi.cn/product/addCart", map, 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();                final JiaBean yonghu = new Gson().fromJson(json, JiaBean.class);                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        String code = yonghu.getCode();                        if("0".equals(code))                        {                            Toast.makeText(XiangQingActivity.this,yonghu.getMsg(),Toast.LENGTH_SHORT).show();                        }else                        {                            Toast.makeText(XiangQingActivity.this,yonghu.getMsg(),Toast.LENGTH_SHORT).show();                        }                    }                });            }        });    }    @Override    public void loadBanner(XBanner banner, View view, int position) {        ImageLoader.getInstance().displayImage(list.get(position),(ImageView) view, ImageLoaderUtil.getDefaultOption());    }}
GridviewAdapter 

public class GridviewAdapter extends RecyclerView.Adapter<GridviewHorder>{    private List<ShangPinBean.DataBean> list;    private Context context;    private ClickListner clickListner;    public GridviewAdapter(Context context, List<ShangPinBean.DataBean> list) {        this.context = context;        this.list = list;    }    //....回想listView的优化,,,1.创建viewHolder....2.与条目的视图进行绑定的    //onCreateViewHolder...创建一个ViewHolder....根据条目的视图创建自己的holder对象    @Override    public GridviewHorder onCreateViewHolder(ViewGroup parent, int viewType) {        //inflater...条目的视图        View view = LayoutInflater.from(context).inflate(R.layout.simple_list_item2, parent, false);        GridviewHorder holder = new GridviewHorder(view);        return holder;    }    /**     * onBindViewHolder 绑定上viewHolder之后的操作...主要是赋值(textView,button,checkBox...)的操作     * @param holder     * @param position     */    @Override    public void onBindViewHolder(GridviewHorder holder, final int position) {        ImageLoader.getInstance().displayImage(list.get(position).getImages(),holder.img, ImageLoaderUtil.getDefaultOption());        holder.title.setText(list.get(position).getTitle());        holder.price.setText(list.get(position).getPrice()+"");        holder.title.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                clickListner.onItemClick(position);            }        });    }    /**     * 条目的数量     * @return     */    @Override    public int getItemCount() {        return list.size();    }    public void setonitemclicklistner(ClickListner clickListner)    {        this.clickListner=clickListner;    }}

public class ShuAdapter extends RecyclerView.Adapter<ShuHorder> {    private List<ShangPinBean.DataBean> list;    private Context context;    private ClickListner clickListner;    public ShuAdapter(Context context, List<ShangPinBean.DataBean> list) {        this.context = context;        this.list = list;    }    //....回想listView的优化,,,1.创建viewHolder....2.与条目的视图进行绑定的    //onCreateViewHolder...创建一个ViewHolder....根据条目的视图创建自己的holder对象    @Override    public ShuHorder onCreateViewHolder(ViewGroup parent, int viewType) {        //inflater...条目的视图        View view = LayoutInflater.from(context).inflate(R.layout.shu_layout, parent, false);        ShuHorder holder = new ShuHorder(view);        return holder;    }    /**     * onBindViewHolder 绑定上viewHolder之后的操作...主要是赋值(textView,button,checkBox...)的操作     * @param holder     * @param position     */    @Override    public void onBindViewHolder(ShuHorder holder, final int position) {        ImageLoader.getInstance().displayImage(list.get(position).getImages().split("\\|")[0],holder.img, ImageLoaderUtil.getRoundOption());        holder.title.setText(list.get(position).getTitle());        holder.price.setText(list.get(position).getPrice()+"");        holder.title.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                clickListner.onItemClick(position);}        });    }    /**     * 条目的数量     * @return     */    @Override    public int getItemCount() {        return list.size();    }    public void setonitemclicklistner(ClickListner clickListner)    {        this.clickListner=clickListner;    }}
GridviewHorder 

public class GridviewHorder extends RecyclerView.ViewHolder{    public TextView title;    public ImageView img;    public  TextView price;    public GridviewHorder(View itemView) {        super(itemView);        title = itemView.findViewById(R.id.title);        img = itemView.findViewById(R.id.img);        price = itemView.findViewById(R.id.price);    }}


public class ShuHorder extends RecyclerView.ViewHolder {    public TextView title;    public ImageView img;    public  TextView price;    public ShuHorder(View itemView) {        super(itemView);        title = itemView.findViewById(R.id.title);        img = itemView.findViewById(R.id.img);        price = itemView.findViewById(R.id.price);    }}
   show.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!--根据选中和未选中的状态,去设置背景颜色的图片-->    <item android:state_checked="true" android:drawable="@drawable/kind_liner"/>    <item android:state_checked="false" android:drawable="@drawable/kind_grid"/>    <item android:drawable="@drawable/kind_grid"/></selector>
textview_bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#666666" />    <corners android:radius="10dp" />    <padding        android:left="5dp"        android:right="5dp"        android:top="5dp"        android:bottom="5dp"        /></shape>
搜索布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:orientation="vertical"    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="bwie.com.zhoukao3.view.activity.SouSuoActivity">    <LinearLayout        android:background="#fff"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:background="#fff"            android:text="返回"            android:layout_weight="2"            android:layout_width="0dp"            android:layout_height="wrap_content" />        <EditText            android:id="@+id/name"            android:hint="多功能插座"            android:layout_weight="6"            android:layout_width="0dp"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/sou"            android:text="搜索"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <RadioButton            android:id="@+id/cb"            android:checked="true"            android:button="@null"            android:layout_marginLeft="10dp"            android:onClick="sousuo"            android:background="@drawable/show"            android:layout_weight="1"            android:layout_width="5dp"            android:layout_height="50dp" />    </LinearLayout>    <android.support.v7.widget.RecyclerView        android:id="@+id/rv"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </android.support.v7.widget.RecyclerView></LinearLayout>
详情页面

<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:orientation="vertical"    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="bwie.com.zhoukao3.view.activity.XiangQingActivity">    <RelativeLayout        android:gravity="center_vertical"        android:background="#fff"        android:layout_width="match_parent"        android:layout_height="40dp">        <Button            android:text="返回"            android:background="#fff"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:text="商品详情"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerVertical="true"            android:layout_centerHorizontal="true" />    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <com.stx.xhb.xbanner.XBanner            xmlns:app="http://schemas.android.com/apk/res-auto"            android:id="@+id/banner_1"            android:layout_width="match_parent"            android:layout_height="180dp"            app:pointsPosition="CENTER"            app:AutoPlayTime="3000"            app:pointsContainerBackground="#44aaaaaa"           />        <LinearLayout            android:orientation="horizontal"            android:id="@+id/linear_layout"            android:layout_alignBottom="@+id/banner_1"            android:layout_centerHorizontal="true"            android:layout_marginBottom="20dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content">        </LinearLayout>    </RelativeLayout>    <TextView        android:id="@+id/name"        android:layout_marginLeft="20dp"        android:layout_marginTop="20dp"        android:text="sdfdfsfsd"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/price"        android:text="原价:999.99"        android:layout_marginLeft="20dp"        android:layout_marginTop="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/bargainPrice"        android:textColor="#f00"        android:text="原价:99.99"        android:layout_marginLeft="20dp"        android:layout_marginTop="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:layout_marginTop="50dp"        android:onClick="addcar"        android:text="加入购物车"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

详情布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <ImageView        android:id="@+id/img"        android:src="@mipmap/ic_launcher"        android:layout_width="80dp"        android:layout_height="match_parent" />    <LinearLayout        android:layout_marginLeft="10dp"        android:gravity="center"        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView            android:layout_marginBottom="10dp"            android:id="@+id/title"            android:text="sdsdsdsds"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/price"            android:text="dfdsfsfsdff"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>

GridviewAdapter

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <ImageView        android:id="@+id/img"        android:src="@mipmap/ic_launcher"        android:layout_width="170dp"        android:layout_height="match_parent" />        <TextView            android:layout_marginBottom="10dp"            android:id="@+id/title"            android:text="sdsdsdsds"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/price"            android:text="dfdsfsfsdff"            android:layout_width="wrap_content"            android:layout_height="wrap_content" /></LinearLayout>

ShuAdapter

<?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="80dp">    <ImageView        android:id="@+id/img"        android:src="@mipmap/ic_launcher"        android:layout_width="80dp"        android:layout_height="match_parent" />    <LinearLayout        android:layout_marginLeft="10dp"        android:gravity="center"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">        <TextView            android:layout_marginBottom="10dp"            android:id="@+id/title"            android:text="sdsdsdsds"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/price"            android:text="dfdsfsfsdff"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>


原创粉丝点击