Android收藏商品/关注商品

Android收藏商品/关注商品

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by xingkong on 2016/01/06.
 */
public class UserCollectGoods extends Activity implements View.OnClickListener {
    private ImageView iv_back_son;
    //获取服务器端收藏数据,这里模拟测试数据
    private List listViewHot = new ArrayList();
    private AdapterListView adapter_listView_hot;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_collect_goods_xml);
        ((TextView) findViewById(R.id.tv_top_title)).setText("收藏的商品");
        iv_back_son = (ImageView) findViewById(R.id.iv_back_son);
        iv_back_son.setOnClickListener(this);
        initView();
    }

    private void initView() {
        //模拟收藏商品信息,暂时使用
        listViewHot.add(new CollectGoods(
                "http://www.apkbus.com/static/image/click/shangbuqi.gif",
                "商品名称1", "¥134.0", "2016-01-11"
        ));
        listViewHot.add(new CollectGoods(
                "http://www.apkbus.com/static/image/click/shangbuqi.gif",
                "商品名称2", "¥134.0", "2016-01-11"
        ));
        adapter_listView_hot = new AdapterListView(this, listViewHot);
        listView = (ListView) findViewById(R.id.my_mylistview_hot);
        listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        listView.setAdapter(adapter_listView_hot);
        //收藏商品单击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
                //跳转到商品详情界面(注意没有商品详情的请使用其他Activity)
                Intent intent = new Intent(UserCollectGoods.this, BabyActivity.class);
                startActivity(intent);
            }
        });
        //收藏商品长按事件
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
                listView.showContextMenu();
                return true;
            }
        });
        //创建弹出菜单
        listView.setOnCreateContextMenuListener(new AdapterView.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.add(0,1,0,"加入购物车").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        //加入购物车
                        Log.e("===","加入购物车");
                        return false;
                    }
                });
                menu.add(0,2,0,"取消收藏").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        //取消收藏
                        Log.e("===","取消收藏");
                        return false;
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_son:
                UserCollectGoods.this.finish();
                break;
        }
    }
}

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created by xingkong on 2016/01/11.
 */
public class AdapterListView extends BaseAdapter {
    private Context context;
    private List list;

    public AdapterListView(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.user_collect_goods_list, null);
            viewHolder.picture = (ImageView) convertView.findViewById(R.id.iv_adapter_collectgoods_list_picture);
            viewHolder.name = (TextView) convertView.findViewById(R.id.iv_adapter_collectgoods_list_name);
            viewHolder.price = (TextView) convertView.findViewById(R.id.iv_adapter_collectgoods_list_price);
            viewHolder.date = (TextView) convertView.findViewById(R.id.iv_adapter_collectgoods_list_date);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final String picture = list.get(position).getPicture();
        final Bundle data = new Bundle();
        new Thread() {
            @Override
            public void run() {
                data.putParcelable("picture", getHttpBitmap(picture));
                data.putBoolean("is", true);
            }
        }.start();
        while (data.getBoolean("is") == false) {
            try {
                //这里的线程延时问题,需要测试来缩短最佳时间
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        viewHolder.picture.setImageBitmap((Bitmap) data.getParcelable("picture"));
        viewHolder.name.setText(list.get(position).getName());
        viewHolder.price.setText(list.get(position).getPrice());
        viewHolder.date.setText(list.get(position).getDate());
        return convertView;
    }

    /**
     * 从服务器取图片
     */
    public static Bitmap getHttpBitmap(String url) {
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setConnectTimeout(0);
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    class ViewHolder {
        ImageView picture;
        TextView name;
        TextView price;
        TextView date;
    }
}


/**
 * Created by xingkong on 2016/01/11.
 */
public class CollectGoods {

    private String picture;
    private String name;
    private String price;
    private String date;

    public CollectGoods(String picture,String name,String price,String date) {
        super();
        this.picture=picture;
        this.name=name;
        this.price=price;
        this.date=date;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * Created by xingkong on 2016/01/11.
 */
public class MyListView extends ListView{
    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

user_collect_goods_xml.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/white"
              android:orientation="vertical">
    "@layout/top_menu_back_son"/>
    "match_parent"
        android:layout_height="1dp"
        android:background="@color/tv_Gray"/>
    "@+id/my_mylistview_hot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="5dp"
        android:numColumns="3"
        android:verticalSpacing="5dp">
    


user_collect_goods_list.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/tv_White"
              android:orientation="vertical">
        "match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="10dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:layout_marginTop="10dp">
            "@+id/iv_adapter_collectgoods_list_picture"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:src="@mipmap/adapter_grid_hot_home_menu1"/>
            "wrap_content"
                          android:layout_height="match_parent"
                          android:layout_marginBottom="10dp"
                          android:layout_marginLeft="10dp"
                          android:layout_marginRight="10dp"
                          android:layout_marginTop="10dp"
                          android:orientation="vertical">
                "@+id/iv_adapter_collectgoods_list_name"
                          android:layout_width="wrap_content"
                          android:layout_height="0dp"
                          android:layout_weight="1"
                          android:text="商品名称"
                          android:textColor="@color/black"/>
                "@+id/iv_adapter_collectgoods_list_price"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginTop="10dp"
                          android:text="¥440.00"
                          android:textColor="@color/red"/>
                "@+id/iv_adapter_collectgoods_list_date"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginTop="5dp"
                          android:text="收藏日期:2016-01-11"/>
            
        

Android收藏商品/关注商品_第1张图片

你可能感兴趣的:(android)