商品

xml:

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <include layout="@layout/searchlayout"></include>
    <include layout="@layout/titlelayout"></include>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        >
    <com.example.library.AutoFlowLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/auto_layout"
        />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="历史分类"
            android:layout_below="@id/auto_layout"
            android:layout_marginTop="80dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/hostroy"
            />
        <com.example.moni.view.FlowLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/fram_layout"
            android:layout_below="@id/hostroy"
            >

        </com.example.moni.view.FlowLayout>
    </RelativeLayout>
</LinearLayout>

//头布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    >
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="最近搜索"
     android:textSize="20sp"
     android:layout_marginLeft="10dp"
     />
    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:id="@+id/delete"
        android:src="@mipmap/sc"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
    />
</RelativeLayout>

//搜索

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    >
<RelativeLayout
    android:layout_width="400dp"
    android:layout_height="35dp"
    android:orientation="horizontal"
    android:background="@drawable/search"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    >
<ImageView
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:src="@mipmap/search_icon2"
    android:layout_marginTop="6dp"
    android:layout_marginLeft="8dp"
    android:id="@+id/search"
    />
    <EditText
        android:layout_width="270dp"
        android:layout_height="30dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="3dp"
        android:id="@+id/edit_content"
        android:hint="在万千商品中海选"
        android:background="@null"
        android:textSize="20sp"
        />
</RelativeLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/diss"
        android:text="取消"
        android:textStyle="bold"
        android:textSize="20sp"
        android:layout_alignParentRight="true"
        android:layout_marginTop="10dp"
        android:layout_marginRight="15dp"
        />
</RelativeLayout>

//商品

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <include layout="@layout/shopingtitlelayout"></include>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pull_listview"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <View
            android:id="@+id/iview"
            android:layout_height="0.75dp"
            android:layout_width="match_parent"
            android:background="#333"
            android:layout_marginTop="45dp"/>
        <LinearLayout
            android:id="@+id/re"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="6dp"
            android:orientation="horizontal"
            android:layout_below="@id/iview">
            <Button
                android:id="@+id/tvZhonghe"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:text="综合"
                android:gravity="center"
                android:layout_weight="1"/>
            <Button
                android:id="@+id/tvXiaoliang"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:text="销量"
                android:gravity="center"
                android:layout_weight="1"/>
            <Button
                android:id="@+id/tvPrice"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:text="价格"
                android:gravity="center"
                android:layout_weight="1"/>
        </LinearLayout>
        <View
            android:id="@+id/view"
            android:layout_height="0.75dp"
            android:layout_width="match_parent"
            android:background="#333"
            android:layout_marginTop="5dp"
            android:layout_below="@id/re"/>
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler"
            />
    </LinearLayout>

</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>

//商品头部:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:id="@+id/back"
    android:src="@mipmap/leftjiantou"
    />
    <RelativeLayout
        android:layout_toRightOf="@id/back"
        android:layout_marginLeft="10dp"
        android:layout_width="400dp"
        android:layout_height="40dp"
        android:background="@drawable/search"
        >
     <ImageView
         android:layout_width="30dp"
         android:layout_height="30dp"
         android:layout_centerVertical="true"
         android:src="@mipmap/search_icon2"
         android:id="@+id/search"
         />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/title"
            android:layout_toRightOf="@id/search"
            android:background="@null"
            android:layout_centerVertical="true"
            android:id="@+id/shopping"
            />
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/diss"
            android:src="@mipmap/a_3"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/kind_liner"
        android:id="@+id/huan"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        />
</RelativeLayout>

//流失布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/auto_tv"
        android:layout_height="35dp"
        android:layout_width="wrap_content"
        android:background="@drawable/horizontal_background_one"
        android:text="豆豆娃"
        android:textSize="17dp"
        android:gravity="center_horizontal"
        android:layout_marginTop="3dp"
        android:paddingTop="4dp"
        android:layout_marginLeft="8dp"/>
</LinearLayout>

//商品展示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relate"
    >
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/ion"
    android:padding="10dp"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/phone_title"
        android:layout_toRightOf="@id/ion"
        android:layout_alignTop="@id/ion"
        android:textSize="20sp"
        android:layout_marginLeft="5dp"
        android:text="111"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ion"
        android:layout_below="@id/phone_title"
        android:id="@+id/price"
        android:textSize="20sp"
        android:textColor="#f00"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="22"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sale"
        android:text="333"
        android:layout_alignBottom="@id/ion"
        android:textSize="18sp"
        android:layout_toRightOf="@id/ion"
        android:layout_marginLeft="5dp"
        />
</RelativeLayout>

//详情:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/web_view"
        ></WebView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:background="#ffffff"
        android:layout_alignParentBottom="true"
        >
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@mipmap/f" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="供应商"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@mipmap/g" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="店铺"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@mipmap/l" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="爱好"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@mipmap/c" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="购物车"
                />
        </LinearLayout>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:background="#f00"
            android:text="加入购物车"
            android:textSize="25sp"
            android:textColor="#fff"
            android:gravity="center"
            />
    </LinearLayout>
</RelativeLayout>

Activity:

package com.example.moni;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.library.AutoFlowLayout;
import com.example.library.FlowAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private List<String> list;
    private ImageView search;
    private EditText edit_content;
    private TextView diss;
    private ImageView delete;
    private AutoFlowLayout auto_layout;
    private View view;
    private ViewGroup fram_layout;
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initview();
        String data[] = new String[]{
                "考拉三周年人气热销榜",
                "电动牙刷",
                "尤妮佳",
                "豆豆鞋",
                "沐浴露",
                "日东红茶",
                "榴莲"
        };
        for (int i = 0; i < 20; i++) {
            TextView textView = new TextView(this);
            textView.setText(data[i % data.length]);
            textView.setBackgroundResource(R.drawable.flow_item_bg);
            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);
            params.leftMargin = 20;
            params.topMargin = 20;
            params.rightMargin = 20;
            fram_layout.addView(textView, params);
        }
    }

    private void initview() {
        list = new ArrayList<>();
        search = findViewById(R.id.search);
        edit_content = findViewById(R.id.edit_content);
        diss = findViewById(R.id.diss);
        delete = findViewById(R.id.delete);
        auto_layout = findViewById(R.id.auto_layout);
        fram_layout = findViewById(R.id.fram_layout);
        search.setOnClickListener(this);
        diss.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
         switch (view.getId()){
             case R.id.search:
                 name = edit_content.getText().toString();
                 list.add(name);
                 auto();
                 break;
             case R.id.diss:
                 edit_content.getText().clear();
                 break;
             case R.id.delete:
                 edit_content.getText().clear();
                 auto_layout.removeAllViews();
                 break;
         }
    }

    private void auto() {
        auto_layout.setAdapter(new FlowAdapter(list) {
            @Override
            public View getView(int i) {
                if(list!=null){
                    view = View.inflate(MainActivity.this, R.layout.sjoplayout, null);
                    TextView auto_tv=view.findViewById(R.id.auto_tv);
                    name = list.get(i);
                    auto_tv.setText(name);
                    list.clear();
                    auto_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent=new Intent(MainActivity.this,ShoppingActivity.class);
                            intent.putExtra("title", name);
                            startActivity(intent);
                        }
                    });
                }
                return view;
            }
        });
    }
}

//商品

package com.example.moni;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.example.moni.adapter.PhoneAdapter;
import com.example.moni.bean.PhoneBean;
import com.example.moni.presenter.PhonePresenterimpl;
import com.example.moni.view.ShoppingView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.List;

public class ShoppingActivity extends AppCompatActivity implements View.OnClickListener,ShoppingView{

    private ImageView back;
    private ImageView search;
    private ImageView diss;
    private EditText shopping;
    private String title;
    private RecyclerView recycler;
    private PhonePresenterimpl phonePresenterimpl;
    private  int page=0;
    private int sort=0;
    private List<PhoneBean.DataBean> data;
    private PhoneAdapter adapter;
   private PullToRefreshScrollView pull_listview;
    private Button zonghe;
    private Button xiaoliang;
    private Button price;
    private ImageView huan;
    private boolean flag=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);
        //初始化控件
        initview();
        title = getIntent().getStringExtra("title");
        shopping.setText(title);
        back.setOnClickListener(this);
        diss.setOnClickListener(this);
        //设置排序的监听
        zonghe.setOnClickListener(this);
        price.setOnClickListener(this);
        xiaoliang.setOnClickListener(this);
        //设置转换的按钮
        huan.setOnClickListener(this);
        phonePresenterimpl = new PhonePresenterimpl(this);
        phonePresenterimpl.getData(page,sort);
        //设置布局管理器
        recycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        pull_listview.setMode(PullToRefreshBase.Mode.BOTH);

        //设置刷新
        pull_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page=1;
                phonePresenterimpl.getData(page,sort);
                pull_listview.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                phonePresenterimpl.getData(page,sort);
            }
        });
    }

    private void initview() {
        back = findViewById(R.id.back);
        search = findViewById(R.id.search);
        diss = findViewById(R.id.diss);
        shopping = findViewById(R.id.shopping);
        huan = findViewById(R.id.huan);
        recycler = findViewById(R.id.recycler);
        pull_listview = findViewById(R.id.pull_listview);
        zonghe = findViewById(R.id.tvZhonghe);
        xiaoliang = findViewById(R.id.tvXiaoliang);
        price = findViewById(R.id.tvPrice);
        huan = findViewById(R.id.huan);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.diss:
                shopping.getText().clear();
                break;
            case R.id.tvZhonghe:
                sort=0;
                phonePresenterimpl.getData(page,sort);
                break;
            case R.id.tvXiaoliang:
                sort=1;
                phonePresenterimpl.getData(page,sort);
                break;
            case R.id.tvPrice:
                sort=2;
                phonePresenterimpl.getData(page,sort);
                break;
            case R.id.huan:
                if(flag==false){
                    recycler.setLayoutManager(new GridLayoutManager(this,2));
                    flag=true;
                    huan.setImageResource(R.mipmap.kind_liner);
                }else if(flag==true){
                    recycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
                    flag=false;
                    huan.setImageResource(R.mipmap.kind_grid);
                }
                break;

        }
    }

    @Override
    public void onSuccess(PhoneBean phoneBean) {
        data = phoneBean.getData();
        if(data!=null){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter = new PhoneAdapter(ShoppingActivity.this,data);
                    recycler.setAdapter(adapter);
                    pull_listview.onRefreshComplete();
                    adapter.setCallback(new PhoneAdapter.onItemCilck() {
                        @Override
                        public void onItemClick(int position) {
                            Intent intent=new Intent(ShoppingActivity.this,XiangqingActivity.class);
                            intent.putExtra("url",data.get(position).getDetailUrl());
                            startActivity(intent);
                        }
                    });
                }
            });
        }
    }

    @Override
    public void onError(int code) {

    }
}

view:

//流失布局

package com.example.moni.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;


public class FlowLayout extends ViewGroup{

    private List<List<View>> mAllViews=new ArrayList<>();

    //记录每一行的行高,行数就是该集合的元素个数
    private List<Integer> mLineHeight=new ArrayList<>();

    public FlowLayout(Context context) {
        this(context,null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        //wrap_content时,需要自己计算宽高
        int width=0;
        int height=0;

        //记录每一行的宽度和高度
        int lineWidth=0;
        int lineHeight=0;

        //得到内部元素的个数
        int cCount=getChildCount();
        for (int i = 0; i < cCount; i++) {
            View child = getChildAt(i);
            //测量子view的宽和高
            measureChild(child,widthMeasureSpec,heightMeasureSpec);
            //得到layoutParams
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            //子view占据的宽度
            int childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            //子view占据的高度
            int childHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;

            //换行
            if (lineWidth+childWidth>sizeWidth-getPaddingLeft()-getPaddingRight())
            {
                //对比得到最大的宽度
                width= Math.max(width,lineWidth);
                //重置lineWidth
                lineWidth=childWidth;
                //记录行高
                height+=lineHeight;
                lineHeight=childHeight;
            }
            else
            {
                //叠加行宽
                lineWidth+=childWidth;
                //得到当前行最大的高度
                lineHeight=Math.max(lineHeight,childHeight);
            }
            //如果是最后一个控件
            if (i==cCount-1)
            {
                width=Math.max(width,lineWidth);
                height+=lineHeight;
            }
        }

        setMeasuredDimension(modeWidth==MeasureSpec.EXACTLY?sizeWidth:width+getPaddingLeft()+getPaddingRight(),
                modeHeight==MeasureSpec.EXACTLY?sizeHeight:height+getPaddingBottom()+getPaddingTop());
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //清除数据,因为会重复调用
        mAllViews.clear();
        mLineHeight.clear();

        //当前viewgroup的宽度
        int width=getWidth();

        int lineWidth=0;
        int lineHeight=0;

        List<View> lineViews=new ArrayList<>();
        int cCount=getChildCount();
        for (int i = 0; i < cCount; i++) {
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

            int childWidth=child.getMeasuredWidth();
            int childHeight=child.getMeasuredHeight();

            //如果需要换行
            if (childWidth+lp.leftMargin+lp.rightMargin+lineWidth>width)
            {
                //记录lineHeight
                mLineHeight.add(lineHeight);
                //记录当期的views
                mAllViews.add(lineViews);

                //重置我们的行宽和行高
                lineWidth=0;
                lineHeight=childHeight+lp.topMargin+lp.bottomMargin;
                //重置我们的view集合
                lineViews=new ArrayList<>();
            }
            lineWidth+=childWidth+lp.leftMargin+lp.rightMargin;
            lineHeight=Math.max(lineHeight,childHeight+lp.topMargin+lp.bottomMargin);
            lineViews.add(child);
        }
        //处理最后一行
        mLineHeight.add(lineHeight);
        mAllViews.add(lineViews);

        //设置子view的位置
        int left=getPaddingLeft();
        int top=getPaddingTop();

        //行数
        int lineNum=mAllViews.size();
        for (int i = 0; i < lineNum; i++) {
            lineViews=mAllViews.get(i);
            lineHeight = mLineHeight.get(i);

            for (int j = 0; j < lineViews.size(); j++) {
                View child = lineViews.get(j);
                //判断child的状态
                if (child.getVisibility()==GONE)
                {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

                int lc=left+lp.leftMargin;
                int tc=top+lp.topMargin;
                int rc=lc+child.getMeasuredWidth();
                int bc=tc+child.getMeasuredHeight();

                //为子view进行布局
                child.layout(lc,tc,rc,bc);
                left+=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            }
            left=getPaddingLeft();
            top+=lineHeight;
        }
    }

    //与当前viewgroup对应的layoutParams
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }
}

presneter:

package com.example.moni.presenter;


public interface PhonePresenter {

    //解绑
    void onDestroy();
}
package com.example.moni.presenter;

import com.example.moni.ShoppingActivity;
import com.example.moni.bean.PhoneBean;
import com.example.moni.model.PhoneModel;
import com.example.moni.model.PhoneModelImpl;
import com.example.moni.view.ShoppingView;



public class PhonePresenterimpl implements PhonePresenter {

    public ShoppingView shoppingView;
    private PhoneModelImpl phoneModel;

    public PhonePresenterimpl(ShoppingView shoppingView) {
        this.shoppingView=shoppingView;
        this.phoneModel = new PhoneModelImpl();
    }
    public void getData(int page,int sort){
        phoneModel.getData(new PhoneModel() {
            @Override
            public void onSucsess(PhoneBean phoneBean, int page, int sort) {
                shoppingView.onSuccess(phoneBean);
            }

            @Override
            public void onError(int code) {
                 shoppingView.onError(1);
            }
        },page,sort);
    }
    //解绑
    @Override
    public void onDestroy() {
        if(shoppingView!=null){
            shoppingView=null;
        }
    }
}

Model:

package com.example.moni.model;

import com.example.moni.bean.PhoneBean;


public interface PhoneModel {

    //成功的方法
    void onSucsess(PhoneBean phoneBean,int page,int sort);

    //失败的方法
    void  onError(int code);
}
package com.example.moni.model;

import android.util.Log;

import com.example.moni.bean.PhoneBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;



public class PhoneModelImpl {

    public void getData(final PhoneModel phoneModel, final int page, final int sort){
        String url="http://www.zhaoapi.cn/product/searchProducts?keywords=%E6%89%8B%E6%9C%BA&page="+page+"&sort="+sort+"";
        //创建okhttp对象
        OkHttpClient okHttpClient=new OkHttpClient
                .Builder()
                .build();
        //创建request对象
        Request request = new Request
                .Builder()
                .url(url)
                .build();
        //执行
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                phoneModel.onError(1);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.i("aaa",str.toString());
                Gson gson=new Gson();
                PhoneBean phoneBean = gson.fromJson(str, PhoneBean.class);
                //调用成功的方法
                phoneModel.onSucsess(phoneBean,page,sort);
            }
        });
    }
}

//适配器:

package com.example.moni.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.moni.R;
import com.example.moni.bean.PhoneBean;

import java.util.List;



public class PhoneAdapter extends RecyclerView.Adapter<PhoneAdapter.ViewHolder>{
    private Context context;
    private List<PhoneBean.DataBean> dataBeanList;
    private View view;

    public PhoneAdapter(Context context, List<PhoneBean.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @NonNull
    @Override
    public PhoneAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.phonelist, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PhoneAdapter.ViewHolder holder, final int position) {
       holder.price.setText("¥"+dataBeanList.get(position).getPrice());
       holder.sale.setText(dataBeanList.get(position).getSalenum()+"销量");
       holder.title.setText(dataBeanList.get(position).getTitle());
       String path = dataBeanList.get(position).getImages().split("\\|")[0];
       Glide.with(context).load(path).into(holder.ion);
       if(callback!=null){
           holder.relate.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                callback.onItemClick(position);
               }
           });
       }
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private  ImageView ion;
        private TextView title;
        private  TextView price;
        private TextView sale;
        private  RelativeLayout relate;

        public ViewHolder(View itemView) {
            super(itemView);
            ion = itemView.findViewById(R.id.ion);
            title = itemView.findViewById(R.id.phone_title);
            price = itemView.findViewById(R.id.price);
            sale = itemView.findViewById(R.id.sale);
            relate = itemView.findViewById(R.id.relate);
        }
    }
    public interface onItemCilck{
        void onItemClick(int position);
    }
    public onItemCilck callback;

    public void setCallback(onItemCilck callback) {
        this.callback = callback;
    }
}

猜你喜欢

转载自blog.csdn.net/MessageFang/article/details/81259178
今日推荐