周五是个好日子呀!总结下,了解了下mvp模式自己尝试着结合volley网络请求框架写了个小demo,发现这玩意这是好用!!!
MVP 全称:Model-View-Presenter ;MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负责显示。
作为一种新的模式,MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,它们之间的通信是通过Presenter (MVC中的Controller)来进行的,所有的交互都发生在Presenter内部,而在MVC中View会直接从Model中读取数据而不是通过 Controller。
在MVC里,View是可以直接访问Model的!从而,View里会包含Model信息,不可避免的还要包括一些业务逻辑。 在MVC模型里,更关注的Model的不变,而同时有多个对Model的不同显示,即View。所以,在MVC模型里,Model不依赖于View,但是View是依赖于Model的。不仅如此,因为有一些业务逻辑在View里实现了,导致要更改View也是比较困难的,至少那些业务逻辑是无法重用的。
虽然 MVC 中的 View 的确“可以”访问 Model,但是我们不建议在 View 中依赖 Model,而是要求尽可能把所有业务逻辑都放在 Controller 中处理,而 View 只和 Controller 交互
看看代码
activity:
public class MainActivity extends Activity implements maininterface{
ListView lv;
itPresenter ip;
//加载自定义的圆圈
ProgressBarCircularIndeterminate pbci;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ip=new itPresenter(this,this);
lv=(ListView) findViewById(R.id.lv);
lv.setVerticalScrollBarEnabled(true);
pbci=(ProgressBarCircularIndeterminate) findViewById(R.id.progressBarCircularIndetermininate);
ip.getMsg();
}
@Override
public void listviewadd(ItModel itmodel) {
// TODO Auto-generated method stub
adapter ad=new adapter(MainActivity.this,itmodel);
lv.setAdapter(ad);
pbci.setVisibility(View.GONE);
}
}
public class adapter extends BaseAdapter{
private LayoutInflater mInflater;
ItModel itm;
Context ct;
public adapter(Context ct,ItModel itm)
{
this.mInflater = LayoutInflater.from(ct);
this.itm=itm;
this.ct=ct;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itm.getData().getItems().size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//显示优化(只要之前显示过的就可以不再再次从布局文件读取,直接从缓存中读取--ViewHolder的作用)
//其实是setTag和getTag中标签的作用
ViewHolder holder = null ;
if(convertView==null){ //如果是第一次显示该页面(要记得保存到viewholder中供下次直接从缓存中调用)
holder = new ViewHolder();
//获取list_item布局文件的视图
convertView = mInflater.inflate(R.layout.list_view, null);
holder.tx=(TextView) convertView.findViewById(R.id.tx);
holder.img=(ImageView) convertView.findViewById(R.id.img);
//设置控件集到convertView
convertView.setTag(holder);
}
else { //如果之前已经显示过该页面,则用viewholder中的缓存直接刷屏
holder =(ViewHolder)convertView.getTag();
}
//用Volley加载图片
VolleyLoadPicture vlp = new VolleyLoadPicture(ct,holder.img);
vlp.getmImageLoader().get(itm.getData().getItems().get(position).getCover_image_url(),vlp.getOne_listener());
holder.tx.setText(itm.getData().getItems().get(position).getShare_msg());
return convertView;
}
public final class ViewHolder
{
public TextView tx;
public ImageView img;
}
public class itPresenter implements accessStateInterface{
maininterface mf;
volleyInterface vif;
public itPresenter(maininterface mf,Context ct)
{
vif=new volleyInterface(this,ct);
this.mf=mf;
}
public void getMsg()
{
vif.sendJsonObject("http://api.wuliaoribao.com/v1/channels/16/items?limit=20&offset=0");
}
@Override
public void success(String data) {
// TODO Auto-generated method stub
Gson gson = new Gson();
ItModel itmodel = gson.fromJson(data, ItModel.class);
mf.listviewadd(itmodel);
}
@Override
public void fild(String error) {
// TODO Auto-generated method stub
}
}
public class ProgressBarCircularIndeterminate extends CustomView {
final static String ANDROIDXML = "http://schemas.android.com/apk/res/android";
int backgroundColor = Color.parseColor("#1E88E5");
public ProgressBarCircularIndeterminate(Context context, AttributeSet attrs) {
super(context, attrs);
setAttributes(attrs);
}
// Set atributtes of XML to View
protected void setAttributes(AttributeSet attrs){
setMinimumHeight(Utils.dpToPx(32, getResources()));
setMinimumWidth(Utils.dpToPx(32, getResources()));
//Set background Color
// Color by resource
int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1);
if(bacgroundColor != -1){
setBackgroundColor(getResources().getColor(bacgroundColor));
}else{
// Color by hexadecimal
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
if (background != -1)
setBackgroundColor(background);
else
setBackgroundColor(Color.parseColor("#1E88E5"));
}
setMinimumHeight(Utils.dpToPx(3, getResources()));
}
/**
* Make a dark color to ripple effect
* @return
*/
protected int makePressColor(){
int r = (this.backgroundColor >> 16) & 0xFF;
int g = (this.backgroundColor >> 8) & 0xFF;
int b = (this.backgroundColor >> 0) & 0xFF;
// r = (r+90 > 245) ? 245 : r+90;
// g = (g+90 > 245) ? 245 : g+90;
// b = (b+90 > 245) ? 245 : b+90;
return Color.argb(128,r, g, b);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(firstAnimationOver == false)
drawFirstAnimation(canvas);
if(cont > 0)
drawSecondAnimation(canvas);
invalidate();
}
float radius1 = 0;
float radius2 = 0;
int cont = 0;
boolean firstAnimationOver = false;
/**
* Draw first animation of view
* @param canvas
*/
private void drawFirstAnimation(Canvas canvas){
if(radius1 < getWidth()/2){
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(makePressColor());
radius1 = (radius1 >= getWidth()/2)? (float)getWidth()/2 : radius1+1;
canvas.drawCircle(getWidth()/2, getHeight()/2, radius1, paint);
}else{
Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(makePressColor());
temp.drawCircle(getWidth()/2, getHeight()/2, getHeight()/2, paint);
Paint transparentPaint = new Paint();
transparentPaint.setAntiAlias(true);
transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
if(cont >= 50){
radius2 = (radius2 >= getWidth()/2)? (float)getWidth()/2 : radius2+1;
}else{
radius2 = (radius2 >= getWidth()/2-Utils.dpToPx(4, getResources()))? (float)getWidth()/2-Utils.dpToPx(4, getResources()) : radius2+1;
}
temp.drawCircle(getWidth()/2, getHeight()/2, radius2, transparentPaint);
canvas.drawBitmap(bitmap, 0, 0, new Paint());
if(radius2 >= getWidth()/2-Utils.dpToPx(4, getResources()))
cont++;
if(radius2 >= getWidth()/2)
firstAnimationOver = true;
}
}
int arcD = 1;
int arcO = 0;
float rotateAngle = 0;
int limite = 0;
/**
* Draw second animation of view
* @param canvas
*/
private void drawSecondAnimation(Canvas canvas){
if(arcO == limite)
arcD+=6;
if(arcD >= 290 || arcO > limite){
arcO+=6;
arcD-=6;
}
if(arcO > limite + 290){
limite = arcO;
arcO = limite;
arcD = 1;
}
rotateAngle += 4;
canvas.rotate(rotateAngle,getWidth()/2, getHeight()/2);
Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(backgroundColor);
// temp.drawARGB(0, 0, 0, 255);
temp.drawArc(new RectF(0, 0, getWidth(), getHeight()), arcO, arcD, true, paint);
Paint transparentPaint = new Paint();
transparentPaint.setAntiAlias(true);
transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
temp.drawCircle(getWidth()/2, getHeight()/2, (getWidth()/2)-Utils.dpToPx(4, getResources()), transparentPaint);
canvas.drawBitmap(bitmap, 0, 0, new Paint());
}
// Set color of background
public void setBackgroundColor(int color){
super.setBackgroundColor(getResources().getColor(android.R.color.transparent));
if(isEnabled())
beforeBackground = backgroundColor;
this.backgroundColor = color;
}
}