物理和编程之间的区别点击ListView

时间:2017-01-05 05:11:23

标签: android listview onitemclick

我在ListView方法上以编程方式点击onCreate项,如下所示:

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

在上面的代码中,当我调试代码changeView(i);正在为程序化物理点击时正确调用。

我正在使用changeView()更改ListView项目视图(TextViewImageView)的颜色,如下所示:

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

使用getViewByPosition()方法获取ListView项目的视图:

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

问题:当我点击物理(手动)但不是<时,上面的代码正在更改列表项的TextViewImageView的颜色强>编程

提前致谢。

2 个答案:

答案 0 :(得分:1)

尝试在chnageView()

中发送视图
  changeView(view);

在chnageView()

ImageView imgIcon = (ImageView) v.findViewById(R.id.icon);
        TextView txtTitle = (TextView) v.findViewById(R.id.title);

答案 1 :(得分:0)

分析代码,我可以推断,当您以编程方式更改颜色并调用changeView()时,不会执行颜色更改代码部分:

if(!myDict.ContainsKey(Xx) && /* Yy is inherited from FrameworkElement */)
{
   myDict.Add(Xx,Yy);
}

请检查是否执行了阻止。