5. 零碎知识整理
(1) 类似QQ,微信聊天界面的ListView适配器
package com.liuguilin.butlerservice.adapter;
/*
* 项目名: AndroidRepost
* 包名: com.liuguilin.androidrepost
* 文件名: ChatListAdapter
* 创建者: LGL
* 创建时间: 2016/8/23 17:27
* 描述: 机器人聊天适配器
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.liuguilin.butlerservice.R;
import com.liuguilin.butlerservice.entity.ChatListBean;
import com.liuguilin.butlerservice.utils.UtilTools;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class ChatListAdapter extends BaseAdapter{
public static final int VALUE_LEFT_TEXT = 1;
public static final int VALUE_RIGHT_TEXT = 2;
private LayoutInflater mInflater;
private Context mContext;
private List<ChatListBean> myList;
public ChatListAdapter(Context context, List<ChatListBean> myList) {
this.myList = myList;
this.mContext = context;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return myList.size();
}
@Override
public Object getItem(int arg0) {
return myList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ChatListBean msg = myList.get(position);
int type = getItemViewType(position);
ViewHolderRightText holderRightText = null;
ViewHolderLeftText holderLeftText = null;
if (convertView == null) {
switch (type) {
// 左边
case VALUE_LEFT_TEXT:
holderLeftText = new ViewHolderLeftText();
convertView = mInflater.inflate(R.layout.chatlist_left, null);
holderLeftText.btnLeftText = (TextView) convertView
.findViewById(R.id.chatlist_tv_left);
holderLeftText.btnLeftText.setText(msg.getValue());
convertView.setTag(holderLeftText);
break;
// 右边
case VALUE_RIGHT_TEXT:
holderRightText = new ViewHolderRightText();
convertView = mInflater.inflate(R.layout.chatlist_right, null);
holderRightText.btnRightText = (TextView) convertView
.findViewById(R.id.chatlist_tv_right);
holderRightText.iv_user_icon = (CircleImageView) convertView.findViewById(R.id.iv_user_icon);
holderRightText.btnRightText.setText(msg.getValue());
convertView.setTag(holderRightText);
break;
}
} else {
switch (type) {
case VALUE_LEFT_TEXT:
holderLeftText = (ViewHolderLeftText) convertView.getTag();
holderLeftText.btnLeftText.setText(msg.getValue());
break;
case VALUE_RIGHT_TEXT:
holderRightText = (ViewHolderRightText) convertView.getTag();
holderRightText.btnRightText.setText(msg.getValue());
UtilTools.getImageToShare(mContext, holderRightText.iv_user_icon);
break;
}
}
return convertView;
}
/**
* 根据数据源的position返回需要显示的的layout的type
* <p/>
* type的值必须从0开始
*/
@Override
public int getItemViewType(int position) {
ChatListBean msg = myList.get(position);
int type = msg.getType();
return type;
}
/**
* 返回所有的layout的数量
*/
@Override
public int getViewTypeCount() {
return 3;
}
// 左边的文本
class ViewHolderLeftText {
private TextView btnLeftText;
}
// 右边的文本
class ViewHolderRightText {
private TextView btnRightText;
private CircleImageView iv_user_icon;
}
}
(2) JSON解析数据
数据:{
"resultcode": "200",
"reason": "成功的返回",
"result": {
"company": "自动匹配",
"com": "auto",
"no": "3950371339303",
"status": "1",
"list": [
{
"datetime": "2017-01-17 16:55:42",
"remark": "到达:江苏市场部庆阳公司 已收件",
"zone": ""
},
{
"datetime": "2017-01-17 19:10:29",
"remark": "到达:江苏市场部庆阳公司 发往:江苏连云港公司",
"zone": ""
},
{
"datetime": "2017-01-17 19:39:48",
"remark": "到达:江苏市场部庆阳公司 已收件",
"zone": ""
},
{
"datetime": "2017-01-17 20:23:03",
"remark": "到达:江苏常州分拨中心 上级站点:江苏市场部庆阳公司",
"zone": ""
},
{
"datetime": "2017-01-17 20:24:38",
"remark": "到达:江苏常州分拨中心 发往:江苏淮安分拨中心",
"zone": ""
},
{
"datetime": "2017-01-18 06:04:15",
"remark": "到达:江苏淮安分拨中心 发往:江苏连云港分拨中心",
"zone": ""
},
{
"datetime": "2017-01-18 11:14:15",
"remark": "到达:江苏连云港公司 发往:江苏赣榆县公司",
"zone": ""
},
{
"datetime": "2017-01-19 11:44:46",
"remark": "到达:江苏赣榆县公司 上级站点:江苏连云港公司 发往:",
"zone": ""
},
{
"datetime": "2017-01-19 14:53:37",
"remark": "到达:江苏赣榆县公司班庄便民寄存点分部 上级站点:江苏赣榆县公司 发往:",
"zone": ""
},
{
"datetime": "2017-01-19 17:42:57",
"remark": "到达:江苏赣榆县公司班庄便民寄存点分部 指定:朱范韵达(14751336022) 派送",
"zone": ""
},
{
"datetime": "2017-01-19 21:30:48",
"remark": "已签收,签收人:西朱范韵达,感谢使用韵达,期待再次为您服务",
"zone": ""
}
]
},
"error_code": 0
}
解析方法:
private List<CourierData> mList = new ArrayList<>();//解析出来的存储数据
private void getJson(String t) {
try {
JSONObject jsonObject = new JSONObject(t);
JSONObject jsonResult = jsonObject.getJSONObject("result");
JSONArray jsonArray = jsonResult.getJSONArray("list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = (JSONObject) jsonArray.get(i);
CourierData data = new CourierData();
data.setRemark(json.getString("remark"));
data.setZone(json.getString("zone"));
data.setDatetime(json.getString("datetime"));
mList.add(data);
}
Collections.reverse(mList);
} catch (JSONException e) {
e.printStackTrace();
}
}
(3) 获取版本号
/**
* 获取版本号
*
* @return
*/
public String getVersion() {
try {
PackageManager manager = getPackageManager();
PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
String version = info.versionName;
return "版本号:" + version;
} catch (Exception e) {
e.printStackTrace();
return "无法获取版本号";
}
}
(4) 设置字体
/**
* 设置字体
*
* @param mContext
* @param textView
*/
public static void setTextViewFont(Context mContext, TextView textView) {
final Typeface fontFace = Typeface.createFromAsset(mContext.getAssets(),
"fonts/YYG.TTF");
textView.setTypeface(fontFace);
}
(5) WebView加载网页时顶部显示进度条
private ProgressBar pb;
pb = (ProgressBar) findViewById(R.id.pb);
pb.setMax(100);
/**
* 接口回调
*/
public class WebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
pb.setProgress(newProgress);
if (newProgress == 100) {
pb.setVisibility(View.GONE);
}
super.onProgressChanged(view, newProgress);
}
}
(6) Log封装
package com.liuguilin.butlerservice.utils;
/*
* 项目名: ButlerService
* 包名: com.liuguilin.butlerservice.utils
* 文件名: L
* 创建者: LGL
* 创建时间: 2016/8/24 10:40
* 描述: Log封装类
*/
import android.util.Log;
/**
* Log等级:i,v,d,w,e
*/
public class L {
//公共的Tag
public static final String TAG = "ButlerService";
//调试开关
public static boolean DEBUG = true;
public static void i(String log) {
if (DEBUG) {
Log.i(TAG, log);
}
}
public static void v(String log) {
if (DEBUG) {
Log.v(TAG, log);
}
}
public static void d(String log) {
if (DEBUG) {
Log.d(TAG, log);
}
}
public static void w(String log) {
if (DEBUG) {
Log.w(TAG, log);
}
}
public static void e(String log) {
if (DEBUG) {
Log.e(TAG, log);
}
}
}
(7) Picasso工具类
package com.liuguilin.butlerservice.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
/**
* Picasso工具类
* Created by LGL on 2016/6/23.
*/
public class PicassoUtils {
/**
* 指定大小加载图片
*
* @param mContext 上下文
* @param path 图片路径
* @param width 宽
* @param height 高
* @param mImageView 控件
*/
public static void loadImageViewSize(Context mContext, String path, int width, int height, ImageView mImageView) {
Picasso.with(mContext).load(path).resize(width, height).centerCrop().into(mImageView);
}
/**
* 加载有默认图片
*
* @param mContext 上下文
* @param path 图片路径
* @param resId 默认图片资源
* @param mImageView 控件
*/
public static void loadImageViewHolder(Context mContext, String path, int resId, ImageView mImageView) {
Picasso.with(mContext).load(path).fit().placeholder(resId).into(mImageView);
}
/**
* 裁剪图片
*
* @param mContext 上下文
* @param path 图片路径
* @param mImageView 控件
*/
public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView) {
Picasso.with(mContext).load(path).transform(new CropImageView()).into(mImageView);
}
/**
* 自定义图片裁剪
*/
public static class CropImageView implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap newBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (newBitmap != null) {
//内存回收
source.recycle();
}
return newBitmap;
}
@Override
public String key() {
return "lgl";
}
}
}
(8) 封装SharedPreferences
package com.liuguilin.butlerservice.utils;
/*
* 项目名: ButlerService
* 包名: com.liuguilin.butlerservice.utils
* 文件名: ShareUtils
* 创建者: LGL
* 创建时间: 2016/8/24 10:44
* 描述: 封装SharedPreferences
*/
import android.content.Context;
import android.content.SharedPreferences;
public class ShareUtils {
/**
* 存字符串
*
* @param mContext
* @param key
* @param values
*/
public static void putString(Context mContext, String key, String values) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().putString(key, values).commit();
}
/**
* 取字符串
*
* @param mContext
* @param key
* @param values 默认值
* @return 取出的值
*/
public static String getString(Context mContext, String key, String values) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getString(key, values);
}
/**
* 存布尔值
*
* @param mContext
* @param key
* @param values
*/
public static void putBoolean(Context mContext, String key, boolean values) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().putBoolean(key, values).commit();
}
/**
* 取布尔值
*
* @param mContext
* @param key
* @param values 默认值
* @return true/false
*/
public static boolean getBoolean(Context mContext, String key, boolean values) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getBoolean(key, values);
}
/**
* 存int值
*
* @param mContext
* @param key
* @param values 值
*/
public static void putInt(Context mContext, String key, int values) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().putInt(key, values).commit();
}
/**
* 取int值
*
* @param mContext
* @param key
* @param values 默认值
* @return
*/
public static int getInt(Context mContext, String key, int values) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getInt(key, values);
}
/**
* 删除一条字段
*
* @param mContext
* @param key
*/
public static void deleShare(Context mContext, String key) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().remove(key).commit();
}
/**
* 删除全部数据
*
* @param mContext
*/
public static void deleShareAll(Context mContext) {
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().clear().commit();
}
}
OK,完成! O(∩_∩)O 这只是一个简单的项目,这学期还得抓紧多写点更高难度的项目,加油!!!
软件github下载地址:
https://github.com/SunnyGZY/SmartButler