I have such a task: there is a list. When you click on any item, scrolling the list and clicking is blocked. In addition to this, it is necessary for me that all items except for the click become half transparent. And, when the clicked item performs an action, all items have lost their transparency. I do this:

for(int x=0; x<lv.getCount(); ;x++){ lv_inspiration.getChildAt(x).setAlpha(0.5f); } 

But the game crashed. I added the code for output to the log and realized that the case was cut off in the last items

Attempt to invoke virtual method 'void android.view.View.setAlpha(float)' on a null object refere

I also logged the number of items in the log, it was 2 times less than in reality (the number is always nine, it was 7). What to do? Aytemov 9, and the eighth and ninth seemed to be gone. Do not offer to put transparency on the list, because you need exactly the effect of transparency on all items except the clicked. Help where to dig? I have my own adapter, maybe there is something?

Here is the listener:

 lv_inspiration.setOnItemClickListener(new OnItemClickListener(){ ProgressBar pb; @Override public void onItemClick(AdapterView<?> p1, View p2, final int position, long p4) { for(int x=0; x<lv_inspiration.getCount(); x++){ if(x!=position) lv_inspiration.getChildAt(x).setAlpha(0.5f); } lv_inspiration.setEnabled(false); lv_inspiration.smoothScrollToPosition(position); pb = (ProgressBar)p2.findViewById(R.id.progress_bar_item); pb.setProgress(0); cdt = new CountDownTimer(50000, 200) { @Override public void onTick(long millisUntilFinished) { if(pb.getProgress()!=100) pb.setProgress(pb.getProgress()+1); else onFinish(); } @Override public void onFinish() { cdt.cancel(); for(int x=0; x<lv_inspiration.getCount(); x++){ lv_inspiration.getChildAt(x).setAlpha(1f); } lv_inspiration.setEnabled(true); } }.start(); }}) 
  • There are no Aytems that are not displayed on the screen and it is impossible to get View from them View - pavlofff

2 answers 2

In general, I did. First, I give transparency to all items, then I remove it when I press it.

    Try starting and ending a loop not with zero and the total number of elements, but through

     int startPosition = listView.getFirstVisiblePosition(); int endPosition = listView.getLastVisiblePosition(); 
    • now how If the list does not twist, and immediately click, then everything is fine. When I scroll down the list, all the items that were not visible before the scroll when you clicked, the game crashes with the same logs. - Flippy
    • transparency for elements where is exposed? in onClickListener for the current item? and where is the transparency removed? - Fitz
    • added to the question - Flippy
    • and where getFirstVisiblePosition () and getLastVisiblePosition ()? - Fitz
    • this does not fit. The same logs. In addition, not all items become transparent, but only fully visible. Ie, on those that stick out on some part of the transparency is not put - Flippy