Android ListView 相關問題(面試常用)

來源:互聯網
上載者:User

標籤:

今日,看到群裡朋友發的一部分面試題,決定把這這些面試題的答案寫下來,如下:

1、ListView怎麼和ScrollView相容? ok
2、ViewPager無限輪播圖片
3、out of memory記憶體溢出怎麼解決
4、三級緩衝如何?
5、登入時怎麼儲存使用者名稱密碼實現下次自動登入
6、如果sp只儲存使用者名稱,比如三個使用者都存在sp裡,取出來怎麼取?存進去怎麼存?你怎麼區分
7、你們登入就只有登入成功和登入失敗嗎?難道沒有重連機制?斷網了以後又有網了重新來到登入介面怎麼登入?
8、怎麼保持線上狀態
9、toobar,actionbar玩的轉不?
10、include標籤總會吧?
11、webview配置會吧
12、CoordinatorLayout和recycleview會吧
13、tab用actionbar就能實現你知道嗎?
14、你知道Listview裡有Button就點不動了你知道嗎 ok
15、簡單的動畫你會嗎?哪怕是運用到activity的出現與退出
16、view的點擊事件跟dialog的點擊事件很容易引錯包
17、集合List<>為什麼動態增長?它有預設長度的,有時候用他步入指定好長度的數組
18、下載時,非同步任務和子線程他倆的區別
19、recycleview代替listview,gridview,瀑布流三種模式切換自如
20、listview怎麼最佳化 ok
21、fragment的倆個適配器的區別
22、MVC,MVP
23、註解?findviewbyid
24、socket,http,xmpp,rstp這些協議
25、oom記憶體流失

今天決定先寫ListView相關的,我把上述的1、14、20放到一起來說,也就是本篇文章,其他的稍後有時間持續更新,大家如果對此感興趣的話,請關注我,我們一起學習。

================================================

ListView怎麼和ScrollView相容?

我們知道,有些時候我們需要在ListView外層嵌套一層ScrollView,代碼如下:

    <ScrollView        android:id="@+id/scrollview"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <ListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="wrap_content"></ListView>    </ScrollView>

只要稍微有點經驗的人都知道這是會出現什麼問題,沒錯,就是“Listview不能顯示正常的條目,只顯示一條或二條”,這是怎麼回事呢?這是因為:由於listView在scrollView中無法正確計算它的大小, 故只顯示一行。
當目前為止,我知道的針對這一問題的解決辦法有:

1. 方法一:重寫ListView, 覆蓋onMeasure()方法
activity_list_view_scroll_view_test.xml:<merge xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.art.demo.ListViewScrollViewTestActivity">    <ScrollView        android:id="@+id/scrollview"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <com.art.demo.WrapperListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="wrap_content"/>    </ScrollView></merge>WrapperListView.java:public class WrapperListView extends ListView {    public WrapperListView(Context context) {        super(context);    }    public WrapperListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public WrapperListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public WrapperListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }    /**     * 重寫該方法,達到使ListView適應ScrollView的效果     */    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }}ListViewScrollViewTestActivity.java:public class ListViewScrollViewTestActivity extends AppCompatActivity {    private ScrollView scrollView;    private WrapperListView listView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_list_view_scroll_view_test);        scrollView = (ScrollView) findViewById(R.id.scrollView);        listView = (WrapperListView) findViewById(R.id.listview);        initListVeiw();    }    private void initListVeiw() {        List<String> list = new ArrayList<>();        for (int i = 0; i < 20; i++) {            list.add("第 " + i + " 條");        }        listView.setAdapter(new ArrayAdapter<String>(this,                android.R.layout.simple_list_item_1, list));    }}另外,哪位大神可以告訴我在代碼塊(```)中,怎麼給某一行加粗,或者做一些其他明顯標記??????????????
2. 方法二:動態設定listview的高度,不需要重寫ListView

只需要在setAdapter之後調用如下方法即可:

public void setListViewHeightBasedOnChildren(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);            // 計運算元項View 的寬高            listItem.measure(0, 0);            // 統計所有子項的總高度            totalHeight += listItem.getMeasuredHeight();        }        ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));        // listView.getDividerHeight()擷取子項間分隔字元佔用的高度        // params.height最後得到整個ListView完整顯示需要的高度        listView.setLayoutParams(params);    }

另外,這時,這時最好給ListView之外嵌套一層LinearLayout,不然有時候這種方法會失效,如下:

<merge xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.art.demo.ListViewScrollViewTestActivity">    <ScrollView        android:id="@+id/scrollview"        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent">            <ListView                android:id="@+id/listview"                android:layout_width="fill_parent"                android:layout_height="match_parent"                android:background="#FFF4F4F4"                android:dividerHeight="0.0dip"                android:fadingEdge="vertical" />        </LinearLayout>    </ScrollView></merge>
3. 方法三:在xml檔案中,直接將Listview的高度寫死

可以確定的是:這種方式可以改變ListView的高度,但是,還有一個嚴重的問題就是listview的資料是可變動的,除非你能正確的寫出listview的高度,否則這種方式就是個雞肋。
如下:

<ScrollView        android:id="@+id/scrollview"        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent">            <ListView                android:id="@+id/listview"                android:layout_width="fill_parent"                android:layout_height="300dip"                android:background="#FFF4F4F4"                android:dividerHeight="0.0dip"                android:fadingEdge="vertical" />        </LinearLayout>    </ScrollView>
4. 方法四:

某些情況下,其實我們可以完全避免ScrollView嵌套Listview,比如使用listview的addHeader() 函數來實現預期效果或者利用布局的特性達到預期效果,當然,具體怎麼用,只有在開發中慢慢琢磨,慢慢總結了.

至此,關於“ListView怎麼和ScrollView相容”這個問題就算是回答完了,如果有不明白的地方可以問我,同樣,那裡有錯誤也歡迎大家指出,真的不勝感激。

接下來要說的就是!!!!!

listview怎麼最佳化?

關於Listview的最佳化,只要面試過的人,我相信都對這個題很熟悉,不管有沒有人問過你這個題,我想你自己也一定準備過,否則,嘿嘿!!!!!而且網上也一搜一大把這裡就簡單提幾個主要的:
1)、convertView複用,對convetView進行判空,當convertView不為空白時重複使用,為空白則初始化,從而減少了很多不必要的View的建立
2)定義一個ViewHolder,封裝Listview Item條目中所有的組件,將convetView的tag設定為ViewHolder,不為空白時通過ViewHolder的屬性擷取對應組件即可
3)、當ListView載入資料量較大時可以採用分頁載入和圖片非同步載入(關於Listview分頁載入和圖片非同步載入思路請看接下來的文章內容)

下面就是關於Listview的一些相關拓展

1. 開啟套有 ListVew的 ScrollView的頁面配置 預設 起始位置不是最頂部?

解決辦法有兩種:
方法一:把套在裡面的ListVew 不讓擷取焦點即可。listview.setFocusable(false);注意:在xml布局裡面設定android:focusable=“false”不生效
方法二:myScrollView.smoothScrollTo(0,0);

2. 上拉載入和下拉重新整理怎麼實現?

實現OnScrollListener 介面重寫onScrollStateChanged 和onScroll方法,
使用onscroll方法實現”滑動“後處理檢查是否還有新的記錄,如果有,調用 addFooterView,添加記錄到adapter, adapter調notifyDataSetChanged 更新資料;如果沒有記錄了,把自訂的mFooterView去掉。使用onScrollStateChanged可以檢測是否滾到最後一行且停止滾動然後執行載入

3. listview失去焦點怎麼處理?

在listview子布局裡面寫,可以解決焦點失去的問題
android:descendantFocusability=”blocksDescendants”

4. ListView圖片非同步載入實現思路?

1.先從記憶體緩衝中擷取圖片顯示(記憶體緩衝)
2.擷取不到的話從SD卡裡擷取(SD卡緩衝,,從SD卡擷取圖片是放在子線程裡執行的,否則快速滑屏的話會不夠流暢)
3.都擷取不到的話從網路下載圖片並儲存到SD卡同時加入記憶體並顯示(視情況看是否要顯示)

5. 你知道Listview裡有Button就點不動了你知道嗎?

原因是button強制擷取了item的焦點,只要設定button的focusable為false即可。

6. 如何自訂一個Adapter(有興趣的可以看一下,大家不呀扔我雞蛋)

繼承自BaseAdapter實現裡面的方法,listView在開始繪製的時候,系統首先調用getCount()函數,根據他的傳回值得到listView的長度,然後根據這個長度,調用getView()逐一繪製每一行。如果你的getCount()傳回值是0的話,列表將不顯示同樣return 1,就只顯示一行。系統顯示列表時,首先執行個體化一個適配器(這裡將執行個體化自訂的適配器)。當手動完成適配時,必 須手動映射資料,這需要重寫getView()方法。系統在繪製列表的每一行的時候將調用此方法。getView()有三個參數,position表示將顯示的是第幾行,covertView是從布局檔案中inflate來的 布局。我們用LayoutInflater的方法將定義好的main.xml檔案提取成View執行個體用來顯示。
然後 將xml檔案中的各個組件執行個體化(簡單的findViewById()方法)。這樣便可以將資料對應到各個組件上了。但是按鈕為了響應點擊事件,需要為它添加點擊監聽器,這樣就能捕獲點擊事件。至此一個自定 義的listView就完成了,現在讓我們回過頭從新審視這個過程。系統要繪製ListView了,
他首先獲得 要繪製的這個列表的長度,然後開始繪製第一行,怎麼繪製呢?
調用getView()函數。在這個函數裡面 首先獲得一個View(實際上是一個ViewGroup),然後再執行個體並設定各個組件,顯示之。好了,繪製完這一行了。那 再繪製下一行,直到繪完為止。在實際的運行過程中會發現listView的每一行沒有焦點了,這是因為Button搶奪了listView的焦點,只要布局檔案中將Button設定為沒有焦點就OK了。

7. listview分頁載入的步驟?

通常實現分頁載入有兩種方式,一種是在ListView底部設定一個按鈕,使用者點擊即載入。另一種是當使用者滑動到底部時自動載入。
在ListView底部設定一個按鈕,使用者點擊即載入實現思路:

        // 加上底部View,注意要放在setAdapter方法前        ListView.addFooterView(moreView);        bt.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                pg.setVisibility(View.VISIBLE);// 將進度條可見                bt.setVisibility(View.GONE);// 按鈕不可見                handler.postDelayed(new Runnable() {                    @Override                    public void run() {                        loadMoreDate();// 載入更多資料                        bt.setVisibility(View.VISIBLE);                        pg.setVisibility(View.GONE);                        mSimpleAdapter.notifyDataSetChanged();// 通知listView重新整理資料                    }                }, 2000);            }        });

當使用者滑動到底部時自動載入實現思路:
實現OnScrollListener 介面重寫onScrollStateChanged 和onScroll方法,使用onscroll方法實現”滑動“後處理檢查是否還有新的記錄,如果有,添加記錄到adapter, adapter調用 notifyDataSetChanged 更新資料;如果沒有記錄了,則不再載入資料。使用onScrollStateChanged可以檢測是否滾到最後一行且停止滾動然後執行載入.

8. ViewHolder內部類非得要聲明成static的呢?

這不是Android的最佳化,而是Java提倡的最佳化,
如果聲明成員類不要求訪問外圍執行個體,就要始終把static修飾符放在它的聲明中,使它成為靜態成員類,而不是非靜態成員類。
因為非靜態成員類的執行個體會包含一個額外的指向外圍對象的引用,儲存這份引用要消耗時間和空間,並且導致外圍類執行個體符合記憶體回收時仍然被保留。如果沒有外圍執行個體的情況下,也需要分配執行個體,就不能使用非靜態成員類,因為非靜態成員類的執行個體必須要有一個外圍執行個體。

9. Listview每個item有特效進入視圖10. ScrollView、ListView剖析 - 上下展開回彈阻尼效果11. 自訂控制項-下拉重新整理和上拉載入的listView

================================================

前邊還有幾篇關於面試的文章,有興趣的小夥伴可以去我的專題裡溜達溜達,另外我所有的簡書文章目錄在這裡

更多內容請關注 我的專題
轉載請註明 原文連結:
http://www.jianshu.com/p/b7741023bc6f

Android ListView 相關問題(面試常用)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.