【问题标题】:Item selection was deselected while scrolling in listview在列表视图中滚动时取消选择项目选择
【发布时间】:2016-10-05 03:46:07
【问题描述】:

我想更改列表视图中所选项目的文本颜色。

主要

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub_alimentacao);

    List<Tag> tags = getTagsSubAlimentacao();

    final ListView listView = (ListView) findViewById(R.id.subAlimentacao);
    listView.setAdapter(new TagSubAlimentacaoAdapter(this, tags));



    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {


            TextView c = (TextView) view.findViewById(R.id.local);
            LikeButton lk = (LikeButton) view.findViewById(R.id.gostei);

            lk.setEnabled(false);


            //OBTEM A COR EM INTEIRO E CONVERTE PARA HEXADECIMAL
            Integer intColor = c.getCurrentTextColor();
            String hexColor = "#" + Integer.toHexString(intColor).substring(2);

            if (hexColor.equalsIgnoreCase("#2196F3")){
                c.setTextColor(Color.parseColor("#aaaaaa"));
                lk.setLiked(false);

            }else{
                c.setTextColor(Color.parseColor("#2196F3"));
                lk.setEnabled(true);
                lk.setLiked(true);

            }



        }
    });

}

适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Tag tag = tags.get(position);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layoute = inflater.inflate(R.layout.item_sub, null);

    TextView titulo = (TextView) layoute.findViewById(R.id.local);
    LikeButton lk = (LikeButton) layoute.findViewById(R.id.gostei);
    titulo.setText(tag.getTitulo());
    lk.setLiked(tag.getAtivo());
    return layoute;
}

点击后一切正常。颜色变了。但我有一个问题,即,

例如,我在列表视图中有 10 个项目,起初只有 5 个项目可见(因为屏幕分辨率),如果我滚动,我可以看到接下来的 5 个项目。

当我选择前 5 个元素时,颜色会发生变化。但是,如果滚动接下来的 5 个元素

前5个元素的颜色恢复到原来的状态。

【问题讨论】:

  • 列表项在每次滚动列表时回收,在对象中定义一个布尔变量,并代表该变量显示隐藏可检查视图。
  • 感谢您的建议,我的问题已经按照他的建议和下面的朋友解决了。

标签: android listview baseadapter


【解决方案1】:

在 ListView 滚动时,它总是设置适配器,所以在适配器内部你必须设置文本颜色,如果你更喜欢 ViewHolder 模式会更好

使用下面的内部适配器获取输出

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Tag tag = tags.get(position);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layoute = inflater.inflate(R.layout.item_sub, null);

    TextView titulo = (TextView) layoute.findViewById(R.id.local);
    LikeButton lk = (LikeButton) layoute.findViewById(R.id.gostei);
    titulo.setText(tag.getTitulo());
    boolean likeValue=tag.getAtivo();
    if(likeValue){
       titulo.setTextColor(Color.parseColor("#2196F3"));
    }else{
       titulo.setTextColor(Color.parseColor("#aaaaaa"));
    }
    lk.setLiked(likeValue);

    return layoute;
}

【讨论】:

  • 在适配器中实现启用和禁用的方法,然后用他的建议调用 main 解决了我的问题。 public void alteraCor(int position){ tags.get(position).setAtivo(true); } public void removeCor(int position){ tags.get(position).setAtivo(false); } 谢谢!
【解决方案2】:

使用 View Holder Pattern... 使用 this answers 根据您的要求进行更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多