登录 注册 流式布局 搜索商品

main1布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.bwie.z.a0106test.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#cdcdcd"
        android:text="登录"
        android:gravity="center"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入手机号"
        android:id="@+id/tel"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/pwd"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:onClick="login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登录"/>
        <Button
            android:onClick="regist"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="注册"/>
    </LinearLayout>
</LinearLayout>

main2布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.z.a0106test.Main2Activity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#cdcdcd"
        android:text="注册"
        android:gravity="center"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入手机号"
        android:id="@+id/tel"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/pwd"/>
    <Button
        android:onClick="regist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="立即注册"/>
</LinearLayout>

main3布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:background="#fff"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.z.a0106test.Main3Activity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/et"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="9"
            android:gravity="center"/>
        <Button
            android:onClick="sousuo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="搜索"
            android:textColor="#929197"
            android:background="#fff"/>
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="热搜"/>
    <com.bwie.z.a0106test.ZFlowLayout
        android:id="@+id/mlayout"
        android:layout_width="match_parent"
        android:layout_height="500dp">
    </com.bwie.z.a0106test.ZFlowLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="历史记录" />
        <ListView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="8"
            android:id="@+id/lv">
        </ListView>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="删除"
            android:onClick="sc" />
    </LinearLayout>
</LinearLayout>

main4布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.z.a0106test.Main4Activity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="9"
            android:text="搜索商品"
            android:textSize="25dp"
            android:gravity="center"/>
        <CheckBox
            android:id="@+id/huan"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:checked="true"
            android:background="@drawable/buju_selector"
            android:layout_marginRight="20dp"
            android:button="@null"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/kuang"
            android:layout_width="0dp"
            android:layout_weight="8"
            android:layout_marginLeft="10dp"
            android:layout_height="wrap_content"
            android:hint="请输入关键词"/>
        <Button
            android:id="@+id/sou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索"/>
    </LinearLayout>
    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.jcodecraeer.xrecyclerview.XRecyclerView>

</LinearLayout>

 表格布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/item_img"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <TextView
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="title"
        android:textSize="16sp" />



    <TextView
        android:id="@+id/item_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/item_title"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/item_bargainPrice"
        android:text="售价"
        android:layout_marginTop="10dp"
        android:textColor="#f00"
        android:textSize="14sp" />
</LinearLayout>

list布局

<?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="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/item_img"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_toRightOf="@+id/item_img">

        <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="title"
            android:textSize="16sp" />



        <TextView
            android:id="@+id/item_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/item_title"
            android:layout_marginLeft="10dp"
            android:text="售价"
            android:layout_marginTop="10dp"
            android:textColor="#f00"
            android:textSize="14sp"

            />

    </RelativeLayout>

</LinearLayout>

流式子布局

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv" />
</LinearLayout>

布局切换的选择器

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/lv_icon"></item>
    <item android:state_checked="false" android:drawable="@drawable/grid_icon"></item>
</selector>

lable_item_bg_normal

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp"
        android:color="#000000" />
</shape>

MainActivity


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

import com.bwie.z.a0106test.bean.LoginBean;
import com.bwie.z.a0106test.bean.RegistBean;
import com.bwie.z.a0106test.presenter.DPresenter;
import com.bwie.z.a0106test.inter.IDActicity;

public class MainActivity extends AppCompatActivity implements IDActicity{

    private EditText tel;
    private EditText pwd;
    private DPresenter dPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tel = (EditText) findViewById(R.id.tel);
        pwd = (EditText) findViewById(R.id.pwd);
        dPresenter = new DPresenter(this);
    }
    public void login(View view){
        String stel = tel.getText().toString();
        String spwd = pwd.getText().toString();
        dPresenter.getLoginData("https://www.zhaoapi.cn/user/login",stel,spwd);
    }
    public void regist(View view){
        Intent intent=new Intent(MainActivity.this,Main2Activity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void LoginSuccess(final LoginBean loginBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if("0".equals(loginBean.getCode())){
                    Toast.makeText(MainActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(MainActivity.this,Main3Activity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(MainActivity.this,loginBean.getCode(),Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    public void RegistSuccess(RegistBean registBean) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(dPresenter!=null){
            dPresenter.destroy();
        }
    }
}

Main2Activity

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

import com.bwie.z.a0106test.bean.LoginBean;
import com.bwie.z.a0106test.bean.RegistBean;
import com.bwie.z.a0106test.presenter.DPresenter;
import com.bwie.z.a0106test.inter.IDActicity;

public class Main2Activity extends AppCompatActivity implements IDActicity {

    private EditText tel;
    private EditText pwd;
    private DPresenter dPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tel = (EditText) findViewById(R.id.tel);
        pwd = (EditText) findViewById(R.id.pwd);
        dPresenter = new DPresenter(this);
    }


    @Override
    public void LoginSuccess(LoginBean loginBean) {

    }

    @Override
    public void RegistSuccess(final RegistBean registBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if("0".equals(registBean.getCode())){
                    Toast.makeText(Main2Activity.this,registBean.getMsg(),Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(Main2Activity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(Main2Activity.this,registBean.getCode(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void regist(View view){
        String stel = tel.getText().toString();
        String spwd = pwd.getText().toString();
        dPresenter.getResgistdata("https://www.zhaoapi.cn/user/reg",stel,spwd);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

Main3Activity


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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

public class Main3Activity extends AppCompatActivity {

    private ListView lv;
    private EditText et;
    private List<String> list;
    private Myadapter myadapter;
    private String[] mLabels = {"购物","美食","游玩","北京","CSDN", "Airsaid", "周游", "新春送祝福", "Airsaid", "周游", "新春送祝福", "Airsaid", "周游", "新春送祝福"};
    private ZFlowLayout mlayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        et = (EditText) findViewById(R.id.et);
        lv = (ListView) findViewById(R.id.lv);
        mlayout = (ZFlowLayout) findViewById(R.id.mlayout);
        list = new ArrayList<>();
        initLabel();
    }
    private void initLabel() {
        ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(30, 30, 10, 10);// 设置边距
        for (int i = 0; i < mLabels.length; i++) {
            final TextView textView = new TextView(Main3Activity.this);
            textView.setTag(i);
            textView.setTextSize(15);
            textView.setText(mLabels[i]);
            textView.setPadding(24, 11, 24, 11);
            textView.setTextColor(Color.BLACK);
            textView.setBackgroundResource(R.drawable.lable_item_bg_normal);
            mlayout.addView(textView, layoutParams);
            // 标签点击事件
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), mLabels[(int) textView.getTag()], Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    public void sousuo(View view){
        Intent intent=new Intent(Main3Activity.this,Main4Activity.class);
        startActivity(intent);
        String set = et.getText().toString();
        /*myadapter = new Myadapter(list, this);
        list.add(set);
        lv.setAdapter(myadapter);
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,final int position, long id) {

                AlertDialog.Builder builder = new AlertDialog.Builder(Main3Activity.this);

                builder.setMessage("确定删除");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.remove(list.get(position));
                        myadapter.notifyDataSetChanged();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                return true;
            }
        });*/
        /*myadapter.notifyDataSetChanged();*/
    }
    public void sc(View view) {
        list.clear();
        myadapter.notifyDataSetChanged();
    }
}

Main4Activity


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.bwie.z.a0106test.adapter.GridAdapter;
import com.bwie.z.a0106test.adapter.RecyclerAdapter;
import com.bwie.z.a0106test.bean.SouBean;
import com.bwie.z.a0106test.utils.OkHttp3Util;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Main4Activity extends AppCompatActivity {

    private CheckBox huan;
    private XRecyclerView xrecyclerView;
    private List<SouBean.DataBean> list;
    private RecyclerAdapter recyclerAdapter;
    private Button sou;
    private EditText kuang;
    private SouBean js;
    boolean flag=false;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        huan = (CheckBox) findViewById(R.id.huan);
        sou = (Button) findViewById(R.id.sou);
        kuang = (EditText) findViewById(R.id.kuang);
        list = new ArrayList<>();
        huan.setChecked(flag);
        xrecyclerView = (XRecyclerView) findViewById(R.id.recycler_view);

        sou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String skuang = kuang.getText().toString();
                if(skuang.equals("")){
                    Toast.makeText(Main4Activity.this,"搜索为空",Toast.LENGTH_SHORT).show();
                }else {
                    String a="http://120.27.23.105/product/searchProducts"+"?"+"keywords="+skuang+"&page="+1+"&source=android";
                    OkHttp3Util.doGet(a, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Toast.makeText(Main4Activity.this,"失败",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if(response.isSuccessful()){
                                String string = response.body().string();
                                Log.i("=========",string);
                                Gson gson=new Gson();
                                js = gson.fromJson(string, SouBean.class);
                                List<SouBean.DataBean> data = js.getData();
                                list.addAll(data);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        xrecyclerView.setLayoutManager(new LinearLayoutManager(Main4Activity.this,LinearLayoutManager.VERTICAL,false));
                                        recyclerAdapter = new RecyclerAdapter(list,Main4Activity.this);
                                        xrecyclerView.setAdapter(recyclerAdapter);
                                    }
                                });
                                huan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                    @Override
                                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                                        if(flag){
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                            xrecyclerView.setLayoutManager(new LinearLayoutManager(Main4Activity.this,LinearLayoutManager.VERTICAL,false));
                                                            recyclerAdapter = new RecyclerAdapter(list,Main4Activity.this);
                                                            xrecyclerView.setAdapter(recyclerAdapter);
                                                            xrecyclerView.refreshComplete();


                                                }
                                            });
                                            huan.setChecked(false);
                                            huan.isChecked();
                                        }else {
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {

                                                            xrecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
                                                            GridAdapter pagrAdapter = new GridAdapter(list, Main4Activity.this);
                                                            xrecyclerView.setAdapter(pagrAdapter);
                                                            xrecyclerView.loadMoreComplete();

                                                }
                                            });
                                            huan.setChecked(true);
                                            flag=huan.isChecked();
                                        }
                                    }
                                });
                            }
                        }
                    });
                }

            }
        });


    }

}

流式布局的适配器


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class Myadapter extends BaseAdapter{
    List<String> list;
    Context context;
    public Myadapter(List<String> list,  Context context) {
        this.list = list;
        this.context=context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        viewHolder vh;
        if(view==null){
            vh=new viewHolder();
            view = View.inflate(context, R.layout.item, null);
            vh.tv=view.findViewById(R.id.tv);
            view.setTag(vh);
        } else {
            vh= (viewHolder) view.getTag();
        }
        vh.tv.setText(list.get(i).toString());
        return view;
    }
    class viewHolder{
        TextView tv;
    }
}

流式布局


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 ZFlowLayout extends ViewGroup {

    // 存储所有子View
    private List<List<View>> mAllChildViews = new ArrayList<>();
    // 每一行的高度
    private List<Integer> mLineHeight = new ArrayList<>();
    public ZFlowLayout(Context context) {
        this(context, null);
    }

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

    public ZFlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @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);

        // 如果当前ViewGroup的宽高为wrap_content的情况
        int width = 0;// 自己测量的 宽度
        int height = 0;// 自己测量的高度
        // 记录每一行的宽度和高度
        int lineWidth = 0;
        int lineHeight = 0;

        // 获取子view的个数
        int childCount = getChildCount();
        for(int i = 0;i < childCount; i++){
            View child = getChildAt(i);
            // 测量子View的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            // 子View占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            // 子View占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            // 换行时候
            if(lineWidth + childWidth > sizeWidth){
                // 对比得到最大的宽度
                width = Math.max(width, lineWidth);
                // 重置lineWidth
                lineWidth = childWidth;
                // 记录行高
                height += lineHeight;
                lineHeight = childHeight;
            }else{// 不换行情况
                // 叠加行宽
                lineWidth += childWidth;
                // 得到最大行高
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 处理最后一个子View的情况
            if(i == childCount -1){
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        // wrap_content
        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mAllChildViews.clear();
        mLineHeight.clear();
        // 获取当前ViewGroup的宽度
        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;
        // 记录当前行的view
        List<View> lineViews = new ArrayList<View>();
        int childCount = getChildCount();
        for(int i = 0;i < childCount; i ++){
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            // 如果需要换行
            if(childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width){
                // 记录LineHeight
                mLineHeight.add(lineHeight);
                // 记录当前行的Views
                mAllChildViews.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);
        mAllChildViews.add(lineViews);

        // 设置子View的位置
        int left = 0;
        int top = 0;
        // 获取行数
        int lineCount = mAllChildViews.size();
        for(int i = 0; i < lineCount; i ++){
            // 当前行的views和高度
            lineViews = mAllChildViews.get(i);
            lineHeight = mLineHeight.get(i);
            for(int j = 0; j < lineViews.size(); j ++){
                View child = lineViews.get(j);
                // 判断是否显示
                if(child.getVisibility() == View.GONE){
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int cLeft = left + lp.leftMargin;
                int cTop = top + lp.topMargin;
                int cRight = cLeft + child.getMeasuredWidth();
                int cBottom = cTop + child.getMeasuredHeight();
                // 进行子View进行布局
                child.layout(cLeft, cTop, cRight, cBottom);
                left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = 0;
            top += lineHeight;
        }

    }

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

DPresenter


import com.bwie.z.a0106test.bean.LoginBean;
import com.bwie.z.a0106test.bean.RegistBean;
import com.bwie.z.a0106test.inter.IDActicity;
import com.bwie.z.a0106test.inter.IDPresenter;
import com.bwie.z.a0106test.model.DModel;


public class DPresenter implements IDPresenter {

    private DModel dmodel;
    private IDActicity idActicity;

    public DPresenter(IDActicity idActicity) {
        this.idActicity = idActicity;
        dmodel = new DModel(this);
    }

    public void getLoginData(String url, String stel, String spwd) {
        dmodel.getLoginData(url,stel,spwd);

    }

    @Override
    public void onLoginSuccess(LoginBean loginBean) {
        idActicity.LoginSuccess(loginBean);
    }

    @Override
    public void onRegistSuccess(RegistBean registBean) {
        idActicity.RegistSuccess(registBean);
    }

    public void destroy(){
        if(idActicity!=null){
            idActicity=null;
        }
    }



    public void getResgistdata(String url, String stel, String spwd) {
        dmodel.getResgistData(url,stel,spwd);
    }
}

DModel

import com.bwie.z.a0106test.utils.OkHttp3Util;
import com.bwie.z.a0106test.bean.LoginBean;
import com.bwie.z.a0106test.bean.RegistBean;
import com.bwie.z.a0106test.inter.IDPresenter;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DModel {
    private IDPresenter idPresenter;

    public DModel(IDPresenter idPresenter) {
        this.idPresenter=idPresenter;

    }

    public void getLoginData(String url, String stel, String spwd) {
        Map<String, String> params=new HashMap<>();
        params.put("mobile",stel);
        params.put("password",spwd);
        //Log.d("qqqqq==","++++++++"+url);
        OkHttp3Util.doPost(url, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String json = response.body().string();
                    LoginBean loginBean=new Gson().fromJson(json,LoginBean.class);
                    idPresenter.onLoginSuccess(loginBean);
                }
            }
        });
    }

    public void getResgistData(String url, String stel, String spwd) {
        Map<String, String> params=new HashMap<>();
        params.put("mobile",stel);
        params.put("password",spwd);
        OkHttp3Util.doPost(url, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String json = response.body().string();
                    RegistBean registBean = new Gson().fromJson(json, RegistBean.class);
                    idPresenter.onRegistSuccess(registBean);
                }
            }
        });
    }
}

IDActicity


import com.bwie.z.a0106test.bean.LoginBean;
import com.bwie.z.a0106test.bean.RegistBean;


public interface IDActicity {
    void LoginSuccess(LoginBean loginBean);

    void RegistSuccess(RegistBean registBean);
}

IDPresenter


import com.bwie.z.a0106test.bean.LoginBean;
import com.bwie.z.a0106test.bean.RegistBean;


public interface IDPresenter {
    void onLoginSuccess(LoginBean loginBean);

    void onRegistSuccess(RegistBean registBean);
}

RecyclerAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwie.z.a0106test.R;
import com.bwie.z.a0106test.bean.SouBean;
import com.bwie.z.a0106test.utils.Imagbase;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class RecyclerAdapter extends XRecyclerView.Adapter<Linholder>{
    private List<SouBean.DataBean> list;
    private Context context;
    public RecyclerAdapter(List<SouBean.DataBean> list,Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public Linholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_child, parent, false);
        Linholder linholder=new Linholder(view);
        return linholder;
    }

    @Override
    public void onBindViewHolder(Linholder holder, int position) {
        holder.title.setText(list.get(position).getTitle());
        holder.price.setText("¥"+list.get(position).getBargainPrice());
        String[] split = list.get(position).getImages().split("\\|");
        ImageLoader.getInstance().displayImage(split[0],holder.img, Imagbase.getDefaultOption());
    }

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

Linholder

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.z.a0106test.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;


class Linholder extends XRecyclerView.ViewHolder{

    public   ImageView img;
    public   TextView title;
    public   TextView price;

    public Linholder(View itemView) {
        super(itemView);
        img = itemView.findViewById(R.id.item_img);
        title = itemView.findViewById(R.id.item_title);
        price = itemView.findViewById(R.id.item_price);
    }
}

GridAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwie.z.a0106test.R;
import com.bwie.z.a0106test.bean.SouBean;
import com.bwie.z.a0106test.utils.Imagbase;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class GridAdapter extends XRecyclerView.Adapter<Gridholder>{
    List<SouBean.DataBean> list;
    Context context;

    public GridAdapter(List<SouBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public Gridholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.grid_child, parent, false);
        Gridholder linholder = new Gridholder(view);
        return linholder;
    }

    @Override
    public void onBindViewHolder(Gridholder holder, int position) {
        holder.name.setText(list.get(position).getTitle());
        holder.price.setText("¥"+list.get(position).getBargainPrice());
        String[] split = list.get(position).getImages().split("\\|");
        ImageLoader.getInstance().displayImage(split[0],holder.img, Imagbase.getDefaultOption());
    }

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

Gridholder


import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.z.a0106test.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;


class Gridholder extends XRecyclerView.ViewHolder{

    public ImageView img;
    public TextView name;
    public TextView price;

    public Gridholder(View itemView) {
        super(itemView);
        img = itemView.findViewById(R.id.item_img);
        name = itemView.findViewById(R.id.item_title);
        price = itemView.findViewById(R.id.item_price);
    }
}








「LLM那些事」系列第 4 篇《上下文窗口的边界》,文章连接:https://blog.csdn.net/houwenjin/article/details/163999753。 演示什么:在「预测」Sheet 的黄色格子里输入一句话(默认「来泡一杯」),四个「模型」——分别只统计最后 1 / 2 / 3 / 4 个字的 n-gram 查表——同时预测下一个字。同一个输入,看的上下文越长,候选越少、预测越确定: ┌────────────────┬──────────┬───────────────┬──────┐ │ 只看最后几个字 │ 用的前缀 │ 候选下一字数 │ 预测 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 1 个 │ 杯 │ 3(茶/子/水) │ 模糊 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 2 个 │ 一杯 │ 2(茶/水) │ 收窄 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 3 个 │ 泡一杯 │ 1(茶) │ 确定 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 4 个 │ 来泡一杯 │ 1(茶) │ 确定 │ └────────────────┴──────────┴───────────────┴──────┘
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值