1.搜索商品适配器
public class SearchAdapter extends RecyclerView.Adapter {
List<SearchBean.ResultBean> result=new ArrayList<>();
Context context;
private ViewHolder holder1;
public SearchAdapter(Context context) {
this.context = context;
}
public void getDate( List<SearchBean.ResultBean> result) {
if (result.size()>0){
this.result.addAll(result);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.child_item, null);
holder1 = new ViewHolder(inflate);
return holder1;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
holder1= (ViewHolder) holder;
Uri parse= Uri.parse(result.get(position).getMasterPic());
holder1.imageView.setImageURI(parse);
holder1.textView.setText(result.get(position).getCommodityName()+"");
holder1.textView2.setText("¥"+result.get(position).getPrice());
holder1.textView3.setText("已售"+result.get(position).getSaleNum());
}
@Override
public int getItemCount() {
return result.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private final SimpleDraweeView imageView;
private final TextView textView;
private final TextView textView2;
private final TextView textView3;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.item_img);
textView = itemView.findViewById(R.id.item_name1);
textView2 = itemView.findViewById(R.id.item_name2);
textView3 = itemView.findViewById(R.id.item_name3);
}
}
}
////child_item
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width=“150dp”
android:layout_height=“150dp”
android:id="@+id/item_img"
android:layout_gravity=“center”
/>
3.自定义MyView
public class MyView extends ViewGroup {
public MyView(Context context) {
this(context,null);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i <getChildCount() ; i++) {
View childAt = getChildAt(i);
measureChild(childAt,widthMeasureSpec,heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0; i <getChildCount() ; i++) {
View childAt = getChildAt(i);
int width = childAt.getMeasuredWidth();
int height = childAt.getMeasuredHeight();
if (i0){
childAt.layout(0,0,width,height);
}
if (i1){
childAt.layout(30,10,width+30,height+10);
}
if (i==2){
childAt.layout(70,0,width+70,height);
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
4.历史记录
public class SearchHistroyView extends ViewGroup {
private int sizeScreenWidth;
public SearchHistroyView(Context context) {
this(context,null);
}
public SearchHistroyView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SearchHistroyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
int temp = 100;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
sizeScreenWidth = MeasureSpec.getSize(widthMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left = 0;
int top = 0;
int marginHorizontal = 20;
for (int i = 0; i < getChildCount(); i++) {
View childAt = getChildAt(i);
int measuredWidth = childAt.getMeasuredWidth();
int measuredHeight = childAt.getMeasuredHeight();
if (left + measuredWidth >= sizeScreenWidth) {
top += measuredHeight;
left = 0;
}
childAt.layout(left,top,left + measuredWidth, top + measuredHeight);
left += measuredWidth + marginHorizontal; } }
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}