Android之記錄ListView標記行的狀態

來源:互聯網
上載者:User

小Demo無什麼特別之處,最特別的就是盡量少用notifyDataSetChanged,開銷太大了,當然使用是會省不少工的,不過有時候還是會遇到別的問題的,項目經驗表示會有這個可能性的,廢話不多少了,直接上關鍵代碼。

@Override    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long arg3) {                /**         * 在ListView中,使用getChildAt(index)的取值,只能是當前可見地區(列表可滾動)的子項!        1、所以如果想擷取前部的將會出現返回Null值問題;        2、getChildCount跟getCount擷取的值將會不一樣(數量多時);        3、如果使用了getChildAt(index).findViewById(...)設定值的話,滾動列表時值就會改變了。               需要使用getFirstVisiblePosition()獲得第一個可見的位置,在用當前的position-getFirstVisiblePosition(),再用getChildAt取值!         * */        int now_pos = pos - adapterView.getFirstVisiblePosition();        View v = adapterView.getChildAt(now_pos);        ImageView imageView = (ImageView) v.findViewById(R.id.image);                if (imageView.getVisibility() == View.VISIBLE) {            imageView.setVisibility(View.GONE);            adapter.setState(pos,NONBIAOZHI);        }else{            imageView.setVisibility(View.VISIBLE);            adapter.setState(pos,BIAOZHI);            // getView調用,不過非常耗費效能//            adapter.notifyDataSetChanged();            // 不使用notifyDataSetChanged,getView不會重新整理,提高效率            refreshListView(pos, imageView);        }    }

 

public void refreshListView(int pos, ImageView imageView) {        if(pre != pos && !hasmap.isEmpty()){            ImageView image = (ImageView) hasmap.get(pre);            image.setVisibility(View.GONE);            hasmap.remove(pre);        }        pre = pos;        hasmap.put(pre, imageView);    }

上面的是在Activity的方法片段,下面的是BaseAdapter的方法片段:

    // 設置標記    public void setState(int pos,int state){        if(state == MainActivity.BIAOZHI){            // 每次點擊都清空列表,保持唯一選擇性            map.clear();            map.put(pos, 1);        }else{            map.remove(pos);        }    }        // 檢測標記    public boolean getState(int p){        if (!map.isEmpty() && map.containsKey(p)) {            return true;        }else            return false;    }=============================getView代碼:        // 固定顯示標記的行        if (getState(position)) {            holder.imageView.setVisibility(View.VISIBLE);        }else            holder.imageView.setVisibility(View.GONE);

附上項目下載,自己運行就會得到標題的效果。ListViewClickTest.zip

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.