首页 > 代码库 > 硅谷商城第二版2--首页模块

硅谷商城第二版2--首页模块

技术分享

1.fragment_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".home.fragment.HomeFragment">

    <include
        android:id="@+id/titlebar"
        layout="@layout/titlebar" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/titlebar" />

    <ImageButton
        android:id="@+id/ib_top"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/top_btn"
        android:visibility="gone" />
</RelativeLayout>

2.RecyclerView中六种item类型:

  横幅广告  频道 活动 秒杀 推荐 热卖

3.横幅广告 

  Banner实现轮播

4.秒杀

HomeRecyclerViewAdapter中

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    .....
    } else if (viewType == SECKILL) {
        return new SeckillViewHolder(mLayoutInflater.inflate(R.layout.seckill_item, null), mContext);
    }
}

seckill_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="380dp"
    android:layout_height="180dp"
    android:background="#fff"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://www.mamicode.com/@drawable/home_arrow_left_flash" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="今日闪购 距·结束"
            android:textColor="#000" />

        <TextView
            android:id="@+id/tv_time_seckill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/time_shape"
            android:padding="2dp"
            android:text="00:00:00"
            android:textColor="#fff" />

        <TextView
            android:id="@+id/tv_more_seckill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="5dp"
            android:drawableRight="@drawable/home_arrow_right"
            android:gravity="end"
            android:text="查看更多" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_seckill"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

  

SeckillViewHolder  

class SeckillViewHolder extends RecyclerView.ViewHolder {
    private TextView tvMore;
    private RecyclerView recyclerView;
    public Context mContext;

    public SeckillViewHolder(View itemView, Context mContext) {
        super(itemView);
        tvTime = (TextView) itemView.findViewById(R.id.tv_time_seckill);
        tvMore = (TextView) itemView.findViewById(R.id.tv_more_seckill);
        recyclerView = (RecyclerView) itemView.findViewById(R.id.rv_seckill);
        this.mContext = mContext;
    }


    public void setData(final ResultBean.SeckillInfoBean data) {
        //设置时间
        if (isFirst) {
//                dt = (int) (Integer.parseInt(data.getEnd_time()) - System.currentTimeMillis());
            dt = (int) (Integer.parseInt(data.getEnd_time()) - (Integer.parseInt(data.getStart_time())));
            isFirst = false;
        }

        //设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        SeckillRecyclerViewAdapter adapter = new SeckillRecyclerViewAdapter(mContext, data);
        recyclerView.setAdapter(adapter);

        //倒计时
        handler.sendEmptyMessageDelayed(0, 1000);

        //点击事件
        adapter.setOnSeckillRecyclerView(new SeckillRecyclerViewAdapter.OnSeckillRecyclerView() {
            @Override
            public void onClick(int position) {
                ResultBean.SeckillInfoBean.ListBean listBean = data.getList().get(position);
                String name = listBean.getName();
                String cover_price = listBean.getCover_price();
                String figure = listBean.getFigure();
                String product_id = listBean.getProduct_id();
                GoodsBean goodsBean = new GoodsBean(name, cover_price, figure, product_id);
//
                Intent intent = new Intent(mContext, GoodsInfoActivity.class);
                intent.putExtra(GOODS_BEAN, goodsBean);
                mContext.startActivity(intent);

                // Toast.makeText(mContext, "position:" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }
}

  

private boolean isFirst = true;
    private TextView tvTime;
    private int dt;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                dt = dt - 1000;
                SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
                tvTime.setText(sd.format(new Date(dt)));

                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0, 1000);
                if (dt == 0) {
                    handler.removeMessages(0);
                }
            }

        }
    };

采用CountdownView实现秒杀倒计时的效果

 

 

github:https://github.com/ganchuanpu/Shopping

 

硅谷商城第二版2--首页模块