加頭加尾
加頭加尾比較簡單,不過我這里用了另一種方法,優(yōu)化上可能不足,單總歸是一種方法丝里,大致邏輯:
總布局單拎一個recyclerView,itemviewType 為2,0為頭部(上圖中的收貨地址)体谒,1為商品信息和底部杯聚,其中type=1時的viewholder中的布局包含有一個固定高度的listview和一個底部(具體展示為評論),然后再為listview做適配抒痒。
總adapter代碼如下:
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_HEAD) return new CreateOrderGoodsHeadViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_createorder_recycler_head_layout,parent,false));
else return new CreateOrderGoodsViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_create_order_goods_list_layout,parent,false),mContext);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
if(getItemViewType(position) == TYPE_LIST){
CreateOrderGoodsViewHolder mHolder = (CreateOrderGoodsViewHolder) holder;
mHolder.initData(mGoodsList);
mHolder.mLeaveMsg.addTextChangedListener(new EditChangedListener(position - 1));
}else if(getItemViewType(position) == TYPE_HEAD){
CreateOrderGoodsHeadViewHolder mHolder = (CreateOrderGoodsHeadViewHolder) holder;
mHolder.mAdrView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, MyDeliveryAdrActivity.class);
intent.putExtra("intentType","createOrder");
mContext.startActivityForResult(intent,10);
}
});
}
}
@Override
public int getItemCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(position == 0) return TYPE_HEAD;
else return TYPE_LIST;
}
總布局xml如下(去掉標(biāo)題和根布局之后的):
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_HEAD) return new CreateOrderGoodsHeadViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_createorder_recycler_head_layout,parent,false));
else return new CreateOrderGoodsViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_create_order_goods_list_layout,parent,false),mContext);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
if(getItemViewType(position) == TYPE_LIST){
CreateOrderGoodsViewHolder mHolder = (CreateOrderGoodsViewHolder) holder;
mHolder.initData(mGoodsList);
mHolder.mLeaveMsg.addTextChangedListener(new EditChangedListener(position - 1));
}else if(getItemViewType(position) == TYPE_HEAD){
CreateOrderGoodsHeadViewHolder mHolder = (CreateOrderGoodsHeadViewHolder) holder;
mHolder.mAdrView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, MyDeliveryAdrActivity.class);
intent.putExtra("intentType","createOrder");
mContext.startActivityForResult(intent,10);
}
});
}
}
@Override
public int getItemCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(position == 0) return TYPE_HEAD;
else return TYPE_LIST;
}
總布局xml如下(去掉標(biāo)題和根布局之后的):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingBottom="@dimen/w_dp_55"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/shopcar_createOrder_Goods_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/shopcar_item_divider"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_alignTop="@+id/toCreateOrder_submit_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/w_dp_1"
android:background="@color/shopcar_edit_layout_divider"/>
<RelativeLayout
android:id="@+id/toCreateOrder_submit_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/w_dp_55"
android:background="@color/bg_white">
<TextView
android:id="@+id/shopcar_submit_order"
android:layout_width="@dimen/w_dp_120"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/appRed"
android:gravity="center"
android:text="提交訂單"
android:textColor="@color/white"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/shopcar_createOrder_money"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/w_dp_20"
android:layout_toLeftOf="@+id/shopcar_submit_order"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/shopcar_createOrder_AllPrice_priceFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥6999."
android:textColor="@color/appRed"
android:textSize="17sp" />
<TextView
android:id="@+id/shopcar_createOrder_AllPrice_priceTail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textColor="@color/appRed"
android:textSize="14sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/shopcar_createOrder_money"
android:gravity="center_vertical"
android:text="合計(jì):"
android:textColor="@color/black"
android:textSize="17sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
商品列的viewholder代碼如下:
public class CreateOrderGoodsViewHolder extends BaseViewHolder {
public NoScrollListView mListView;
public EditText mLeaveMsg;
private ShopCarOrderGoodsAdapter mAdapter;
private Context mContext;
private List dbGoodsCartModels;
public TextView mPrice_GoodsCount;
public TextView mPrice_GoodsPrice;
public CreateOrderGoodsViewHolder(View itemView, Context context) {
super(itemView);
mContext = context;
mListView = (NoScrollListView) itemView.findViewById(R.id.shopcar_order_goods_info_lv);
mLeaveMsg = (EditText) itemView.findViewById(R.id.shopcar_createrOrder_lea_msg);
mPrice_GoodsCount = (TextView) itemView.findViewById(R.id.shopcar_createOrder_Goods_price_count);
mPrice_GoodsPrice = (TextView) itemView.findViewById(R.id.shopcar_createOrder_Goods_price_allCount);
mAdapter = new ShopCarOrderGoodsAdapter(mContext);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
if (dbGoodsCartModels != null) {
Intent intent = new Intent();
intent.setClass(mContext, ProductInfoActivity.class);
intent.putExtra(ProductInfoActivity.INTENT_PRODUCTID, dbGoodsCartModels.get(position).getGoodsId()+"");
mContext.startActivity(intent);
}
}
});
}
public void initData(List dbGoodsCartModels) {
if (dbGoodsCartModels == null || dbGoodsCartModels.size() < 1) {
return;
}
this.dbGoodsCartModels = dbGoodsCartModels;
mListView.setAdapter(mAdapter);
mAdapter.setData(dbGoodsCartModels);
double allPrice = 0 ;
int allNumGoods = 0 ;
String strAllPrice = null;
BigDecimal one = new BigDecimal("1");
DecimalFormat df = new DecimalFormat("######0.00");
for(DbGoodsCartModel dbGoodsCartModel : dbGoodsCartModels){
allNumGoods += dbGoodsCartModel.getGoogdsnum();
BigDecimal a1 = new BigDecimal(Double.toString(dbGoodsCartModel.getGoodsprice()));
BigDecimal a2 = new BigDecimal(Double.toString(dbGoodsCartModel.getGoogdsnum()));
BigDecimal result = a1.multiply(a2);
allPrice += result.divide(one,2,BigDecimal.ROUND_HALF_UP).doubleValue();
}
strAllPrice = df.format(allPrice);
mPrice_GoodsPrice.setText("¥"+strAllPrice);
mPrice_GoodsCount.setText("共"+allNumGoods+"件商品 小計(jì):");
}
}
其中起作用的代碼只有
mListView.setAdapter(mAdapter);
mAdapter.setData(dbGoodsCartModels);
再來看此viewholder的xml文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.feihong.txgw.view.NoScrollListView
android:id="@+id/shopcar_order_goods_info_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_white" />
<LinearLayout
android:background="@color/bg_white"
android:paddingRight="@dimen/w_dp_13"
android:paddingLeft="@dimen/w_dp_13"
android:paddingTop="@dimen/w_dp_5"
android:layout_width="match_parent"
android:layout_height="@dimen/w_dp_83"
android:visibility="visible"
android:orientation="horizontal">
<TextView
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="買家留言:"/>
<EditText
android:gravity="top|left"
android:maxLines="3"
android:padding="5dp"
android:layout_marginLeft="@dimen/w_dp_3"
android:id="@+id/shopcar_createrOrder_lea_msg"
android:textSize="14sp"
android:background="@null"
android:textColorHint="#cfcdcd"
android:hint="對本次交易的說明"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/w_dp_1"
android:background="@color/shopcar_edit_layout_divider"/>
<RelativeLayout
android:background="@color/bg_white"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="@dimen/w_dp_45">
<TextView
android:layout_toLeftOf="@+id/shopcar_createOrder_Goods_price_allCount"
android:id="@+id/shopcar_createOrder_Goods_price_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="共計(jì)1件商品 小計(jì):"/>
<TextView
android:layout_alignParentRight="true"
android:id="@+id/shopcar_createOrder_Goods_price_allCount"
android:textColor="@color/appRed"
android:text="¥6999.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
一個自定義的固定listview加評論底部幌绍,簡單的加頭加尾到此完成
recycleView列表中有多種的數(shù)據(jù)集
有時候會遇到一個列表中有多種itemviewType的情況,常用的方法就是根據(jù)數(shù)據(jù)集的position來判斷 然后返回我們設(shè)置的類型故响,以上圖購物車的內(nèi)容為例傀广,圖中有三種類型的布局,其中中間的那種是我臨時加上去的彩届,邏輯:
主布局:recyclerView
主adapter:根據(jù)數(shù)據(jù)的類型分類 我這里分為了三類 正常商品 失效商品1 失效商品2 伪冰, 跳過正常商品 , 失效商品1 占有一個type 類似于上面的加尾 也直接跳過
失效商品2 占有兩個type 分別是 失效商品的標(biāo)題 和 失效商品的列表展示
ps:失效商品1對應(yīng)圖中商品的最后 失效商品2對應(yīng)的是購物車列表的中間商品
正常商品 對應(yīng)購物車商品的最上面的商品
失效商品1的布局:定長的listview和標(biāo)題
失效商品2的布局同正常商品的布局一樣
失效商品2的標(biāo)題布局 hhh~~
代碼如下:
主布局:
主adapter:
package com.feihong.txgw.shopcar.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.activity.base.BaseActivity;
import com.feihong.txgw.activity.product.ProductInfoActivity;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.db.DbGoodsCartModel;
import com.feihong.txgw.shopcar.listener.GoodsCountChangeListener;
import com.feihong.txgw.shopcar.listener.GoodsSelectStatusListener;
import com.feihong.txgw.shopcar.listener.OnDelFailureGoodsListener;
import com.feihong.txgw.shopcar.viewholder.ShopCarFail2DevieViewHolder;
import com.feihong.txgw.shopcar.viewholder.ShopcarFailure2ViewHolder;
import com.feihong.txgw.shopcar.viewholder.ShopcarFailureGoodsShowViewHolder;
import com.feihong.txgw.shopcar.viewholder.ShopcarFailureGoodsViewHolder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
* @name txgw_app
* @class name:com.shopcar.adapter
* @class describe: 購物車適配器
* @time 2017/6/29 13:23
* @change
* @chang time
* @class describe
*/
public class ShopCarAdapter extends RecyclerView.Adapter {
private BaseActivity mContext;
private ArrayList mGoodsList;
private GoodsSelectStatusListener mGoodsSelectStatusListener;
private ArrayList mFailureGoods;
private ArrayList mNormalGoods;
private ArrayList mFailure2Goods;
private final int FAILURE_GOODS = 0;
private final int NORMAL_GOODS = 1;
private final int FAILURE_2_GOODS = 3;
private final int FAILURE_2_GOODS_DEVIDE = 4;
public ShopCarAdapter(Context context){
mContext = (BaseActivity) context;
mGoodsList = new ArrayList<>();
mFailureGoods = new ArrayList<>();
mNormalGoods = new ArrayList<>();
mFailure2Goods = new ArrayList<>();
}
/**
* 清空數(shù)據(jù)
*/
public void setClearData(){
mGoodsList.clear();
mNormalGoods.clear();
mFailureGoods.clear();
mFailure2Goods.clear();
notifyDataSetChanged();
}
/**
* 添加數(shù)據(jù)集合
* @param goodsList
*/
public void setGoodsList(List goodsList){
mGoodsList.addAll(goodsList);
Log.i("wangzi", "adapter setGoodslist: "+mGoodsList.size()+" " +goodsList.size());
for(DbGoodsCartModel dbGoodsCartModel : goodsList){
if(dbGoodsCartModel.getGoodsStatus() == 0)
mFailureGoods.add(dbGoodsCartModel);
else if(dbGoodsCartModel.getGoodsStatus() == -1)
mFailure2Goods.add(dbGoodsCartModel);
else
mNormalGoods.add(dbGoodsCartModel);
}
notifyDataSetChanged();
}
/**
* 添加單條數(shù)據(jù)
* @param goods
*/
public void setGoods(DbGoodsCartModel goods){
mGoodsList.add(goods);
//mGoodsList
notifyDataSetChanged();
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == NORMAL_GOODS)
return new ShopCarViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_item_layout,parent,false));
else if (viewType == FAILURE_GOODS)
return new ShopcarFailureGoodsShowViewHolder(mContext,LayoutInflater.from(mContext).inflate(R.layout.shop_car_fail_lv_layout,parent,false));
else if(viewType == FAILURE_2_GOODS_DEVIDE)
return new ShopCarFail2DevieViewHolder(mContext,LayoutInflater.from(mContext).inflate(R.layout.shopcar_failure_2_devide_layout,parent,false));
else if(viewType == FAILURE_2_GOODS)
return new ShopcarFailure2ViewHolder(mContext,LayoutInflater.from(mContext).inflate(R.layout.shopcar_item_layout,parent,false));
return null;
}
@Override
public void onBindViewHolder(final BaseViewHolder holder, final int position) {
if(getItemViewType(position) == NORMAL_GOODS){
}else if(getItemViewType(position) == FAILURE_GOODS){
Log.i("wangzi", "過期商品: "+mFailureGoods.size());
ShopcarFailureGoodsShowViewHolder mHolder = (ShopcarFailureGoodsShowViewHolder) holder;
mHolder.initData(mFailureGoods,mDelFailureGoodsListener);
}
}
public static final String INTENT_PRODUCTID = "productID";
@Override
public int getItemCount() {
// return mGoodsList.size();
return mNormalGoods.size() + 1 + mFailure2Goods.size() + 1;
}
@Override
public int getItemViewType(int position) {
if(position < mNormalGoods.size())
return NORMAL_GOODS;
else if(position == mNormalGoods.size())
return FAILURE_2_GOODS_DEVIDE;
else if(mNormalGoods.size() < position && position < mFailure2Goods.size() + mNormalGoods.size() + 1)
return FAILURE_2_GOODS;
else if(position >= mFailure2Goods.size() + mNormalGoods.size() + 1)
return FAILURE_GOODS;
return 0;
}
protected class ShopCarViewHolder extends BaseViewHolder {
private ImageView mItemGoodsSelect;
private TextView mItemGoodsName,mItemGoodsPrice; // 商品名稱 價格
private TextView mItemGoodsCountAdd,mItemGoodsCountSubtract; // 商品數(shù)量添加減少
private TextView mItemGoodsCount; // 商品數(shù)量
private TextView mItemGoodsPriceRemainder; // 價格后綴
private TextView mItemGoodsDescribe; // 商品描述
private ImageView mItemGoodsImg; // 商品縮略圖
public ShopCarViewHolder(View itemView) {
super(itemView);
}
}
}
ps:因?yàn)槠脑?將一些不必要的邏輯代碼去掉了 這些代碼純屬項(xiàng)目中的邏輯處理樟蠕,并不影響本篇文章
失效商品1和失效商品2的viewholder 都是一些布局的實(shí)例化或者是listview的適配 所以這里也就不貼出來了
最后貼上定長的listview
public class NoScrollListView extends ListView {
public NoScrollListView(Context context) {
super(context);
}
public NoScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NoScrollListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
/**
* 重寫該方法贮聂,達(dá)到使ListView適應(yīng)ScrollView的效果
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}