布局 login_activity
<?xml version="1.0" encoding="utf-8"?><TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="简易登录"
android:textSize="30sp" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp" />
<EditText
android:id="@+id/ed_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入用户名..." />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20sp" />
<EditText
android:id="@+id/ed_pwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入密码..."
android:password="true" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/btn_jzpwd"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="记住密码" />
<CheckBox
android:id="@+id/btn_bcpwd"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="保存密码" />
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="登录登录" />
<Button
android:id="@+id/login_reg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
**注册布局** <?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="简易注册"
android:textSize="30sp" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp" />
<EditText
android:id="@+id/ed_zcname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入用户名..." />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20sp" />
<EditText
android:id="@+id/ed_zcpwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入密码..." />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ccc" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="注册注册" />
**刷新布局** <?xml version="1.0" encoding="utf-8"?>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pullto"
android:layout_width="match_parent"
android:layout_height="match_parent" />
**生成二维码** <?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/sou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:text="扫一扫有惊喜~" />
<ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp" />
LoginActivity主页面
package com.example.lg.lx_1207;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import bean.News;
import bean.User;
import core.DataCall;
import presenter.LoginPresenter;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, DataCall {
/**
* 请输入用户名...
*/
private EditText mEdName;
/**
* 请输入密码...
*/
private EditText mEdPwd;
/**
* 记住密码
*/
private CheckBox mBtnJzpwd;
/**
* 保存密码
*/
private CheckBox mBtnBcpwd;
/**
* 登录登录
*/
private Button mBtnLogin;
LoginPresenter presenter = new LoginPresenter(this);
/**
* 注册
*/
private Button mLoginReg;
private String mobile;
private String password;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//初始化控件
initView();
//创建sp
sp = getSharedPreferences("config.txt", Context.MODE_PRIVATE);
//判断是否记住密码
rememberPwd();
}
//记住密码
private void rememberPwd() {
//获取传来的账号密码
String getmoilde = sp.getString("putmoilde", "");
String getpwd = sp.getString("putpwd", "");
boolean getremember = sp.getBoolean("putremember", false);
//判断选中状态
if (getremember) {
//选中设置 默认登录显示
mEdName.setText(getmoilde);
mEdPwd.setText(getpwd);
//设置选中
mBtnJzpwd.setChecked(getremember);
} else {
//未选中为空
mEdName.setText("");
mEdPwd.setText("");
mBtnJzpwd.setChecked(false);
}
}
//初始化控件
private void initView() {
mEdName = findViewById(R.id.ed_name);
mEdPwd = findViewById(R.id.ed_pwd);
mBtnJzpwd = findViewById(R.id.btn_jzpwd);
mBtnBcpwd = findViewById(R.id.btn_bcpwd);
mBtnLogin = findViewById(R.id.btn_login);
mBtnLogin.setOnClickListener(this);
mLoginReg = findViewById(R.id.login_reg);
mLoginReg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.btn_login:
//登录获取输入的账户密码
mobile = mEdName.getText().toString();
password = mEdPwd.getText().toString();
//p层方法
presenter.request(mobile, password);
break;
case R.id.login_reg:
//注册页面跳转
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
break;
}
}
//调用接口
@Override
public void callback(Object data) {
//数据转型
User user = (User) data;
Toast.makeText(getBaseContext(), user.getCode() + " " + user.getMsg(), Toast.LENGTH_LONG).show();
//*********获取记住密码勾选状态
boolean remember = mBtnJzpwd.isChecked();
SharedPreferences.Editor edit = sp.edit();
//判断是否记住密码
if (remember) {//勾选传值
edit.putString("putmoilde", mobile);
edit.putString("putpwd", password);
edit.putBoolean("putremember", true);
} else {//未勾选 复选框未选中
edit.putBoolean("putremember", false);
}
edit.commit();
//登录跳转
Intent intent = new Intent(LoginActivity.this, ShowActivity.class);
intent.putExtra("name", mobile);//传入用户名 做二维码
startActivity(intent);
}
@Override
public void faild(String error) {//异常吐司
Toast.makeText(getBaseContext(), " 有问题啊老铁", Toast.LENGTH_LONG).show();
}
@Override
public void showcallback(News news) {
}
}
RegisterActivity注册
package com.example.lg.lx_1207;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import bean.News;
import bean.User;
import core.DataCall;
import presenter.RegisterPresenter;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, DataCall {
/**
* 请输入用户名...
*/
private EditText mEdZcname;
/**
* 请输入密码...
*/
private EditText mEdZcpwd;
/**
* 注册注册
*/
private Button mBtnRegister;
RegisterPresenter presenter = new RegisterPresenter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initView();
}
private void initView() {
mEdZcname = findViewById(R.id.ed_zcname);
mEdZcpwd = findViewById(R.id.ed_zcpwd);
mBtnRegister = findViewById(R.id.btn_register);
mBtnRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.btn_register:
final String mobile = mEdZcname.getText().toString();
final String password = mEdZcpwd.getText().toString();
presenter.request(mobile, password);
break;
}
}
@Override
public void callback(Object data) {
User user = (User) data;
Toast.makeText(getBaseContext(),user.getCode()+" "+user.getMsg(),Toast.LENGTH_LONG).show();
finish();
}
@Override
public void faild(String error) {
Toast.makeText(getBaseContext(),"有问题啊老铁"+error,Toast.LENGTH_LONG).show();
}
@Override
public void showcallback(News news) {
}
}
BasePrsenter
package presenter;
import android.os.Handler;
import android.os.Message;
import bean.User;
import core.DataCall;
/**
-
P层基类----注意抽象类
*/
public abstract class BasePresenter {private final DataCall call;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
//******super删掉
//取得消息–转成封装bean类
User user = (User) msg.obj;
//获取状态码
String u = user.getCode();
//强转int—做判断
int u2 = Integer.parseInt(u);
//为0则成功 调用 正确接口
if (u2 == 0) {
call.callback(msg.obj);} else {//不为0 调用 错误接口 call.faild(u); } }
};
//重写P层调用接口
public BasePresenter(DataCall dataCall) {
call = dataCall;
}/**
- 这个方法被子类实现之后,方法里面调用model获取数据----Stirng 数据
*/
public abstract Object useModel(String… args);
public void request(final String… args) {
new Thread() {
@Override
public void run() {
super.run();
//获取数据
Object data = useModel(args);
//handler消息
Message message = handler.obtainMessage();
message.obj = data;
//发送消息
handler.sendMessage(message);
}
}.start();
}
}
LoginPresenter
package presenter; - 这个方法被子类实现之后,方法里面调用model获取数据----Stirng 数据
import core.DataCall;
import model.LoginModel;
/***
-
登录P层----继承基类
*/
public class LoginPresenter extends BasePresenter {
public LoginPresenter(DataCall dataCall) {
super(dataCall);
}@Override
public Object useModel(String… args) {
//返回 登录M
return LoginModel.login(args[0], args[1]);
}
}
RegisterPresenter
package presenter;
import core.DataCall;
import model.RegisterModel;
/***
-
注册P层----继承基类
*/
public class RegisterPresenter extends BasePresenter {
public RegisterPresenter(DataCall dataCall) {
super(dataCall);
}@Override
public Object useModel(String… args) {
//返回注册M层
return RegisterModel.register(args[0], args[1]);
}
}
ShowPresenter
package presenter;
import android.os.Handler;
import android.os.Message;
import bean.News;
import core.DataCall;
import model.ShowModel;
/**
-
展示P层 接口不同 需要自己写
*/
public class ShowPresenter {private final DataCall dataCall;
//重写 调用接口
public ShowPresenter(DataCall back) {
dataCall = back;
}private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
News news = (News) msg.obj;
//调用接口
dataCall.showcallback(news);
}
};//sel方法–子线程
public void sel(final int page) {
new Thread() {
@Override
public void run() {
super.run();
//获取数据
News news = ShowModel.sel(page);
Message message = handler.obtainMessage();
message.obj = news;
//发送handler
handler.sendMessage(message);
}
}.start();}
}
httpUtils
package utils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpUtils {
private static final String TAG = “HttpUtils+++++”;
public static String get(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();//获取网络返回的输入流;
//可拼接的字符串
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp = “”;
while ((temp = bufferedReader.readLine()) != null) {
stringBuilder.append(temp);
temp = “”;
}
//这个是网络获取的数据
String data = stringBuilder.toString();
Log.d(TAG, “get: +++++”+data);
return data;
} catch (Exception e) {
e.printStackTrace();
}
return “”;
}
}
modelLogin
package model;
import com.google.gson.Gson;
import bean.User;
import utils.HttpUtils;
/**
- 登录M层
*/
public class LoginModel {
public static User login(String mobile, String password) {
//工具类获取登录接口
String loginData = HttpUtils.get(“http://www.zhaoapi.cn/user/login?mobile=”
+ mobile + “&password=” + password);
//解析数据
Gson gson = new Gson();
User user = gson.fromJson(loginData, User.class);
return user;
}
}
show展示数据
package model;
import com.google.gson.Gson;
import bean.News;
import utils.HttpUtils;
/**
- 展示M层
*/
public class ShowModel {
public static News sel(int page) {
//工具类获取数据接口
String news_data = HttpUtils.get(“http://api.tianapi.com/huabian/?key=0a62585968ff23c66d052aec24bc6cc1&num=10&page=” + page);
//解析数据
Gson gson = new Gson();
News news = gson.fromJson(news_data, News.class);
return news;
}
}
接口展示datacall
package core;
import bean.News;
import bean.User;
/**
-
接口
*/
public interface DataCall {
//正确接口
void callback(Object data);//错误接口
void faild(String error);//展示接口
void showcallback(News news);
}
MyApp
package app;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.os.Handler;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import java.io.File;
/**
- Created by gjl on 2018/11/9.
*/
public class MyApp extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context=this;
// 初始化imageloa
ImageLoader.getInstance().init(getConfig());
}
// 全局的上下文
public static Context getContext(){
return context;
}
public ImageLoaderConfiguration getConfig() {
String path = Environment.getExternalStorageDirectory() + "/imagefile";
File cacheDir = new File(path);
if (!cacheDir.exists()) {
cacheDir.mkdir();
}
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.diskCache(new UnlimitedDiskCache(cacheDir)) // default 闪存缓存
.diskCacheSize(50 * 1024 * 1024) // 闪存缓存大小
.writeDebugLogs() // LOG
.build();
return config;
}
// 配置
public static DisplayImageOptions getOptions() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true) // default
.cacheOnDisk(true) // default
.bitmapConfig(Bitmap.Config.ARGB_4444) // default
.displayer(new SimpleBitmapDisplayer()) // default
.handler(new Handler()) // default
.build();
return options;
}
}
NewSAdapter优化
package adapters;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.lg.lx_1207.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
import app.MyApp;
import bean.News;
/**
- Created by Lenovo on 2018/12/6.
*/
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<News.NewslistBean> list;
public NewsAdapter(Context context, List<News.NewslistBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHodel hodel;
if (convertView == null) {
convertView = View.inflate(context, R.layout.itemlayout, null);
hodel = new ViewHodel();
hodel.text_title = convertView.findViewById(R.id.textview);
hodel.img_images = convertView.findViewById(R.id.imageview);
convertView.setTag(hodel);
} else {
hodel = (ViewHodel) convertView.getTag();
}
hodel.text_title.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getPicUrl(), hodel.img_images, MyApp.getOptions());
return convertView;
}
class ViewHodel {
TextView text_title;
ImageView img_images;
}
}
Fragment01
package fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.lg.lx_1207.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.List;
import adapters.NewsAdapter;
import app.MyApp;
import bean.News;
import core.DataCall;
import presenter.ShowPresenter;
public class Fragment1 extends Fragment implements DataCall {
private PullToRefreshListView listView;
private int type = 0;
private int page = 1;
private List<News.NewslistBean> list;
private NewsAdapter adapter;
private ShowPresenter presenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
//初始化控件
listView = view.findViewById(R.id.pullto);
presenter = new ShowPresenter(this);
presenter.sel(page);
listView.setMode(PullToRefreshBase.Mode.BOTH);
listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
type = 1;
page = 1;
getDataForNet(type, page);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
type = 2;
page++;
getDataForNet(type, page);
}
});
return view;
}
private void getDataForNet(int type, int page) {
switch (type) {
case 0:
adapter.notifyDataSetChanged();
break;
case 1:
adapter.notifyDataSetChanged();
listView.onRefreshComplete();
break;
case 2:
presenter.sel(page);
adapter.notifyDataSetChanged();
listView.onRefreshComplete();
break;
}
}
@Override
public void callback(Object data) {
}
@Override
public void faild(String error) {
}
@Override
public void showcallback(News news) {
list = news.getNewslist();
//建立适配器
adapter = new NewsAdapter(MyApp.getContext(), list);
listView.setAdapter(adapter);
if (type == 2) {
List<News.NewslistBean> list1 = news.getNewslist();
list.addAll(list1);
type = 0;
}
}
}
Fragment02
package fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.lg.lx_1207.R;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
public class Fragment2 extends Fragment {
private static final String TAG = “Fragment2++++”;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bitmap bitmap = (Bitmap) msg.obj;
imageView.setImageBitmap(bitmap);
}
};
private ImageView imageView;
private String name;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, container, false);
imageView = view.findViewById(R.id.img);
Intent intent = getActivity().getIntent();
name = intent.getStringExtra("name");
Log.d(TAG, "onCreateView: ++++" + name);
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(name, 300);
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);
}
}) {
}.start();
return view;
}
}