加头加尾
ce.gif加头加尾比较简单,不过我这里用了另一种方法,优化上可能不足,单总归是一种方法,大致逻辑:
总布局单拎一个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如下(去掉标题和根布局之后的):
@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如下(去掉标题和根布局之后的):
<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="合计:"
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+"件商品 小计:");
}
}
其中起作用的代码只有
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="共计1件商品 小计:"/>
<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列表中有多种的数据集
20170910150502071359b4cb293ef80.gif有时候会遇到一个列表中有多种itemviewType的情况,常用的方法就是根据数据集的position来判断 然后返回我们设置的类型,以上图购物车的内容为例,图中有三种类型的布局,其中中间的那种是我临时加上去的,逻辑:
主布局:recyclerView
主adapter:根据数据的类型分类 我这里分为了三类 正常商品 失效商品1 失效商品2 , 跳过正常商品 , 失效商品1 占有一个type 类似于上面的加尾 也直接跳过
失效商品2 占有两个type 分别是 失效商品的标题 和 失效商品的列表展示
ps:失效商品1对应图中商品的最后 失效商品2对应的是购物车列表的中间商品
正常商品 对应购物车商品的最上面的商品
失效商品1的布局:定长的listview和标题
失效商品2的布局同正常商品的布局一样
失效商品2的标题布局 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<>();
}
/**
* 清空数据
*/
public void setClearData(){
mGoodsList.clear();
mNormalGoods.clear();
mFailureGoods.clear();
mFailure2Goods.clear();
notifyDataSetChanged();
}
/**
* 添加数据集合
* @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();
}
/**
* 添加单条数据
* @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; // 商品数量添加减少
private TextView mItemGoodsCount; // 商品数量
private TextView mItemGoodsPriceRemainder; // 价格后缀
private TextView mItemGoodsDescribe; // 商品描述
private ImageView mItemGoodsImg; // 商品缩略图
public ShopCarViewHolder(View itemView) {
super(itemView);
}
}
}
ps:因为篇幅的原因 将一些不必要的逻辑代码去掉了 这些代码纯属项目中的逻辑处理,并不影响本篇文章
失效商品1和失效商品2的viewholder 都是一些布局的实例化或者是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
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
网友评论