android網絡編程---“八卦頭條”app開發總結

八卦頭條 git代碼下載:
https://github.com/vae260772/TopNews

最近寫了個app,做個總結。

實現設計到的技術
1)Activity+fragment的 數據傳遞
2)viewpager+fragment的結合使用,包括fragment的設置緩存數據頁面狀態,fragment動態添加/刪除
添加可以選擇支持的分類,這些分類是由服務器控制的,支持多選。
刪除可以選擇除當前界面的其他fragment,支持多選

3)actionbar的使用,其中設計到sdk api的版本,是否兼容低版本的手機問題,android.app.ActionBar、
android.support.v7.widget.ActionBar,
樣式theme設置,
bar中設置menu菜單點擊問題。
4)頂部切換的導航欄,使用第三方庫。viewpager指示器:ViewPagerIndicator-library
滾動的banner使用了,cn.bingoogolapple.bgabanner.BGABanner
,其中banner中添加的網絡圖片加載使用了
com.bumptech.glide.Glide

5)fragment中主頁使用recycleview顯示。
下拉刷新的實現使用了第三方com.handmark.pulltorefresh.library.PullToRefreshBase,
但是由於第三方不支持recycleview顯示數據,所以在第三方的代碼基礎上,擴展了刷新功能,支持
recycleview的顯示。
pullToRefreshrecycleview = (PullToRefreshRecyclerView) view.findViewById(R.id.listView);
recycleview = pullToRefreshrecycleview.getRefreshableView();

6)我的新聞相關數據使用了
https://www.juhe.cn/docs/api/id/235
聚合數據新聞頭條。
在首次啓動應用時候,會自動從服務器獲取數據,解析顯示。之後用戶可以自己刷新加載最新新聞。
從服務器獲取數據:
使用Retrofit2.0
Retrofit retrofit = new Retrofit.Builder().
baseUrl(“http://v.juhe.cn/“).
addConverterFactory(GsonConverterFactory.create()).build();
call.enqueue(new Callback() {…}
獲取服務器返回結果,
失敗,通過handler+toast給出錯誤提示
成功,org.json.JSONObject進行解析。

7)RecyclerView.Adapter
涉及到item重用。
其中item中的圖片顯示使用了
com.nostra13.universalimageloader.core.ImageLoader
adapter中設置item的點擊事件
((CustomViewHolder) viewholder).list_item.setOnLongClickListener(new CustomListener(data));

長按彈出對話框
Utils.showOperateDialog(context, data, null);

可以點擊收藏、在瀏覽器打開、分享給別人。
收藏:

收藏功能把數據保存在數據庫中SQLiteOpenHelper
DBUtils db = new DBUtils(context);
db.insert(data);

在瀏覽器打開:
通過網址打開
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);

分享:
簡單的使用了系統分享:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(“text/plain”);
intent.putExtra(Intent.EXTRA_SUBJECT, “分享”);
intent.putExtra(Intent.EXTRA_TEXT,
“我正在瀏覽這條新聞,覺得真不錯,推薦給你哦~地址點這裏:\n” + data.getUrl());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, “share”));
效果

以上是主頁涉及到的技術。

8)向右側滑彈出menu。SlidingMenu
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
// 設置觸摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

// 設置滑動菜單視圖的寬度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 設置漸入漸出效果的值
menu.setFadeEnabled(false)
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//爲側滑菜單設置佈局
menu.setMenu(R.layout.left_menu);

實現側滑菜單。節省空間。這裏設置setBehindOffsetRes參數比較麻煩。
9)側滑驗證碼登錄實現。
登錄驗證碼這個使用的三方支持
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
這個需要用戶自己去註冊審覈,纔可以真正免費,否則一天測試的短信有限制!!!
地址申請:
http://www.mob.com/
通過三方的sdk獲取驗證碼,驗證通過後,我把登錄的用戶手機號記錄下來
保存在SharedPreferences 中,
用戶註銷登錄,在把登錄的用戶設置爲空。
這裏登錄沒有實質性的作用,只是實現一個小功能而已。
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>

android:id=”@+id/email”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:hint=”@string/prompt_email”
android:inputType=”phone”
android:maxLines=”1”
android:singleLine=”true” />

其中登錄界面有個動畫文字提示效果TextInputLayout
10)查看收藏
數據顯示使用了

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
最終是使用listview
之所以使用PullToRefreshListView是想做一個刷新加載其他收藏的新聞。
默認顯示4條條新聞,如果還有收藏的,每次上拉、下拉刷新加載一條。

actionbar中支持搜索關鍵字查找收藏的新聞。
menu中隱藏action全屏顯示功能,下拉刷新時候可以再次顯示出來。

使收藏和主頁界面主題風格一致。
使用了Theme.Holo風格。
注意:如果你的Activity繼承actionbaractivity,那你就不能使用Theme.Holo風格,低版本不能使用高版本的
主題,解決方案網上也有,不折騰了,乾脆不支持低版本吧!!!

好了,大致的功能介紹如此。
這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述
這裏寫圖片描述

這裏寫圖片描述這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章