1. 程式人生 > >矽谷商城第二版2--首頁模塊

矽谷商城第二版2--首頁模塊

horizon back parent move recycler else pri version mat

技術分享

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="@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--首頁模塊