//第一个页面 实现搜索商品
public class SearchActivity extends AppCompatActivity {//if (str.equals("劳力士")) {// OkhttpUtils.doGet(Api.SEARCHURL, new GsonObjectCallback() {// @Override// public void onUi(SearchBean searchBean) {// Intent intent = new Intent(SearchActivity.this, SearchShowActivity.class);// intent.putExtra("searchbean", searchBean);// intent.putExtra("s", str);// startActivity(intent);// }//// @Override// public void onFailed(Call call, IOException e) {//// }// });// } private SearchView searchView; private SearchALG searchALG; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); searchView = (SearchView) findViewById(R.id.searchView); initData(); searchView.setOnSearchListener(new MyOnSearchListener()); } private List changedHintDatas; /** * 设置searview的监听 */ class MyOnSearchListener implements SearchView.OnSearchListener { /** * 搜索回调 * * @param searchText 进行搜索的文本 */ @Override public void onSearch(final String searchText) { if (!TextUtils.isEmpty(searchText)) { Toast.makeText(SearchActivity.this, "完成搜索" + searchText, Toast.LENGTH_SHORT).show(); if (searchText.equals("laolishi")) { OkhttpUtils.doGet(Api.SEARCHURL, new GsonObjectCallback () { @Override public void onUi(SearchBean searchBean) { Intent intent = new Intent(SearchActivity.this, SearchShowActivity.class); intent.putExtra("searchbean", searchBean); intent.putExtra("s", searchText); startActivity(intent); } @Override public void onFailed(Call call, IOException e) { } }); } } else { Toast.makeText(SearchActivity.this, "搜索内容不能为空!", Toast.LENGTH_SHORT).show(); } } /** * 刷新提示列表 * * @param changedText 改变后的文本 */ @Override public void onRefreshHintList(String changedText) { if (changedHintDatas == null) { changedHintDatas = new ArrayList<>(); } else { changedHintDatas.clear(); } if (TextUtils.isEmpty(changedText)) { return; } for (int i = 0; i < hint_datas.size(); i++) { String hint_data = hint_datas.get(i); boolean isAdd = searchALG.isAddToHintList(hint_data, changedText); if (isAdd) { changedHintDatas.add(hint_datas.get(i)); } } /** * 根据搜索框文本的变化,动态的改变提示的listView */ searchView.updateHintList(changedHintDatas); } } //热搜数据 private List hot_datas; //提示列表数据 private List hint_datas; private void initData() { hot_datas = new ArrayList<>(); hint_datas = new ArrayList<>(); searchALG = new SearchALG(this); for (int i = 0; i < 10; i++) { hot_datas.add("Android Hot " + i); } //设置热搜数据显示的列数 searchView.setHotNumColumns(2); //设置热搜数据 searchView.setHotSearchDatas(hot_datas); /** * 设置提示数据的集合 */ for (int i = 0; i < 10; i++) { hint_datas.add("ts" + "安卓学习" + "Android Hint " + i + " 你好"); } /** * 设置自动保存搜索记录 */ searchView.keepSearchHistory(true); //设置提示列表的最大显示列数 searchView.setMaxHintLines(8); //设置保存搜索记录的个数 searchView.setMaxHistoryRecordCount(6); }}
//第一个页面 的布局名字
//第一个布局点击条目数进入到的详情页
public class SearchShowActivity extends AppCompatActivity implements View.OnClickListener/*, PullLoadMoreRecyclerView.PullLoadMoreListener*/ { private ImageView iv_searchshowback; private EditText et_searchshow; private Button bt_searchshow; private PullLoadMoreRecyclerView rv_searchshow; private SearchBean searchbean; private MyAdapter_SearchShow searchShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_show); initView(); //获取传入的值 final Intent intent = getIntent(); //获取值 searchbean = (SearchBean) intent.getSerializableExtra("searchbean"); String s = intent.getStringExtra("s"); et_searchshow.setText(s); getSearchData(); bt_searchshow.setOnClickListener(this); searchShow.setOnRecycler(new MyAdapter_SearchShow.OnRecycler() { @Override public void onrecyclerview(int position) { Intent i = new Intent(SearchShowActivity.this,DetailsActivity.class); i.putExtra("goods_id",searchbean.getDatas().getGoods_list().get(position).getGoods_id()); startActivity(i); } }); } private void initView() { iv_searchshowback = (ImageView) findViewById(R.id.iv_searchshowback); et_searchshow = (EditText) findViewById(R.id.et_searchshow); bt_searchshow = (Button) findViewById(R.id.bt_searchshow); rv_searchshow = (PullLoadMoreRecyclerView) findViewById(R.id.rv_searchshow); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_searchshowback: finish(); break; } } private void getSearchData() { rv_searchshow.setLinearLayout(); Listgoods_list = searchbean.getDatas().getGoods_list(); if(searchShow == null){ searchShow = new MyAdapter_SearchShow(SearchShowActivity.this,goods_list); rv_searchshow.setAdapter(searchShow); } }}
//详情页的布局名字叫activity_search_show
//详情页的适配器
public class MyAdapter_SearchShow extends RecyclerView.Adapter{ private Context context; private List list; public MyAdapter_SearchShow(Context context, List list) { this.context = context; this.list = list; } //上下拉刷新加载数据 public void loadMord(List goodsListBeen,boolean flag){ for (int i = 0;i
//适配器里的布局文件名字item_searchshow
//添加到购物车
public class DetailsActivity extends BaseActivity implements View.OnClickListener { private ImageView iv_detailsback; private ImageView d_googs_pic; private TextView d_goods_name; private TextView d_goods_price; private TextView area_name; private TextView content; private TextView if_store_cn; private WebView webview; private Button buy_now; private Button add_cart; private String goods_id; private String goodsid; private static final int SDK_PAY_FLAG = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); initView(); //获取传入的值 goods_id = getIntent().getStringExtra("goods_id"); //获取商品详情数据 getDetailsData(); //获取webview展示数据 getWebViewData(); } /** * 展示详情数据 */ private void getDetailsData() { OkhttpUtils.doGet(Api.DetailsURL + goods_id, new GsonObjectCallback() { @Override public void onUi(DetailsBean detailsBean) { goodsid = detailsBean.getDatas().getGoods_info().getGoods_id(); DetailsBean.DatasBean.GoodsInfoBean goods_info = detailsBean.getDatas().getGoods_info(); ImageLoader.getInstance().displayImage(detailsBean.getDatas().getGoods_image(),d_googs_pic, MyApp.getDisplay()); d_goods_name.setText(goods_info.getGoods_name()); d_goods_price.setText("¥"+goods_info.getGoods_price()); area_name.setText(detailsBean.getDatas().getGoods_hair_info().getArea_name()); content.setText(detailsBean.getDatas().getGoods_hair_info().getContent()); if_store_cn.setText(detailsBean.getDatas().getGoods_hair_info().getIf_store_cn()); } @Override public void onFailed(Call call, IOException e) { } }); } /** * 获取webview数据 */ private void getWebViewData() { WebSettings settings = webview.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); webview.setWebChromeClient(new WebChromeClient()); webview.setWebViewClient(new WebViewClient()); webview.loadUrl(Api.WebViewURL+goods_id); } private void initView() { iv_detailsback = (ImageView) findViewById(R.id.iv_detailsback); d_googs_pic = (ImageView) findViewById(R.id.d_googs_pic); d_goods_name = (TextView) findViewById(R.id.d_goods_name); d_goods_price = (TextView) findViewById(R.id.d_goods_price); area_name = (TextView) findViewById(R.id.area_name); content = (TextView) findViewById(R.id.content); if_store_cn = (TextView) findViewById(R.id.if_store_cn); webview = (WebView) findViewById(R.id.webview); buy_now = (Button) findViewById(R.id.buy_now); add_cart = (Button) findViewById(R.id.add_cart); buy_now.setOnClickListener(this); add_cart.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.buy_now: //立即支付 postData(); break; case R.id.add_cart: if(Api.sharedPreferences.getBoolean("isLogin",false) == false){ startActivity(new Intent(DetailsActivity.this,LoginActivity.class)); return; } //获取登录成功之后保存的key值 存入map集合 Map map = new HashMap<>(); map.put("key",Api.sharedPreferences.getString("key","")); map.put("goods_id",goodsid); map.put("quantity",1+""); OkhttpUtils.doPost(map, Api.CartADDURL, new GsonObjectCallback () { @Override public void onUi(UnregBean unregBean) { if(unregBean.getCode() == 200){ Toast.makeText(DetailsActivity.this,"成功添加到购物车",Toast.LENGTH_SHORT).show(); } } @Override public void onFailed(Call call, IOException e) { } }); break; case R.id.iv_detailsback: finish(); break; } } private void postData() { //添加参数,url中的ip可以换成我们自己的后台ip String url = "http://169.254.255.250:8080/PayServer/AlipayDemo"; StringBuffer sb = new StringBuffer("?"); sb.append("subject="); sb.append("来自Server测试的商品"); sb.append("&"); sb.append("body="); sb.append("该测试商品的详细描述"); sb.append("&"); sb.append("total_fee="); sb.append("0.01"); String urll = url + sb.toString(); OkhttpUtils.doGet(urll, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { final String string = response.body().string(); Runnable payRunnable = new Runnable() { @Override public void run() { // 构造PayTask 对象 PayTask alipay = new PayTask(DetailsActivity.this); // 调用支付接口,获取支付结果 String result = alipay.pay(string, true); // Log.i("TAG", "走了pay支付方法............."); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; Thread payThread = new Thread(payRunnable); payThread.start(); } }); } //这里需要一个handler private Handler mHandler = new Handler() { @SuppressWarnings("unused") public void handleMessage(Message msg) { switch (msg.what) { case SDK_PAY_FLAG: { PayResult payResult = new PayResult((String) msg.obj); /** * 同步返回的结果必须放置到服务端进行验证(验证的规则请看https://doc.open.alipay.com/doc2/ * detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665& * docType=1) 建议商户依赖异步通知 */ String resultInfo = payResult.getResult();// 同步返回需要验证的信息 String resultStatus = payResult.getResultStatus(); // 判断resultStatus 为“9000”则代表支付成功,具体状态码代表含义可参考接口文档 if (TextUtils.equals(resultStatus, "9000")) { Toast.makeText(DetailsActivity.this, "支付成功", Toast.LENGTH_SHORT).show(); } else { // 判断resultStatus 为非"9000"则代表可能支付失败 // "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态) if (TextUtils.equals(resultStatus, "8000")) { Toast.makeText(DetailsActivity.this, "支付结果确认中", Toast.LENGTH_SHORT).show(); } else { // 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误 Toast.makeText(DetailsActivity.this, "支付失败", Toast.LENGTH_SHORT).show(); } } break; } default: break; } } };}
//添加购物车布局名字activity_details
//接口
public class Api { public static String API_IP = "192.168.23.22";//这一行改成自己的IP地址 public static String Client = "android"; public static String IndexURL = "http://m.yunifang.com/yunifang/mobile/home"; public static String REGURL = "http://"+API_IP+"/mobile/index.php?act=login&op=register"; public static String LOGINURL = "http://"+API_IP+"/mobile/index.php?act=login"; public static String TYPEBEANURL = "http://"+API_IP+"/mobile/index.php?act=goods_class"; public static String UNREG = "http://"+API_IP+"/mobile/index.php?act=logout"; public static String SEARCHURL = "http://"+API_IP+"/mobile/index.php?act=goods&op=goods_list&page=100"; public static String WebViewURL = "http://"+API_IP+"/mobile/index.php?act=goods&op=goods_body&goods_id="; public static String DetailsURL = "http://"+API_IP+"/mobile/index.php?act=goods&op=goods_detail&goods_id="; public static String CartADDURL = "http://"+API_IP+"/mobile/index.php?act=member_cart&op=cart_add"; public static String ShopShowURL = "http://"+API_IP+"/mobile/index.php?act=member_cart&op=cart_list"; public static String DeleteURL = "http://"+API_IP+"/mobile/index.php?act=member_cart&op=cart_del"; public static String AddressURL = "http://"+API_IP+"/mobile/index.php?act=member_address&op=address_list"; public static String AddAddressURL = "http://"+API_IP+"/mobile/index.php?act=member_address&op=address_add"; public static String DeleteAddress = "http://"+API_IP+"/mobile/index.php?act=member_address&op=address_del"; public static String UpdateAddress = "http://"+API_IP+"/mobile/index.php?act=member_address&op=address_edit"; public static SharedPreferences sharedPreferences; public static SharedPreferences.Editor editor; public static void init(Context context){ sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); editor = sharedPreferences.edit(); }}
//权限
//依赖
//picasso图片加载依赖 //recyclerview依赖 compile project(':okhttplibrary') compile project(':searchview_library') compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.android.support:recyclerview-v7:23.2.0' compile 'com.google.code.gson:gson:2.8.2' compile 'org.greenrobot:eventbus:3.0.0' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.wuxiaolong.pullloadmorerecyclerview:library:1.0.4' testCompile 'junit:junit:4.12' compile 'cn.yipianfengye.android:zxing-library:2.1' compile 'com.youth.banner:banner:1.4.9' compile 'com.github.bumptech.glide:glide:3.7.0' compile files('libs/alipaySdk-20170725.jar')