繁体   English   中英

滚动时SimpleAdapter不保持位置

[英]SimpleAdapter not keeping position when scrolling

我有一个SimpleAdapter,用于显示两行列表视图。 我希望此简单适配器上的第15个位置具有不同的格式,但是当我滚动列表视图时,它会松开该位置,并且格式会更改位置。 有解决方案吗? 我试过与convertView混在一起,但似乎无法获得正确的格式。 我的代码如下。 谢谢!

        SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "publisher"}, new int[] {android.R.id.text1, android.R.id.text2}){
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            if (position == 15) {
                TextView text1 = (TextView) view.findViewById(android.R.id.text1);
                text1.setTextColor(Color.WHITE);
                TextView text2 = (TextView) view.findViewById(android.R.id.text2);
                text2.setTextColor(Color.WHITE);
                view.setBackgroundColor(getResources().getColor(R.color.teal_dark));
            }

            return view;
        }


    };

尝试添加else子句以将格式重置为默认格式

if (position == 15) {
    TextView text1 = (TextView) view.findViewById(android.R.id.text1);
    text1.setTextColor(Color.WHITE);
    TextView text2 = (TextView) view.findViewById(android.R.id.text2);
    text2.setTextColor(Color.WHITE);
    view.setBackgroundColor(getResources().getColor(R.color.teal_dark));
} else {
    TextView text1 = (TextView) view.findViewById(android.R.id.text1);
    text1.setTextColor(Color.BLACK);
    TextView text2 = (TextView) view.findViewById(android.R.id.text2);
    text2.setTextColor(Color.BLACK);
    view.setBackgroundColor(getResources().getColor(R.color.teal_light));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM