图片加载 二维码 解析

   日期:2020-05-22     浏览:129    评论:0    
核心提示:图片加载 二维码 解析1. layout布局文件(1)activity_category.xml

图片加载 二维码 解析

1. layout布局文件

(1)activity_category.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".view.activity.MainActivity">
   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/categoryList"
       android:background="@color/grgray"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
      />

   <FrameLayout
       android:layout_below="@+id/categoryList"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
      <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/productList"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
      <androidx.recyclerview.widget.RecyclerView
          android: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="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=".view.activity.MainActivity">

   <Button
       android: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="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:background="#99000000">

    <TextView
        android: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="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"
    >

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/icon"
        android:layout_alignParentTop="true"
        android:layout_width="100dp"
        app:placeholderImage="@mipmap/ic_launcher"
        android:layout_centerInParent="true"
        android:layout_height="100dp"/>

    <TextView
        android: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="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:background="#99111111">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/icon"
        android:layout_alignParentTop="true"
        android:layout_width="100dp"
        app:placeholderImage="@mipmap/ic_launcher"
        android:layout_centerInParent="true"
        android:layout_height="100dp"/>

    <TextView
        android: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= "http://mobile.bwstudent.com/small/";
    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;
    @Override
    public 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 {
    //view
    interface ICategoryView extends IBaseView {
        void getCategorySuccess(CategoryBean bean);
        void getProductListSuccess(ProductList productList);
    }

    //presenter
    interface ICategoryPresenter{
        void getCategory();
    }

    //model
    interface 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() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public 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(){
        @Override
        public 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() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //子线程
                String data = response.body().string();

                Log.e("myMessage", "=== " + data);
            }
        });
    }




    public class LogginInterceptor implements Interceptor{
        @Override
        public 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();
    }
    @Override
    public void getCategory() {
        httpUtils2.getData(Constant.BASE_URL + Constant.CATEGORY_LIST, new HttpUtils2.CallbackData() {
            @Override
            public 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() {
            @Override
            public 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;

    @Override
    protected 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;
    }

    @Override
    protected 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;

    @Override
    int setSelfContentView() {
        return R.layout.activity_category;
    }

    @Override
    void initView() {
        //一级分类
        recyclerView = findViewById(R.id.categoryList);
        categoryAdapter = new CategoryAdapter(this, new CategoryAdapter.ClickItemCallback() {
            @Override
            public 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() {
            @Override
            public 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);



    }

    @Override
    CategroyPresenter setSelfPresenter() {
        return new CategroyPresenter();
    }

    @Override
    void initData() {
        getPresenter().getCategory();
    }

    private void loadProductById(String categoryId) {
        getPresenter().getProductsById(categoryId,1);
    }

    @Override
    public void getCategorySuccess(CategoryBean bean) {
        resultBeans = bean.getResult();
        categoryAdapter.setData(resultBeans);
    }

    @Override
    public void getProductListSuccess(ProductList productList) {
        categoryProductAdapter.setData(productList.getResult());
    }
}

(3)ZxingActivity.java

public class ZxingActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zxing);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ZxingActivity.this, CaptureActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    @Override
    protected 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
    @Override
    public 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<>();
    @Override
    public 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() {
            @Override
            public 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);
    }

    @Override
    public 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
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_category_products, null);
        return new ViewHolder(view);
    }

    @Override
    public 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() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public 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
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_category_second, null);
        return new ViewHolder(view);
    }

    @Override
    public 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() {
            @Override
            public void onClick(View v) {
                clickItemCallback.clickItem(list.get(position).getId());
            }
        });
    }

    public interface ClickItemCallback{
        void clickItem(String categoryId);
    }

    @Override
    public 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="http://schemas.android.com/apk/res/android"
    package="cj.com.a1711amvpproject">

    <application
        android: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>
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服