自定义View圆
//第一个页面 实现搜索商品
public class SearchActivity extends AppCompatActivity { //if (str.equals("劳力士")) { // OkhttpUtils.doGet(Api.SEARCHURL, new GsonObjectCallback<SearchBean>() { // @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<String> 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<SearchBean>() { @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<String> hot_datas; //提示列表数据 private List<String> 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); } }
//第一个页面 的布局名字
<LinearLayout 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" android:orientation="vertical" tools:context="com.bawei.xiangmutwo.activity.SearchActivity"> <com.example.searchview_library.SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
//第一个布局点击条目数进入到的详情页
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(); List<SearchBean.DatasBean.GoodsListBean> goods_list = searchbean.getDatas().getGoods_list(); if(searchShow == null){ searchShow = new MyAdapter_SearchShow(SearchShowActivity.this,goods_list); rv_searchshow.setAdapter(searchShow); } } }
//详情页的布局名字叫activity_search_show
<LinearLayout 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" android:orientation="vertical" tools:context="com.bawei.xiangmutwo.activity.SearchShowActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="55dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/iv_searchshowback" android:layout_width="13dp" android:layout_height="15dp" android:layout_marginLeft="8dp" android:background="@mipmap/back"/> <TextView android:layout_marginLeft="25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_centerVertical="true" android:text="@string/searchFrom" /> <EditText android:id="@+id/et_searchshow" android:layout_width="210dp" android:layout_height="40dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <Button android:id="@+id/bt_searchshow" android:layout_width="70dp" android:layout_height="40dp" android:text="搜索" android:textSize="20sp" android:textColor="#ffffff" android:layout_marginLeft="10dp" android:background="@drawable/btn_yuanjiao_login"/> </LinearLayout> <com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView android:id="@+id/rv_searchshow" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" /> </LinearLayout>
//详情页的适配器
public class MyAdapter_SearchShow extends RecyclerView.Adapter<MyAdapter_SearchShow.MyViewHolder> { private Context context; private List<SearchBean.DatasBean.GoodsListBean> list; public MyAdapter_SearchShow(Context context, List<SearchBean.DatasBean.GoodsListBean> list) { this.context = context; this.list = list; } //上下拉刷新加载数据 public void loadMord(List<SearchBean.DatasBean.GoodsListBean> goodsListBeen,boolean flag){ for (int i = 0;i<goodsListBeen.size();i++){ if(flag == true){ this.list.addAll(0,goodsListBeen); }else{ this.list.addAll(goodsListBeen); } } notifyDataSetChanged(); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //加载布局 View inflate = LayoutInflater.from(context).inflate(R.layout.item_searchshow, parent, false); final MyViewHolder myViewHolder = new MyViewHolder(inflate); inflate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onRecycler.onrecyclerview(myViewHolder.getPosition()); } }); return myViewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { //获取数据 SearchBean.DatasBean.GoodsListBean goodsListBean = list.get(position); //展示数据 holder.goods_name.setText(goodsListBean.getGoods_name()); holder.goods_price.setText("¥"+goodsListBean.getGoods_price()); holder.store_name.setText(goodsListBean.getStore_name()); ImageLoader.getInstance().displayImage(goodsListBean.getGoods_image_url(),holder.goods_image_url, MyApp.getDisplay()); } @Override public int getItemCount() { return list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { private final ImageView goods_image_url; private final TextView goods_price,store_name,goods_name; public MyViewHolder(View itemView) { super(itemView); //获取资源id goods_image_url = (ImageView) itemView.findViewById(R.id.goods_image_url); goods_name = (TextView) itemView.findViewById(R.id.goods_name); goods_price = (TextView) itemView.findViewById(R.id.goods_price); store_name = (TextView) itemView.findViewById(R.id.store_name); } } //接口回调设置监听事件 private OnRecycler onRecycler; public interface OnRecycler{ void onrecyclerview(int position); } public void setOnRecycler(OnRecycler onRecycler) { this.onRecycler = onRecycler; } }
//适配器里的布局文件名字item_searchshow
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/goods_image_url" android:layout_width="130dp" android:layout_height="130dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/goods_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="17sp" android:text="111"/> <TextView android:id="@+id/goods_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#ff5001" android:layout_marginTop="10dp" android:text="111"/> <TextView android:id="@+id/store_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="111"/> </LinearLayout> </LinearLayout>
//添加到购物车
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<DetailsBean>() { @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<String,String> 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<UnregBean>() { @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
<LinearLayout 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" android:orientation="vertical" tools:context="com.bawei.xiangmutwo.activity.DetailsActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#ff5001" android:orientation="horizontal"> <ImageView android:id="@+id/iv_detailsback" android:layout_width="13dp" android:layout_height="15dp" android:layout_marginLeft="8dp" android:background="@mipmap/back" android:layout_centerVertical="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商品详情" android:textSize="25sp" android:textColor="#ffffff" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="10"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/d_googs_pic" android:layout_width="match_parent" android:layout_height="250dp"></ImageView> <TextView android:id="@+id/d_goods_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="23sp" android:background="@color/colorWhite2" android:layout_marginTop="5dp"/> <TextView android:id="@+id/d_goods_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="@color/colorPrice" android:layout_marginTop="5dp" android:background="@color/colorWhite2"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite2" android:layout_marginTop="5dp"> <TextView android:id="@+id/area_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/if_store_cn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" /> </RelativeLayout> <ImageView android:layout_width="fill_parent" android:layout_height="250dp" android:background="@mipmap/xiangqing"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商品详情" android:textSize="21sp" android:layout_marginTop="5dp" android:layout_gravity="center_horizontal"/> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="wrap_content"></WebView> </LinearLayout> </ScrollView> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ffffff" android:orientation="horizontal"> <ImageView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="center_vertical" android:src="@mipmap/shop" /> <ImageView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="center_vertical" android:src="@mipmap/kefu"/> <ImageView android:layout_weight="1" android:layout_width="match_parent" android:layout_gravity="center_vertical" android:layout_height="50dp" android:src="@mipmap/shoucang"/> <Button android:id="@+id/buy_now" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="立即购买" android:background="#FF5100" android:textColor="@color/colorWhite2" android:layout_alignParentRight="true" android:layout_alignParentBottom="true"/> <Button android:id="@+id/add_cart" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="添加到购物车" android:textColor="@color/colorWhite2" android:background="#ff9c02" android:layout_toLeftOf="@id/buy_now" android:layout_alignParentBottom="true" /> </LinearLayout> </LinearLayout>
//接口
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(); } }
//权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
//依赖
//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')