Android開發中一些常見的問題解決方案
分享一下自己開發中遇到的一些常見問題及解決方案,方面以后快速開發少走彎路,也可以供大家一起學習。
1、開發中很常見的一個問題,項目中的listview不僅僅是簡單的文字,
常常需要自己定義listview,自己的Adapter去繼承BaseAdapter,在adapter中按照需求進行編寫,問題就出現了,
可能會發生點擊每一個item的時候沒有反應,無法獲取的焦點。
原因:
多半是由于在你自己定義的Item中存在諸如ImageButton,Button,CheckBox等子控件(也可以說是Button或者Checkable的子類控件),
此時這些子控件會將焦點獲取到,所以常常當點擊item時變化的是子控件,item本身的點擊沒有響應。
解決方案:
在布局中設置 android:descendantFocusability="blocksDescendants"
2、ScrollView和ListView兩個View都有滾動的效果,在嵌套使用時起沖突的問題
/** * 重新計算ListView的高度,解決ScrollView和ListView兩個View都有滾動的效果,在嵌套使用時起沖突的問題 * @param listView */ public void setListViewHeight(ListView listView) { // 獲取ListView對應的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回數據項的數目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 計算子項View 的寬高 totalHeight += listItem.getMeasuredHeight(); // 統計所有子項的總高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
在scrollview里面嵌套了一個listview ,通過設置一個方法設置了listview的高度 現在的情況就是進到這個界面的時候看到的不是最上面 而是中間 ,該問題的解決辦法為:
mScrollView.smoothScrollTo(0,20);
如此以上代碼還是無效, 在代碼里去掉listview的焦點 lv.setFocusable(false),再試下就可以了;
或者。。。
跟EditText一樣,在父元素的屬性下面下下面這兩行即可,不用代碼設置。親測可用。
android:focusableInTouchMode="true"
android:focusable="true"
3、分享自己的app
/** * 分享一個應用程序 */ private void shareApplication() { // Intent { act=android.intent.action.SEND typ=text/plain flg=0x3000000 cmp=com.android.mms/.ui.ComposeMessageActivity (has extras) } from pid 256 Intent intent = new Intent(); intent.setAction("android.intent.action.SEND"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, "推薦您使用一款軟件,名稱叫:"+appInfo.getName()); startActivity(intent); }
未完待續。。。
浙公網安備 33010602011771號