博客园客户端设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011415782/article/details/49329349

一.主要实现功能:

1.跑马灯文字设计 

2.欢迎界面跳转

3.下拉刷新 

4.方向传感器设计使用(网上的资源,此处用来测试)

 5.本地收藏

二.代码实现简要:

1.设置权限:

 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <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.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

2."首页博客" 实现类代码参考:

public class HomeBlogsView extends Activity {

	private CustomListView lv_homeBlogs;// 自定义ListView
	private static final String TAG = "FindGroupFragment";
	
	private static final int LOAD_DATA_FINISH = 10;// 上拉刷新
	private static final int REFRESH_DATA_FINISH = 11;// 下拉刷新
	//页码索引
	private int PageIndex=1;
	//加载数据条数
	private int dataSize=5;
	private BlogsAdapter homeBlogsAdapter;
	 private ArrayList<Blogs> blogs;
	 private ArrayList<Blogs> dataBlogs;
	 private BlogsAdapter adapter5;
	 private Col_BlogsDao collectionDao;
	 private TextView tv_back,tv_mark;
	 private LinearLayout ll_loadingView;
	 private XmlPulltoParser xpb;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.push_right_in, R.anim.hold);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.home_blogs);
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_mark = (TextView) findViewById(R.id.tv_mark);
		ll_loadingView=(LinearLayout) findViewById(R.id.loading);
		ll_loadingView.setVisibility(View.VISIBLE);
		lv_homeBlogs = (CustomListView) findViewById(R.id.lv_homeBlogs);
		collectionDao=new Col_BlogsDao(this);
		xpb=new XmlPulltoParser();
		blogs=new ArrayList<Blogs>();
		loadData(-1);
		lv_homeBlogs.setAdapter(homeBlogsAdapter);
		lv_homeBlogs.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//很有意思的情况,点击的item 调取下一item的信息
				lv_BlogsToCentent(view,position-1,blogs);
			}

			private void lv_BlogsToCentent(View view, int position,ArrayList<Blogs> blog) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putSerializable("BlogBean", blog.get(position));
				intent.putExtras(bundle);
				
				intent.setClass(HomeBlogsView.this, BlogsContentActivity.class);
				startActivity(intent);
			}
		});
		lv_homeBlogs.setOnRefreshListener(new CustomListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				// TODO 下拉刷新
				Log.e(TAG, "onRefresh");
				loadData(0);
			}
		});
		lv_homeBlogs.setOnLoadListener(new CustomListView.OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				// TODO 加载更多
				Log.e(TAG, "onLoad");
				loadData(1);
			}
		});
		// //关闭下拉刷新
		// GroupList.setCanRefresh(!GroupList.isCanRefresh());
		// //关闭上拉刷新
		// GroupList.setCanLoadMore(!GroupList.isCanLoadMore());
		tv_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.hold,R.anim.push_right_out);
			}
		});
	}
	/*
	 * 上下拉刷新加载数据方法
	 */
	public void loadData(final int updateTag) {
		new Thread() {
			@Override
			public void run() {
				switch (updateTag) {
				case -1:
				case  0:// 这里是刷新数据
					PageIndex =1;
					if (NetUtil.isNetOk(HomeBlogsView.this)) {
						try {
							URL url = new URL(
									"http://wcf.open.cnblogs.com/blog/sitehome/paged/"+PageIndex+"/7");
							URLConnection connection5 = url.openConnection();
							InputStream inputStream = connection5.getInputStream();
							blogs.clear();
							dataBlogs = xpb.ParseBlogs(inputStream); 

							
							for (Blogs hotnew : dataBlogs) {
								System.out.println("---" + hotnew.getSummary());
								System.out.println(hotnew.getId());
								System.out.println("---" + hotnew.getTitle());
							}
							blogs.addAll(dataBlogs);
							
						} catch (MalformedURLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (Throwable e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					break;

				case 1:
					// 这里是上拉刷新
					PageIndex ++;
					
					if (NetUtil.isNetOk(HomeBlogsView.this)) {
						try {
							URL url = new URL(
									"http://wcf.open.cnblogs.com/blog/sitehome/paged/"+PageIndex+"/5");
							URLConnection connection5 = url.openConnection();
							InputStream inputStream = connection5.getInputStream();

							dataBlogs = xpb.ParseBlogs(inputStream); 
						} catch (MalformedURLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (Throwable e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					break;
				}

				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				if (updateTag == 0) {// 下拉刷新
					// 通知Handler
					handler.sendEmptyMessage(REFRESH_DATA_FINISH);
				} else if (updateTag == 1) {// 上拉刷新
					// 通知Handler
					handler.sendEmptyMessage(LOAD_DATA_FINISH);
				}else if (updateTag==-1) {
					Message msg = handler.obtainMessage();
					msg.obj = dataBlogs;
					msg.what = 2;
					handler.sendMessage(msg);
				}
			}
		}.start();
	}
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 2:
				homeBlogsAdapter = new BlogsAdapter(HomeBlogsView.this,
						blogs);
				lv_homeBlogs.setAdapter(homeBlogsAdapter);
				if (blogs.size() != 0) {
					ll_loadingView.setVisibility(View.GONE);
				}
				break;
			case REFRESH_DATA_FINISH:
				
				homeBlogsAdapter = new BlogsAdapter(HomeBlogsView.this,
						blogs);
				lv_homeBlogs.setAdapter(homeBlogsAdapter);
				if (blogs.size() != 0) {
					ll_loadingView.setVisibility(View.GONE);
				}
				if (homeBlogsAdapter != null) {
					homeBlogsAdapter.notifyDataSetChanged();
				}
				lv_homeBlogs.onRefreshComplete(); // 下拉刷新完成
				break;
			case LOAD_DATA_FINISH:
				Log.d("mo", "LOAD_DATA_FINISH");
				if (homeBlogsAdapter != null) {
					blogs.addAll(dataBlogs);
					homeBlogsAdapter.notifyDataSetChanged();
				}
				lv_homeBlogs.onLoadMoreComplete(); // 加载更多完成
				break;
			default:
				break;
			}
		}

	};
	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
		if (keyCode==KeyEvent.KEYCODE_BACK) {
			finish();
			overridePendingTransition(R.anim.hold,R.anim.push_right_out);
			}
	return false;
}
}

3.BlogAdapter,适配博客信息:

public class BlogsAdapter extends BaseAdapter{
	
	private Context context;
	private LayoutInflater inflater;
	private ArrayList<Blogs> blogs=new ArrayList<Blogs>();
	
	private RequestQueue mQueue;
    private ImageLoader mImageLoader;
    private ImageListener listener;
	public BlogsAdapter(Context context,ArrayList<Blogs> Blogs) {
		
		this.context=context;
		this.blogs=Blogs;
		inflater = LayoutInflater.from(context);
		mQueue = Volley.newRequestQueue(context);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return blogs.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder hodler;
		if (convertView==null) {
			
			//inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=inflater.inflate(R.layout.item_blogs, null);
			hodler=new ViewHolder();
			hodler.tv_Title=(TextView) convertView.findViewById(R.id.tv_newTitle);
			hodler.tv_name=(TextView) convertView.findViewById(R.id.tv_name);
			hodler.tv_uri=(TextView) convertView.findViewById(R.id.tv_uri);
			hodler.iv_avatar=(ImageView) convertView.findViewById(R.id.iv_avatar);
			hodler.tv_diggs=(TextView) convertView.findViewById(R.id.tv_diggs);
			hodler.tv_views=(TextView) convertView.findViewById(R.id.tv_views);
			hodler.tv_comments=(TextView) convertView.findViewById(R.id.tv_comments);
			hodler.tv_summary=(TextView) convertView.findViewById(R.id.tv_summary);
			hodler.tv_updated=(TextView) convertView.findViewById(R.id.tv_updated);
			hodler.tv_postid=(TextView) convertView.findViewById(R.id.tv_blogID);
			convertView.setTag(hodler);
		}else {
			hodler=(ViewHolder) convertView.getTag();
			
		}
		hodler.iv_avatar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "准备添加收藏!", Toast.LENGTH_SHORT).show();
			}
		});
		String Pub_Date = blogs.get(position).getPublished().substring(0,10);
		String Pub_Time = blogs.get(position).getPublished().substring(11,19);
		
		hodler.tv_Title.setText(blogs.get(position).getTitle());
		hodler.tv_summary.setText("     "+blogs.get(position).getSummary());
		
		hodler.tv_name.setText("博主:"+blogs.get(position).getAuthorName());
		hodler.tv_uri.setText("主页:"+blogs.get(position).getAuthorUri());
		hodler.tv_diggs.setText(String.valueOf(blogs.get(position).getDiggs()));
		hodler.tv_comments.setText(String.valueOf(blogs.get(position).getComments()));
		hodler.tv_views.setText(String.valueOf(blogs.get(position).getViews()));
		hodler.tv_postid.setText(String.valueOf(blogs.get(position).getId()));
		hodler.tv_updated.setText("时间:"+Pub_Date+"\t\t"+Pub_Time);
		
		String imgAvaterUrl=blogs.get(position).getAuthorAvater();
		 listener = ImageLoader.getImageListener(hodler.iv_avatar, R.drawable.icon3,  R.drawable.icon3);
		 //根据博主头像存在与否进行 图片加载判断
		 if (imgAvaterUrl==null ||imgAvaterUrl.equals("")) {
				hodler.iv_avatar.setImageResource(R.drawable.icon3);;
			}else {
				mImageLoader.get(imgAvaterUrl, listener);
			}
		
		return convertView;
	}

	class ViewHolder{
		
		TextView tv_Title,tv_published,
						tv_sourceName,tv_summary,tv_views,tv_comments,tv_diggs,tv_postid,
						tv_name,tv_uri,tv_updated;
		ImageView iv_avatar;
	}
}

4.博客信息解析类代码参考:

/**
	 * 推荐博客的Pull解析方法
	 * @param inputStream
	 * @return
	 */
	public static ArrayList<Blogs> ParseBlogs(InputStream inputStream) {
		
		ArrayList<Blogs> blogList = null;
		Blogs blog = null;
		boolean isEntry=false;
		XmlPullParser parser=Xml.newPullParser();
		try {
			parser.setInput(inputStream, "UTF-8");
			int eventType=parser.getEventType();
			
			while(eventType!=XmlPullParser.END_DOCUMENT){
			
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					blogList=new ArrayList<Blogs>();
					break;
				case XmlPullParser.START_TAG:
					if(parser.getName().equals("entry")){  
						isEntry=true;
						blog=new Blogs();
					}else if(parser.getName().equals("id") && isEntry){
						blog.setId(parser.nextText());	
					}else if(parser.getName().equals("title") && isEntry){
						blog.setTitle(parser.nextText());	
					}else if(parser.getName().equals("summary") && isEntry){
						blog.setSummary(parser.nextText());			
					}else if(parser.getName().equals("published") && isEntry){
						blog.setPublished(parser.nextText());	
					}else if(parser.getName().equals("updated") && isEntry){
						String[] t=parser.nextText().split("T");
						blog.setUpdated(t[0]+t[1]);
					}else if(parser.getName().equals("views") && isEntry){
						blog.setViews(Integer.parseInt(parser.nextText()));
					}else if(parser.getName().equals("comments") && isEntry){
						blog.setComments(Integer.parseInt(parser.nextText()));
					}else if(parser.getName().equals("diggs") && isEntry){
						blog.setDiggs(Integer.parseInt(parser.nextText()));
					}else if(parser.getName().equals("name") && isEntry){
						blog.setAuthorName(parser.nextText());
					}
					else if(parser.getName().equals("uri") && isEntry){
						blog.setAuthorUri(parser.nextText());
					}
					else if(parser.getName().equals("avatar") && isEntry){
						blog.setAuthorAvater(parser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:
					if(parser.getName().equals("entry") && blog!=null){
						blogList.add(blog);
						blog=null;
					}
					break;
				default:
					break;
				}
				parser.next();
				eventType=parser.getEventType();
			}
			inputStream.close();		
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return blogList;
	}

5.提示:毕竟项目所涉及的代码太多,相信拥有Android基础的你一定看的懂,再次不做赘述,希望能给你带来帮助。

三.项目运行截图

1.微博首页加载及其显示,可进行下拉刷新

2.搜索博主页面,进行列表显示

3.下拉刷新加载博主的博文信息
4.方向传感器是的使用
5.收藏书签
6.项目退出

四.源代码提供

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区

领取腾讯云代金券