It is necessary that when you click on the Item ListView , the alpha channel value of all other Item 's is set to 0.5 . I tried to implement it through a cycle:

 for (int a = 0; a < lv.getChildCount(); a++) { lv.getChildAt(a).setAlpha(0.5f); } 

But when you click on Item application crashes with the log:

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

That is: with those Item 's that are not visible on the screen, nothing can be done (there are none).

How then to implement this?

    2 answers 2

    To solve such problems came up with an adapter =)

     public class SomeAdapter extends BaseAdapter { LayoutInflater layoutInflater; ArrayList<SomeClass> myList; private class ViewHolder{ TextView tv1; ImageView iv1; } public SomeAdapter(Context mCon, ArrayList<SomeClass> list){ this.myList = list; layoutInflater = LayoutInflater.from(mCon); this.context = mCon; } @Override public int getCount() { return myList.size(); } @Override public ProfileEntry getItem(int position) { return myList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null){ holder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.row_profile,null); //находим элементы в разметке, но не меняем их holder.tv1 = (TextView)convertView.findViewById(R.id.tv_profileRow1); holder.iv1 = (ImageView) convertView.findViewById(R.id.iv_profileRow1); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } //тут назначаем свойства разметке в зависимости от данных if (myList.get(position).getSomeMethod().equals("Что-то")){ // прописываем какой-то текст holder.tv1.setText("Укажите город"); //устанавливаем картинку int picId = context.getResources().getIdentifier(myList.get(position).getImage(),"drawable",context.getPackageName()); holder.iv1.setImageResource(picId); }else{ holder.tv1.setText(myList.get(position).getAnotherMethod()); int picId = context.getResources().getIdentifier(myList.get(position).getImage(),"drawable",context.getPackageName()); holder.iv1.setImageResource(picId); } 

      Do not try to change the list layout elements not from the adapter.

      1. Enter an int in the adapter to store the position of the element that was clicked.
      2. When you click on an element, set the value of this variable.
      3. In getView , depending on whether the current position matches the value of this variable or not, set the transparency of the element's markup.

      In adapter class:

       private int selectedPosition = -1; @Override public View getView(int position, View convertView, ViewGroup parent) { ... if(position==selectedPosition) {//делаем непрозрачным} else{//делаем прозрачным} //тут вешаем слушатель нажатий где меняем значение selectedPosition } 
      • Can I have a rough code? - Flippy
      • @ SergeyGrushin, wrote something. - Yuriy SPb
      • Strangely, the OnItemClickListener not running. I need him. - Flippy
      • @ SergeyGrushin, show in the code how you set it. - Ivan Vovk
      • one
        @IvanVovk, yes, for about 2 years I’m not touching the ListView and I ’m happy) - Yuriy SPb