mah.farmer.ui.ProductInfoActivity.java Source code

Java tutorial

Introduction

Here is the source code for mah.farmer.ui.ProductInfoActivity.java

Source

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package mah.farmer.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.baidu.locTest.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import cn.pedant.SweetAlert.SweetAlertDialog;
import mah.farmer.util.ImageLoadOptions;
import mah.farmer.bean.FarmerProduct;
import mah.farmer.http.FarmerConfig;

/**??
 * Created by  on 2016/4/28.
 */
public class ProductInfoActivity extends ActivityBase implements AppBarLayout.OnOffsetChangedListener {

    //?
    private static final int PERCENTAGE_TO_SHOW_IMAGE = 20;
    private View mFab;
    private int mMaxScrollSize;
    private boolean mIsImageHidden;
    private TextView danjia, xiaoliang, desc, supplier_name, supplier_location, product_leibie;
    private String supplierPhone;
    private ImageView supplier_avatar, produt_avatar;
    private FarmerProduct farmerProduct;
    private LinearLayout addshoppingcart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_info);
        initAppBarLayout();

    }

    /**
     * APPLayout,?
     */
    private void initAppBarLayout() {

        //????
        farmerProduct = (FarmerProduct) getIntent().getSerializableExtra("products");

        //ID
        produt_avatar = (ImageView) findViewById(R.id.productinfo_avatar);
        mFab = findViewById(R.id.flexible_example_fab);
        danjia = (TextView) findViewById(R.id.product_danjia);
        desc = (TextView) findViewById(R.id.productinfo_desc);
        xiaoliang = (TextView) findViewById(R.id.product_xiaoliang);
        supplier_name = (TextView) findViewById(R.id.supplier_name);
        supplier_location = (TextView) findViewById(R.id.supplier_location);
        supplier_avatar = (ImageView) findViewById(R.id.supplier_avatar);
        addshoppingcart = (LinearLayout) findViewById(R.id.add_shopping_cart);
        product_leibie = (TextView) findViewById(R.id.product_leibie);

        //??
        danjia.setText("?" + farmerProduct.getMyprice() + "/");
        desc.setText(farmerProduct.getDescription());
        if (farmerProduct.getSalesNum() == null || farmerProduct.getSalesNum().equals("")) {
            xiaoliang.setText("???");

        } else {
            xiaoliang.setText("?" + farmerProduct.getSalesNum() + "");
        }

        product_leibie
                .setText("" + farmerProduct.getTagOne() + "     ?" + farmerProduct.getTagTwo());
        supplier_location.setText("?" + farmerProduct.getSupplierAddress());
        supplier_name.setText(": " + farmerProduct.getSupplier());

        //?
        if (farmerProduct.getAvata_md5() != null && !farmerProduct.getAvata_md5().equals("")) {
            ImageLoader.getInstance().displayImage(farmerProduct.getAvata_md5(), produt_avatar,
                    ImageLoadOptions.getOptions());
        } else {
            produt_avatar.setImageResource(R.drawable.default_head);
        }

        //?
        getSupplierAvatar();

        //??
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //??
                Uri uri = Uri.parse("tel:" + farmerProduct.getSupplierPhone());
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(intent);

            }
        });

        //???
        addshoppingcart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //???
                if (FarmerConfig.getCachedFarmerNickname(ProductInfoActivity.this)
                        .equals(farmerProduct.getSupplier())) {
                    new SweetAlertDialog(ProductInfoActivity.this, SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("?").setContentText("??")
                            .show();
                } else {
                    Intent intent = new Intent(ProductInfoActivity.this, AddShoppingDeliveryActivity.class);
                    intent.putExtra("userTrueName",
                            FarmerConfig.getCachedFarmerPhoTruename(ProductInfoActivity.this));
                    intent.putExtra("userAddress", FarmerConfig.getCachedFarmerAddress(ProductInfoActivity.this));
                    intent.putExtra("userPhone", FarmerConfig.getCachedFarmerPhone(ProductInfoActivity.this));
                    intent.putExtra("product", farmerProduct);
                    startActivity(intent);
                }
            }
        });

        //toolbar?
        Toolbar toolbar = (Toolbar) findViewById(R.id.flexible_example_toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        AppBarLayout appbar = (AppBarLayout) findViewById(R.id.flexible_example_appbar);
        appbar.addOnOffsetChangedListener(this);

        /**
         * 
         */
        final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(
                R.id.flexible_example_collapsing);
        collapsingToolbarLayout.setTitle(farmerProduct.getName());
        collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.farmer_shop_list_back)); // transperent color = #00000000
        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.rgb(232, 247, 239));
    }

    /**
     * applayout?
     * @param appBarLayout
     * @param i
     */
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        if (mMaxScrollSize == 0)
            mMaxScrollSize = appBarLayout.getTotalScrollRange();

        int currentScrollPercentage = (Math.abs(i)) * 100 / mMaxScrollSize;

        //?toolbar???????
        if (currentScrollPercentage >= PERCENTAGE_TO_SHOW_IMAGE) {
            if (!mIsImageHidden) {
                mIsImageHidden = true;

                ViewCompat.animate(mFab).scaleY(0).scaleX(0).start();
            }
        }

        if (currentScrollPercentage < PERCENTAGE_TO_SHOW_IMAGE) {
            if (mIsImageHidden) {
                mIsImageHidden = false;
                ViewCompat.animate(mFab).scaleY(1).scaleX(1).start();
            }
        }
    }

    public static void start(Context c) {
        c.startActivity(new Intent(c, ProductInfoActivity.class));
    }

    /**
     * ???
     */
    public void getSupplierAvatar() {
        RequestQueue requestQueue = Volley.newRequestQueue(ProductInfoActivity.this);
        JSONObject json = new JSONObject();//json
        try {
            //?putjson
            json.put("userNickName", farmerProduct.getSupplier());
        } catch (Exception e) {

        }

        //
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                "http://192.168.235.22:8080/showPersonality/",

                json, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                        try {
                            /**
                             * ??
                             */
                            Toast.makeText(ProductInfoActivity.this, "myinfo" + jsonObject.getString("reason"),
                                    Toast.LENGTH_LONG).show();
                            JSONObject json = jsonObject.getJSONObject("data");
                            String avatar = json.getString("userImage");

                            //??
                            if (avatar != null && !avatar.equals("")) {
                                ImageLoader.getInstance().displayImage(avatar, supplier_avatar,
                                        ImageLoadOptions.getOptions());
                            } else {
                                supplier_avatar.setImageResource(R.drawable.default_default_head);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    /**
                     * ??
                     * @param volleyError
                     */
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        new SweetAlertDialog(ProductInfoActivity.this, SweetAlertDialog.ERROR_TYPE)
                                .setTitleText("").setContentText("???")
                                .show();

                    }
                });
        requestQueue.add(jsonObjectRequest);

    }
}