android学习 豆瓣 整体架构

来源:互联网 发布:理财哪个软件好 编辑:程序博客网 时间:2024/05/01 21:08

这次的课程的架构不像上次的那么难 ,但是不能说不好,体现了简洁的智慧这是应该好好学习的

baseactivity:规范了一些需要的方法 初始化了重要的一些变量 初始化了一些控件

也可以在base中添加menu 按需添加

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = new MenuInflater(this);inflater.inflate(R.menu.main_tab_menu, menu);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.main_tab_menu_clear_user:System.out.println("clear");SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);Editor editor = sp.edit();editor.putString("accesstoken", "");editor.putString("tokensecret", "");editor.commit();break;}return super.onOptionsItemSelected(item);}

baseactivity:


package cn.itcast.douban;import com.google.gdata.client.douban.DoubanService;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.AnimationSet;import android.view.animation.ScaleAnimation;import android.widget.ImageButton;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;public abstract class  BaseActivity extends Activity {public TextView mTextViewTitle;public RelativeLayout mRelativeLoading;public DoubanService myService;public ImageButton mImageBack;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);String apiKey = "0c51c1ba21ad8cfd24f5452e6508a6f7";String secret = "359e16e5e5c62b6e";myService = new DoubanService("黑马小瓣瓣", apiKey,secret);//密钥设置给myServiceSharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);String accesstoken = sp.getString("accesstoken", "");String tokensecret = sp.getString("tokensecret", "");myService.setAccessToken(accesstoken, tokensecret);setupView();setListener() ;fillData();}public abstract void setupView();public abstract void setListener();public abstract void fillData();public  void showLoading(){mRelativeLoading.setVisibility(View.VISIBLE);AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);aa.setDuration(1000);ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);sa.setDuration(1000);AnimationSet set = new AnimationSet(false);set.addAnimation(sa);set.addAnimation(aa);mRelativeLoading.setAnimation(set);mRelativeLoading.startAnimation(set);}public  void hideLoading(){AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);aa.setDuration(1000);ScaleAnimation sa = new ScaleAnimation(1.0f, 0.0f, 1.0f,0.0f);sa.setDuration(1000);AnimationSet set = new AnimationSet(false);set.addAnimation(sa);set.addAnimation(aa);mRelativeLoading.setAnimation(set);mRelativeLoading.startAnimation(set);mRelativeLoading.setVisibility(View.INVISIBLE);}public void showToast(String text){Toast.makeText(this, text, 0).show();}}
获取信息的页面

package cn.itcast.douban;import java.io.IOException;import cn.itcast.douban.util.LoadImageAsynTask;import cn.itcast.douban.util.LoadImageAsynTask.LoadImageAsynTaskCallback;import com.google.gdata.data.Link;import com.google.gdata.data.TextContent;import com.google.gdata.data.douban.UserEntry;import com.google.gdata.util.ServiceException;import android.graphics.Bitmap;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AlphaAnimation;import android.view.animation.AnimationSet;import android.view.animation.ScaleAnimation;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;public class MyInfoActivity extends BaseActivity implements OnClickListener {private TextView tv_name;private TextView tv_location;private TextView tv_info;private ImageView iv_icon;String name ;String location ;String content ;String iconurl;@Overrideprotected void onCreate(Bundle savedInstanceState) {//保证首先设置contentview 然后再去调用父类的方法setContentView(R.layout.my_info);super.onCreate(savedInstanceState);setupView();setListener();fillData();}@Overridepublic void setupView() {tv_name= (TextView) this.findViewById(R.id.txtUserName);tv_location= (TextView) this.findViewById(R.id.txtUserAddress);tv_info= (TextView) this.findViewById(R.id.txtUserDescription);iv_icon = (ImageView)this.findViewById(R.id.imgUser);mRelativeLoading = (RelativeLayout) this.findViewById(R.id.loading);mTextViewTitle = (TextView) this.findViewById(R.id.myTitle);mImageBack = (ImageButton)this.findViewById(R.id.back_button);}@Overridepublic void setListener() {mImageBack.setOnClickListener(this);}@Overridepublic void fillData() {new AsyncTask<Void, Void, Void>() {//onPreExecute 在异步任务执行之前调用的方法 // 运行在主线程里面的 // 初始化ui的操作@Overrideprotected void onPreExecute() {showLoading();super.onPreExecute();}// onPostExecute 在异步任务(后台任务)执行之后调用的方法 // 运行在ui线程中 , // @Overrideprotected void onPostExecute(Void result) {hideLoading();super.onPostExecute(result);tv_info.setText(content);tv_location.setText(location);tv_name.setText(name);//设置用户的头像 LoadImageAsynTask task = new LoadImageAsynTask(new LoadImageAsynTaskCallback() {public void beforeLoadImage() {iv_icon.setImageResource(R.drawable.ic_launcher);}public void afterLoadImage(Bitmap bitmap) {if (bitmap!=null) {iv_icon.setImageBitmap(bitmap);}else{iv_icon.setImageResource(R.drawable.ic_launcher);}}});task.execute(iconurl);}// doInBackground 后台执行的任务 // 方法运行在一个子线程当中 @Overrideprotected Void doInBackground(Void... params) {// 执行耗时的操作 try {UserEntry ue = myService.getAuthorizedUser(); name = ue.getTitle().getPlainText(); location = ue.getLocation(); content = ((TextContent) ue.getContent()).getContent().getPlainText();for (Link link : ue.getLinks()) {if("icon".equals(link.getRel())){iconurl = link.getHref();}}} catch (Exception e) {e.printStackTrace();}return null;}}.execute();}public void onClick(View v) {switch (v.getId()) {case R.id.back_button:finish();break;}}}



原创粉丝点击