0

I am clicking ListView item programmatically on onCreate method like below:

    // setting listener for ListView item click
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                changeView(i);
            }
        });

    // Clicking ListView first position programmatically.
    listview.performItemClick(listview.getChildAt(0), 0,
                listview.getAdapter().getItemId(0));

In above code when i debug the code changeView(i); is calling properly for both programmatic and physical click.

I am using changeView() to change colour of ListView item views (TextView and ImageView) like below:

private void changeView(int position) {
        for (int i = 0; i < adapter.getCount(); i++) {
            View v = getViewByPosition(i, listview);
            ImageView imgIcon = (ImageView) v.findViewById(R.id.icon);
            TextView txtTitle = (TextView) v.findViewById(R.id.title);
            if (i == position) {
                imgIcon.setColorFilter(ContextCompat.getColor(mActivity, R.color.app_red));
                txtTitle.setTextColor(ContextCompat.getColor(mActivity, R.color.app_red));
            } else {
                imgIcon.setColorFilter(ContextCompat.getColor(mActivity, R.color.colorWhite));
                txtTitle.setTextColor(ContextCompat.getColor(mActivity, R.color.colorWhite));
            }
        }

        lvSlideMenu.setItemChecked(position, true);
        lvSlideMenu.setSelection(position);
    }

Where getViewByPosition() method is used to get view of ListView item:

public View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }

PROBLEM : Above code is changing colour of TextView and ImageView of list item when i click physically (by hand) but not programmatically.

Thanks in advance.

Viks
  • 1,510
  • 4
  • 22
  • 50

2 Answers2

1

Try sending view in chnageView()

  changeView(view);

And in chnageView()

ImageView imgIcon = (ImageView) v.findViewById(R.id.icon);
        TextView txtTitle = (TextView) v.findViewById(R.id.title);
Dharmita bhatt
  • 475
  • 4
  • 16
  • v is view passed in changeview(view) method. remove for (int i = 0; i < adapter.getCount(); i++) { View v = getViewByPosition(i, listview); – Dharmita bhatt Jan 05 '17 at 09:00
0

Analyzing the code, I can deduce that, when you are changing color programmatically and changeView() is called, your color change code part is not executed :

for (int i = 0; i < adapter.getCount(); i++) {
            View v = getViewByPosition(i, listview);
            ImageView imgIcon = (ImageView) v.findViewById(R.id.icon);
            TextView txtTitle = (TextView) v.findViewById(R.id.title);
            if (i == position) {
                imgIcon.setColorFilter(ContextCompat.getColor(mActivity, R.color.app_red));
                txtTitle.setTextColor(ContextCompat.getColor(mActivity, R.color.app_red));
            } else {
                imgIcon.setColorFilter(ContextCompat.getColor(mActivity, R.color.colorWhite));
                txtTitle.setTextColor(ContextCompat.getColor(mActivity, R.color.colorWhite));
            }
        }

Can you please check if for block is executed.

Nargis
  • 4,687
  • 1
  • 28
  • 45
  • i set setOnItemClickListener before programmatically click on item and this is calling colour changing code. I debug to see if the code is called or not and its calling. – Viks Jan 05 '17 at 06:33