編輯:Android開發教程
此案例主要講的是Android控件Gridview(九宮格)完美實現仿支付寶首頁,包含添加和刪除功能;Fragment底部按鈕切換的效果,包含四個模塊,登錄頁面圓形頭像等,一個小項目的初始布局。
效果圖:
自定義GridView
<menu.menudemo.ui.view.ZQScrollGridView android:id="@+id/home_gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#E4E4E4" android:cacheColorHint="#00000000" android:fadingEdge="none" android:horizontalSpacing="1dp" android:listSelector="#00000000" android:numColumns="3" android:scrollbars="none" android:verticalSpacing="1dp" ></menu.menudemo.ui.view.ZQScrollGridView>
/** * 不能滾動的GridView,用於嵌套在如ScrollView之類的可滾動的控件中 */ public class ZQScrollGridView extends GridView { public ZQScrollGridView(Context context){ this(context, null); } public ZQScrollGridView(Context context, AttributeSet attrs){ super(context, attrs); } public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, mExpandSpec); } }
首頁代碼
/** * 首頁 */ public class HomeFragment extends Fragment implements View.OnClickListener { private View view; private TextView tv_sign; private Intent intent; private ZQScrollGridView gridView; private static HomeAdapter adapter; public static List<Integer> listPosition; public static List<UserBean> list; private static ACache aCache; private Context context; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_home, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); view = getView(); aCache = ACache.get(getActivity()); context = getActivity(); initView(); } public void initView() { tv_sign = (TextView) view.findViewById(R.id.home_sign); tv_sign.setOnClickListener(this); gridView = (ZQScrollGridView) view.findViewById(R.id.home_gridview); onLoad(); } public void onLoad() { adapter = new HomeAdapter(getActivity()); gridView.setAdapter(adapter); gridView.setOnItemClickListener(onItemClickListener); gridView.setOnItemLongClickListener(onItemLongClickListener); showData(); } /***** * 刷新數據 */ private void notifyData() { adapter.notifyDataSetChanged(); } @Override public void onClick(View v) { switch (v.getId()) { } } public Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { showLongClick(false); } else if (msg.what == 2)//刪除 { showDelete(); } } }; AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (position == (listPosition.size() - 1)) { intent = new Intent(getActivity(), SearchActivity.class); intent.putExtra("list", (Serializable) listPosition); startActivityForResult(intent, 1); } else { } } }; AdapterView.OnItemLongClickListener onItemLongClickListener = new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { if (position == (listPosition.size() - 1)) { return false; } showLongClick(true); Message msg = new Message(); msg.what = 1;// 刪除 msg.obj = 1; LoginFragmentActivity.handler.sendMessage(msg); return false; } }; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if (resultCode == 1) { if (data.getStringExtra("key").equals("2")) { listPosition.clear(); listPosition = HomeData.POSITION(aCache); notifyData(); } } } } private void showData() { listPosition = HomeData.POSITION(aCache); list = new ArrayList<UserBean>(); for (int i = 0; i < 34; i++) { UserBean user = new UserBean(HomeData.IMG[i], HomeData.TITLE[i], false, false); list.add(user); } notifyData(); } /*** * 刪除 */ private void showDelete() { //刪除緩存 String home = ""; for (int i = 0; i < listPosition.size() - 1; i++) { UserBean userBean = list.get(listPosition.get(i)); if (!userBean.isCheck()) { home += listPosition.get(i) + ","; } } aCache.remove("home"); listPosition.clear(); try { UtilFileDB.ADDFile(aCache, "home", home.substring(0, (home.length() - 1))); if (listPosition == null || listPosition.size() <= 1) { listPosition = HomeData.POSITION(aCache); } } catch (Exception e) { listPosition.add((list.size()-1));//只留加號 } showLongClick(false); } /**** * 重新刷新數據 * * @param isvisibility */ private void showLongClick(boolean isvisibility) { list.clear(); for (int i = 0; i < 34; i++) { UserBean user = new UserBean(HomeData.IMG[i], HomeData.TITLE[i], false, isvisibility); list.add(user); } HomeFragment.list.get(33).setIsvisibility(false); notifyData(); } }
由於代碼太多未完全給出,源碼直接下載即可(點擊下載)
源碼點擊下載
版本: Android Studio 0.5.1因為Android的一些特定功能需要高版本的支持, 當最小SDK版本(minSdkVersion)過低時, 會出現報錯,
Android應用中的Activity指具有屏幕顯示支持用戶交互的基本模塊,類似於Java ME中的MIDlet, Windows 應用中的Form。 比如可以是撥號,發
上節openfire服務器已經搭建完成,這節看看,怎麼使用smack基於xmpp協議登錄openfire服務器,在服務器中創建好測試的賬號下面看android客戶端的代碼
先明確幾個概念的區別:padding margin:都是邊距的含義,關鍵問題得明白是什麼相對什麼的邊距padding:是 控件的內容相對控件的邊緣的邊距.margin &