图片加载 二维码 解析

编程入门 行业动态 更新时间:2024-10-28 18:24:19

图片<a href=https://www.elefans.com/category/jswz/34/1771433.html style=加载 二维码 解析"/>

图片加载 二维码 解析

图片加载 二维码 解析

1. layout布局文件

(1)activity_category.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".view.activity.MainActivity"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/categoryList"android:background="@color/grgray"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"/><FrameLayoutandroid:layout_below="@+id/categoryList"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/productList"android:layout_width="wrap_content"android:layout_height="wrap_content"/><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/secondList"android:layout_width="wrap_content"android:layout_height="wrap_content"/></FrameLayout></RelativeLayout>

(2)activity_zxing.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".view.activity.MainActivity"><Buttonandroid:id="@+id/button"android:layout_width="100dp"android:layout_height="50dp"/></RelativeLayout>

(3)item_category.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="#99000000"><TextViewandroid:id="@+id/oneName"android:textSize="20sp"android:layout_margin="20dp"android:textColor="@color/encode_view"android:layout_width="wrap_content"android:layout_height="wrap_content"/></RelativeLayout>

(4)item_category_products.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"><com.facebook.drawee.view.SimpleDraweeViewandroid:id="@+id/icon"android:layout_alignParentTop="true"android:layout_width="100dp"app:placeholderImage="@mipmap/ic_launcher"android:layout_centerInParent="true"android:layout_height="100dp"/><TextViewandroid:id="@+id/productName"android:textSize="16sp"android:layout_centerHorizontal="true"android:layout_below="@+id/icon"android:textColor="@color/grgray"android:layout_width="wrap_content"android:layout_height="wrap_content"/></RelativeLayout>

(5)item_category_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="#99111111"><com.facebook.drawee.view.SimpleDraweeViewandroid:id="@+id/icon"android:layout_alignParentTop="true"android:layout_width="100dp"app:placeholderImage="@mipmap/ic_launcher"android:layout_centerInParent="true"android:layout_height="100dp"/><TextViewandroid:id="@+id/secondName"android:textSize="20sp"android:layout_centerHorizontal="true"android:layout_below="@+id/icon"android:textColor="@color/encode_view"android:layout_width="wrap_content"android:layout_height="wrap_content"/></RelativeLayout>

2.java文件

(1)application包下的

1.Constant.java

package cj.com.a1711amvpproject.application;public class Constant {public static final String BASE_URL= "/";public static final String REGISTER = "user/v1/register";public static final String LOGIN = "user/v1/login";public static final String PRODUCT_LIST = "commodity/v1/commodityList";public static final String ODER_LIST = "order/verify/v1/findOrderListByStatus";public static final String CATEGORY_LIST = "commodity/v1/findCategory";public static final String CATEGORY_PRODUCT_LIST = "commodity/v1/findCommodityByCategory";}

2.MyApplication.java

package cj.com.a1711amvpproject.application;import android.app.Application;
import android.content.Context;import com.facebook.drawee.backends.pipeline.Fresco;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;public class MyApplication extends Application {public static Context mContext;@Overridepublic void onCreate() {super.onCreate();mContext = this;ZXingLibrary.initDisplayOpinion(this);Fresco.initialize(this);}
}

(2)contract包下的

ICategoryContract.java

package cj.com.a1711amvpproject.contract;import cj.com.a1711amvpproject.model.bean.CategoryBean;
import cj.com.a1711amvpproject.model.bean.HomeBean;
import cj.com.a1711amvpproject.model.bean.ProductList;
import cj.com.a1711amvpproject.view.interfaces.IBaseView;public interface ICategoryContract {//viewinterface ICategoryView extends IBaseView {void getCategorySuccess(CategoryBean bean);void getProductListSuccess(ProductList productList);}//presenterinterface ICategoryPresenter{void getCategory();}//modelinterface ICategoryModel{void getHomeData();}
}

(3)model包下的

一.bean包

1.CategoryBean.java

                             alt+s即可

2.ProductList.java

                             alt+s即可

二.http包

HttpUtils2.java

public class HttpUtils2 {private static HttpUtils2 httpUtils;private HttpUtils2() {}public static HttpUtils2 getInstance() {if (httpUtils == null) {httpUtils = new HttpUtils2();}return httpUtils;}private CallbackData mCallbackData;//get 请求public void getData(String url,CallbackData callbackData) {this.mCallbackData = callbackData;//1、初始化 HttpClient 对象//OkHttpClient okHttpClient = new OkHttpClient();OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new LogginInterceptor()).build();//2、创建Request对象 先创建Build 的对象 new XXX.Build(); .......build;//why 1、设置属性(构造方法、setter getter)Request request = new Request.Builder().url(url).get().build();//3、把 request 赋值给okHttpClient 创建call 对象Call call = okHttpClient.newCall(request);//同步请求//call.execute();//4、调用 异步请求call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {}@Overridepublic void onResponse(Call call, Response response) throws IOException {//子线程String data = response.body().string();Message message = handler.obtainMessage();message.obj = data;handler.sendMessage(message);Log.e("myMessage", "=== " + data);}});}@SuppressLint("HandlerLeak")Handler handler = new Handler(){@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);String data = (String) msg.obj;mCallbackData.success(data);}};public interface CallbackData{void success(String data);}//post 请求public void postData(String url, HashMap<String, String> params) {//1、初始化 HttpClient 对象//OkHttpClient okHttpClient = new OkHttpClient();OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new LogginInterceptor()).build();//2、创建RequestBody 对象FormBody.Builder builder = new FormBody.Builder();Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, String> next = iterator.next();String key = next.getKey();String value = next.getValue();builder.add(key, value);}FormBody body = builder.build();//3、创建Request对象Request request = new Request.Builder().url(url).post(body).build();//4、把 request 赋值给okHttpClient 创建call 对象Call call = okHttpClient.newCall(request);//同步请求//call.execute();//5、调用 异步请求call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {}@Overridepublic void onResponse(Call call, Response response) throws IOException {//子线程String data = response.body().string();Log.e("myMessage", "=== " + data);}});}public class LogginInterceptor implements Interceptor{@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();Response response =  chain.proceed(request);return response;}}}

(4)presenter包下的

1.BasePresenter.java

public class BasePresenter<V extends IBaseView> {private V view;public V getView() {return view;}public void setView(V view) {this.view = view;}public void detachView() {if (view != null) {view = null;}}
}

2.CategroyPresenter.java

public class CategroyPresenter extends BasePresenter<ICategoryContract.ICategoryView> implements ICategoryContract.ICategoryPresenter {private final HttpUtils2 httpUtils2;public CategroyPresenter(){httpUtils2 = HttpUtils2.getInstance();}@Overridepublic void getCategory() {httpUtils2.getData(Constant.BASE_URL + Constant.CATEGORY_LIST, new HttpUtils2.CallbackData() {@Overridepublic void success(String data) {Gson gson = new Gson();CategoryBean categoryBean = gson.fromJson(data, CategoryBean.class);getView().getCategorySuccess(categoryBean);}});}public void getProductsById(String categoryId,int page){String url = Constant.BASE_URL + Constant.CATEGORY_PRODUCT_LIST +"?categoryId="+categoryId+"&page="+page+"&count=10";httpUtils2.getData(url, new HttpUtils2.CallbackData() {@Overridepublic void success(String data) {Gson gson = new Gson();ProductList productList = gson.fromJson(data, ProductList.class);getView().getProductListSuccess(productList);}});}
}

(5)view包下的

1.BasePresenter.java

一.activity包

(1)BaseActivity.java

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView {private P presenter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(setSelfContentView());initView();presenter = setSelfPresenter();if(presenter!=null) {presenter.setView(this);}initData();}abstract int setSelfContentView();abstract void initView();abstract P setSelfPresenter();abstract void initData();public P getPresenter() {return presenter;}@Overrideprotected void onDestroy() {super.onDestroy();if(presenter!=null) {presenter.detachView();}}
}

(2)CategoryActivity.java

public class CategoryActivity extends BaseActivity<CategroyPresenter> implements ICategoryContract.ICategoryView {private RecyclerView recyclerView;private RecyclerView secondList;private RecyclerView productList;private CategoryAdapter categoryAdapter;private CategorySecondAdapter categorySecondAdapter;private CategoryProductAdapter categoryProductAdapter;private List<CategoryBean.ResultBean> resultBeans;@Overrideint setSelfContentView() {return R.layout.activity_category;}@Overridevoid initView() {//一级分类recyclerView = findViewById(R.id.categoryList);categoryAdapter = new CategoryAdapter(this, new CategoryAdapter.ClickItemCallback() {@Overridepublic void clickItem(View view, int position) {TextView textView = (TextView) view;textView.setTextColor(Color.RED);categorySecondAdapter.setData(resultBeans.get(position).getSecondCategoryVo());}});recyclerView.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));recyclerView.setAdapter(categoryAdapter);//二级分类secondList = findViewById(R.id.secondList);secondList.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));categorySecondAdapter = new CategorySecondAdapter(this, new CategorySecondAdapter.ClickItemCallback() {@Overridepublic void clickItem(String categoryId) {loadProductById(categoryId);}});secondList.setAdapter(categorySecondAdapter);//商品列表productList = findViewById(R.id.productList);productList.setLayoutManager(new GridLayoutManager(this,2));categoryProductAdapter = new CategoryProductAdapter(this);productList.setAdapter(categoryProductAdapter);}@OverrideCategroyPresenter setSelfPresenter() {return new CategroyPresenter();}@Overridevoid initData() {getPresenter().getCategory();}private void loadProductById(String categoryId) {getPresenter().getProductsById(categoryId,1);}@Overridepublic void getCategorySuccess(CategoryBean bean) {resultBeans = bean.getResult();categoryAdapter.setData(resultBeans);}@Overridepublic void getProductListSuccess(ProductList productList) {categoryProductAdapter.setData(productList.getResult());}
}

(3)ZxingActivity.java

public class ZxingActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_zxing);findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(ZxingActivity.this, CaptureActivity.class);startActivityForResult(intent, 1001);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);/*** 处理二维码扫描结果*/if (requestCode == 1001) {//处理扫描结果(在界面上显示)if (null != data) {Bundle bundle = data.getExtras();if (bundle == null) {return;}if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {String result = bundle.getString(CodeUtils.RESULT_STRING);Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {Toast.makeText(this, "解析二维码失败", Toast.LENGTH_LONG).show();}}}}
}

二.adapter包

(1)CategoryAdapter.java

public class CategoryAdapter extends RecyclerView.Adapter {private Context mContext;private ClickItemCallback mClickItemCallback;public CategoryAdapter(Context context,ClickItemCallback clickItemCallback) {this.mContext = context;this.mClickItemCallback = clickItemCallback;}@NonNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(mContext).inflate(R.layout.item_category, null);return new ViewHolder(view);}ArrayList<TextView> arrayList = new ArrayList<>();@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {ViewHolder viewHolder = (ViewHolder) holder;viewHolder.oneName.setText(list.get(position).getName());arrayList.add(viewHolder.oneName);viewHolder.oneName.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {for (int i = 0; i < arrayList.size(); i++) {TextView textView = arrayList.get(i);textView.setTextColor(Color.WHITE);}mClickItemCallback.clickItem(v,position);notifyDataSetChanged();}});}public interface ClickItemCallback{void clickItem(View view,int position);}@Overridepublic int getItemCount() {if(list!=null) {return list.size();}return 0;}private List<CategoryBean.ResultBean> list;public void setData(List<CategoryBean.ResultBean> result) {list = result;notifyDataSetChanged();}class ViewHolder extends RecyclerView.ViewHolder{TextView oneName;public ViewHolder(@NonNull View itemView) {super(itemView);oneName = itemView.findViewById(R.id.oneName);}}
}

(2)CategoryProductAdapter.java

public class CategoryProductAdapter extends RecyclerView.Adapter {private Context mContext;public CategoryProductAdapter(Context context) {this.mContext = context;}@NonNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(mContext).inflate(R.layout.item_category_products, null);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {ViewHolder viewHolder = (ViewHolder) holder;viewHolder.productName.setText(list.get(position).getCommodityName());viewHolder.icon.setImageURI(list.get(position).getMasterPic());viewHolder.icon.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {}});}@Overridepublic int getItemCount() {if(list!=null) {return list.size();}return 0;}private List<ProductList.ResultBean> list;public void setData(List<ProductList.ResultBean> result) {list = result;notifyDataSetChanged();}class ViewHolder extends RecyclerView.ViewHolder{TextView productName;SimpleDraweeView icon;View view;public ViewHolder(@NonNull View itemView) {super(itemView);view = itemView;productName = itemView.findViewById(R.id.productName);icon = itemView.findViewById(R.id.icon);}}
}

(3)CategorySecondAdapter.java

public class CategorySecondAdapter extends RecyclerView.Adapter {private Context mContext;private ClickItemCallback clickItemCallback;public CategorySecondAdapter(Context context,ClickItemCallback clickItemCallback) {this.mContext = context;this.clickItemCallback = clickItemCallback;}@NonNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(mContext).inflate(R.layout.item_category_second, null);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {ViewHolder viewHolder = (ViewHolder) holder;viewHolder.secondName.setText(list.get(position).getName());//viewHolder.icon.setImageURI();viewHolder.icon.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {clickItemCallback.clickItem(list.get(position).getId());}});}public interface ClickItemCallback{void clickItem(String categoryId);}@Overridepublic int getItemCount() {if(list!=null) {return list.size();}return 0;}private List<CategoryBean.ResultBean.SecondCategoryVoBean> list;public void setData(List<CategoryBean.ResultBean.SecondCategoryVoBean> result) {list = result;notifyDataSetChanged();}class ViewHolder extends RecyclerView.ViewHolder{TextView secondName;SimpleDraweeView icon;View view;public ViewHolder(@NonNull View itemView) {super(itemView);view = itemView;secondName = itemView.findViewById(R.id.secondName);icon = itemView.findViewById(R.id.icon);}}
}

三.interfaces包

IBaseView.java

public interface IBaseView {
}

3.assets文件

test.html文档导入

4.AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""package="cj.com.a1711amvpproject"><applicationandroid:name=".application.MyApplication"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".view.activity.CategoryActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".view.activity.WebViewActivity"/></application><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.INTERNET" /></manifest>

更多推荐

图片加载 二维码 解析

本文发布于:2023-06-23 16:36:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/851779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   图片   二维码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!