八卦頭條 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風格,低版本不能使用高版本的
主題,解決方案網上也有,不折騰了,幹脆不支援低版本吧!!!
好了,大緻的功能介紹如此。