0

I have CustomListView:

public class CustomView extends View {

    private String[] hoursList = getResources().getStringArray(R.array.hours); ;
    private String[] minutesList = getResources().getStringArray(R.array.minutes);;
    private ListView listView;
    private HourAdapter hourAdapter;
    private MinuteAdapter minuteAdapter;
 
    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, ViewGroup viewGroup) {
        super(context);
        inflate(context, R.layout.time_picker_test, viewGroup);

        listView = viewGroup.findViewById(R.id.minutes_list);
        minuteAdapter = new MinuteAdapter(context, minutesList);
        listView.setAdapter(minuteAdapter);

        listView = viewGroup.findViewById(R.id.hours_list);
        hourAdapter = new HourAdapter(context, hoursList);
        listView.setAdapter(hourAdapter);

    }
}

and adapters classes:

public class MinuteAdapter extends BaseAdapter {
    private LayoutInflater lInflater;
    private String[] minutesValueList;

    public MinuteAdapter(Context context, String[] minutesValueList) {
        lInflater = LayoutInflater.from(context);
        this.minutesValueList = minutesValueList;
    }

    @Override
    public int getCount() {
        return minutesValueList.length;
    }

    @Override
    public Object getItem(int position) {
        return minutesValueList[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.row, parent, false);

         TextView textMinutes = view.findViewById(R.id.textRow);
         textMinutes.setText(minutesValueList[position]);

        }

        if (view.getVisibility() == View.VISIBLE) {
            if (position ==2) {
                view.setBackgroundColor(Color.BLUE);
            } else {
                view.setBackgroundColor(Color.WHITE);
            }
        }
        TextView textMinutes = view.findViewById(R.id.textRow);
        textMinutes.setText(minutesValueList[position]);
        return view;
    }
}

I need, that here (see picture) in the middle position (where now 11,2 and PM) always was BLUE color (no matter what element is currently in this position). I used this:

if (view.getVisibility() == View.VISIBLE) {
                if (position ==2) {
                    view.setBackgroundColor(Color.BLUE);
                } else {
                    view.setBackgroundColor(Color.WHITE);
                }
            }

but it is changes element color. But I need that only cell (middle of visible elements) was BLUE always.

Julia
  • 329
  • 7

1 Answers1

1

If you want middle cell of middle row to be blue change if (position ==2) to if (position ==1) , index in array start from zero not from 1

aryan agarwal
  • 989
  • 1
  • 5
  • 15
  • Sorry, maybe my question is not very correctly..... i need , that cell, where now 11,2, PM was always blue. when we do scroll and there are another positions, this cell must be blue. – Julia Aug 06 '20 at 13:02
  • 1
    well I don't know how to do that but if it is no problem then I can suggest you place a blue layout exactly behind where your middle element, and place your listview above that and then you will get your desired result – aryan agarwal Aug 06 '20 at 13:58
  • # aryan agarwal could you explain please? – Julia Aug 06 '20 at 14:05
  • 2
    in your xml layout create a blue container and place your listview above that in that way blue color will remain fixed and your functionality will be as usual, this is just a hack – aryan agarwal Aug 06 '20 at 14:10
  • just want to say place blue layout in xml behind middle element, thereafter no code will be required and you will get your desired output – aryan agarwal Aug 06 '20 at 14:14
  • understand, but can how can I do that text which is in these cell was visible? – Julia Aug 06 '20 at 14:14
  • I don't have your xml code, i will try to solve your problem – aryan agarwal Aug 06 '20 at 14:16
  • oh, I understand what you mean. thank you for your advice. but I have to do this by code. – Julia Aug 06 '20 at 14:19
  • 2
    I believe you can add a scrolllistener to list view get firstvisible+1 position and set its background color to blue , or set it as clickable, see this link https://stackoverflow.com/a/35911911/8719734 – aryan agarwal Aug 06 '20 at 14:27