1,依赖
compile 'com.squareup.okio:okio:1.5.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.google.code.gson:gson:2.8.2' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.jcodecraeer:xrecyclerview:1.3.2'
2,权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3,main布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="ywf.bwei.com.moni_zhoukaoer.MainActivity"> <EditText android:id="@+id/et_userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/et_userName" android:hint="密码" android:inputType="textPassword" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_weight="1" android:text="登录" android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_weight="1" android:text="注册" android:id="@+id/btn_re" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>4,main方法public class MainActivity extends AppCompatActivity implements Iview { private Presenter userPresenter; private EditText etUserName; private EditText etPassword; private Button btn_re; private Button btn_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUserName = (EditText) findViewById(R.id.et_userName); etPassword = (EditText) findViewById(R.id.et_password); btn_re = (Button) findViewById(R.id.btn_re); btn_login = (Button) findViewById(R.id.btn_login); userPresenter = new Presenter(this); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = etUserName.getText().toString().trim(); String pas = etPassword.getText().toString().trim(); if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pas)){ Toast.makeText(MainActivity.this, "输入项不能为空", Toast.LENGTH_SHORT).show(); return; }else { userPresenter.login(new User(name,pas),0); } } }); btn_re.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, zhuce.class); startActivity(intent); } }); } @Override public void loginSuccess() { Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show(); } @Override public void loginFailed() { Toast.makeText(MainActivity.this,"full",Toast.LENGTH_SHORT).show(); } }
5,zhuce布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="ywf.bwei.com.moni_zhoukaoer.zhuce"> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名" /> <EditText android:id="@+id/pas" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/et_userName" android:hint="密码" android:inputType="textPassword" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_weight="1" android:text="立即注册" android:id="@+id/re" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
6,zhuce方法
public class zhuce extends AppCompatActivity implements Iview { private EditText name; private EditText pas; private Button re; private Presenter userPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zhuce); name = (EditText) findViewById(R.id.name); pas = (EditText) findViewById(R.id.pas); re = (Button) findViewById(R.id.re); userPresenter =new Presenter(this); re.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name1 = name.getText().toString().trim(); String pas1 = pas.getText().toString().trim(); if(TextUtils.isEmpty(name1)||TextUtils.isEmpty(pas1)){ Toast.makeText(zhuce.this, "输入项不能为空", Toast.LENGTH_SHORT).show(); return; }else { userPresenter.login(new User(name1,pas1),1); } } }); } @Override public void loginSuccess() { Toast.makeText(zhuce.this,"成功",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(zhuce.this, MainActivity.class); startActivity(intent); finish(); } @Override public void loginFailed() { Toast.makeText(zhuce.this,"失败",Toast.LENGTH_SHORT).show(); } }
7,main2布局<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="ywf.bwei.com.moni_zhoukaoer.Main2Activity"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/recal" android:layout_width="match_parent" android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView> </android.support.constraint.ConstraintLayout>
8,main2 方法public class Main2Activity extends AppCompatActivity implements IView { private XRecyclerView mRecal; private List<Goods.TuijianBean.ListBean> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mRecal = (XRecyclerView) findViewById(R.id.recal); mRecal.setLayoutManager(new LinearLayoutManager(this)); press pre = new press(this); Map<String,String> mmap = new HashMap<>(); pre.getUrl(API.HOST,mmap); mRecal.setPullRefreshEnabled(true); mRecal.setLoadingMoreEnabled(true); mRecal.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { Toast.makeText(Main2Activity.this, "刷新", Toast.LENGTH_SHORT).show(); mRecal.refreshComplete(); } @Override public void onLoadMore() { Toast.makeText(Main2Activity.this, "加载更多", Toast.LENGTH_SHORT).show(); mRecal.loadMoreComplete(); } }); } public void getData(Goods goods) { MaDapter maDapter = new MaDapter(Main2Activity.this, goods.getTuijian().getList()); mRecal.setAdapter(maDapter); } }
9,item布局<?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" android:orientation="horizontal"> <ImageView android:id="@+id/head" android:layout_width="150dp" android:layout_height="150dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="11111"/> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1111" android:textSize="20sp" android:textColor="#ed0b26"/> </LinearLayout> </LinearLayout>
10,写个app包 里面写个MyAPP类
public class MyAPP extends Application { public static MyAPP mInstance; @Override public void onCreate() { super.onCreate(); mInstance = this; ImageLoaderConfiguration aDefault = ImageLoaderConfiguration.createDefault(this); ImageLoader.getInstance().init(aDefault); } public static MyAPP getInstance() { return mInstance; } }在写个Login包,包里面在创建个View,model,persenter,bean,api包view包里是个接口public interface Iview { void loginSuccess(); void loginFailed(); }model包里有个类,有个接口public class UserModel implements Imodel{ //定义接口变量 private OnFinishLisenter lisenter; //定义接口 public interface OnFinishLisenter{ void onFinish(Userbean userbean); void onFinish2(Register register); } public void setOnFinishLisenter(OnFinishLisenter lisenter){ this.lisenter = lisenter; } @Override public void login(final User user, int i) { HashMap<String,String> map = new HashMap<>(); map.put("mobile",user.getName()); map.put("password",user.getPass()); if(i==0){ OkHttp3Utils.doPost(LoginApi.LOGIN, map, new GsonObjectCallback<Userbean>() { @Override public void onUi(Userbean userbean) { if(lisenter!=null){ lisenter.onFinish(userbean); } } @Override public void onFailed(Call call, IOException e) { } }); }else { OkHttp3Utils.doPost(LoginApi.REG, map, new GsonObjectCallback<Register>() { @Override public void onUi(Register register) { if(lisenter!=null){ lisenter.onFinish2(register); } } @Override public void onFailed(Call call, IOException e) { } }); } } }model接口public interface Imodel { void login(User user, int i); }persenter包里写个类public class Presenter implements UserModel.OnFinishLisenter{ private Iview iview; private UserModel userModel; public Presenter (Iview iview){ this.iview = iview; this.userModel = new UserModel(); } public void login(User user, int i){ userModel.setOnFinishLisenter(this); userModel.login(user,i); } public void onFinish(Userbean userbean) { String code = userbean.getCode(); if("0".equals(code)){ iview.loginSuccess(); }else { iview.loginFailed(); } } @Override public void onFinish2(Register register) { String code = register.getCode(); if("0".equals(code)){ iview.loginSuccess(); }else { iview.loginFailed(); } } }
bean包里面 Register,Userbean,User
Register 写解析 http://120.27.23.105/user/reg
Userbean 写解析 http://120.27.23.105/user/login
User 里面写
public class User { private String name; private String pass; public User(String name, String pass) { this.name = name; this.pass = pass; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", pass='" + pass + '\'' + '}'; } }
11,写个api包,里面写个 LoginApi类
public class LoginApi { public static final String LOGIN = "http://120.27.23.105/user/login"; public static final String REG="http://120.27.23.105/user/reg"; }12,在写个show的总包,里面有View,model,persenter,bean,api包View包里写个接口public interface IView { void getData(Goods goods); }model包里有个类,有个接口public class IModel implements Model { Goods goods = new Goods(); private OnFinish onFinish; public interface OnFinish{ void onFinishListener(Goods goods); } public void setOnFinish(OnFinish onFinish) { this.onFinish = onFinish; } @Override public void getUrl(String url, Map<String,String> mmap) { OkHttp3Utils.doPost(url, mmap, new GsonObjectCallback<Goods>() { @Override public void onUi(Goods goods) { onFinish.onFinishListener(goods); } @Override public void onFailed(Call call, IOException e) { } }); } }model接口public interface Model { void getUrl(String url, Map<String,String > mapp); }persenter包里有个类public class press implements IModel.OnFinish { private IView userview; private IModel model; public press(IView userview) { this.userview = userview; this.model=new IModel(); model.setOnFinish(this); } public void getUrl(String url, Map<String,String> mmap){ model.getUrl(url,mmap); } @Override public void onFinishListener(Goods goods) { userview.getData(goods); } }bean包写个类 Goods
Goods类里写要解析 http://120.27.23.105/ad/getAd
api包有个API类
public class API { public static final String HOST="http://120.27.23.105/ad/getAd"; }在show的总包写写个 MaDapter 类
public class MaDapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { Context context; private List<Goods.TuijianBean.ListBean> list; public MaDapter(Context context, List<Goods.TuijianBean.ListBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); MyRecyclerViewHolder viewHolder = new MyRecyclerViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { Goods.TuijianBean.ListBean listBean = list.get(position); String images = listBean.getImages(); String[] split1 = images.split("\\|"); ImageLoader.getInstance().displayImage(split1[0],((MyRecyclerViewHolder)holder).head); ((MyRecyclerViewHolder)holder).title.setText(listBean.getTitle()); ((MyRecyclerViewHolder)holder).price.setText("¥"+listBean.getPrice()); } @Override public int getItemCount() { return list.size(); } class MyRecyclerViewHolder extends RecyclerView.ViewHolder{ ImageView head; TextView title; TextView price; public MyRecyclerViewHolder(View itemView) { super(itemView); head = itemView.findViewById(R.id.head); title = itemView.findViewById(R.id.title); price = itemView.findViewById(R.id.price); } } }注意:在导进utils工具类application 要name="";