Difference between physical and programmatically click on ListView

93 Views Asked by At

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.

2

There are 2 best solutions below

1
On

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.

1
On

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);