【问题标题】:How can I keep the state in an item from a ListView (like the item selected) when I scroll the list?滚动列表时,如何保持 ListView 中项目的状态(如所选项目)?
【发布时间】:2015-11-04 18:19:59
【问题描述】:

我的列表视图有问题;我在两个片段中创建了两个 ListView 以在相同的布局中显示它们

并且当我从左侧ListView中选择一个item时,会根据选择的item自动刷新右侧ListView。 问题是,当我从右侧 ListView 中选择一个项目时,它会将颜色更改为绿色,这没关系,因为我是这样编程的,如果我选择另一个项目,它也必须更改为绿色,并保持原始颜色从第一个选定的项目开始,如下图所示

First selection

但是当我滚动左侧 ListView 以选择另一个项目并返回列表顶部时,所选项目的状态已经消失,如下所示

The new view, without keep the state selected

我知道原因是因为列表视图回收了它的项目视图,但是我尝试了很多方法来尝试保持状态,无论我滚动列表,但我无法实现这个目标。 在这一点上,我不知道我必须从我的代码中改变什么;我向您展示了重要的部分;如果您需要我的代码中的更多信息,请告诉我。

ListItem 的构造函数:

    public class Lista_Item {
        private String color, texto;
        private String textoSuperior, textoInferior;
        boolean seleccionado = false;

    public Lista_Item(String color, String textoSuperior, String textoInferior, boolean seleccionado) {
        // TODO Auto-generated constructor stub
        this.color = color;
        this.textoSuperior = textoSuperior;
        this.textoInferior = textoInferior;
        this.seleccionado = seleccionado;
    }

    public Lista_Item(String color, String texto) {
        // TODO Auto-generated constructor stub
        this.color = color;
        this.texto = texto;
    }

    public String getTextoSuperior() {
        return textoSuperior;
    }

    public String getTextoInferior() {
        return textoInferior;
    }

    public boolean getSeleccionado() {
        return seleccionado;
    }

    public String getTexto() {
        return texto;
    }

    public String getColor() {
        return color;
    }


    public void setSeleccionado(boolean seleccionado) {
          this.seleccionado = seleccionado;
         }



}

适配器:

private class ListAdapter extends  ArrayAdapter<Lista_Item> {

private int mResourceId = 0;
private LayoutInflater mLayoutInflater;

public ListAdapter(Context context, int resource, int textViewResourceId, ArrayList<Lista_Item> listaItem) {
    super(context, resource, textViewResourceId, listaItem);
    mResourceId = resource;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null) {

        convertView = mLayoutInflater.inflate(mResourceId, parent, false);
        holder = new ViewHolder();

        holder.name = (TextView) convertView.findViewById(R.id.tvItemListaColor);
        holder.l = (LinearLayout)convertView.findViewById(R.id.layoutColor);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    Lista_Item pedido = listItems.get(position);
    holder.name.setText(pedido.getTexto());
    holder.l.setBackgroundColor(Color.parseColor(pedido.getColor()));
    holder.name.setTag(pedido);

    return convertView;
}

private class ViewHolder {
    TextView name;
    LinearLayout l;
}

}

OnItemClickListener 事件:

public AdapterView.OnItemClickListener d = new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> list, View view, int pos, long id) {
        // TODO Auto-generated method stub

    if(listener!=null){
        listener.onPedidoSeleccionado((Lista_Item)list.getAdapter().getItem(pos));
    }

    String[] item = parts[pos].split("!");
    Toast.makeText(getActivity(), "Ha pulsado el item " +item[0], Toast.LENGTH_SHORT).show();
    Log.d("Color","Color = "+colorSaved);

    if (currentSelectedView != null && currentSelectedView != view) {
        unhighlightCurrentRow(currentSelectedView, colorSaved);
        colorSaved = item[1];
    }

    currentSelectedView = view;
    highlightCurrentRow(currentSelectedView);
    colorSaved = item[1];
}

};

以及我上面使用的方法:

private void unhighlightCurrentRow(View rowView, String color) {
        rowView.setBackgroundColor(Color.parseColor("#"+color));
    }

    private void highlightCurrentRow(View rowView) {
        rowView.setBackgroundColor(Color.GREEN);

    }

请帮忙???

【问题讨论】:

  • 更改问题的标题,使其更有意义。
  • 嗨,我很抱歉,但由于某种原因,这并没有让我写自己的标题,我想它写了另一个来自其他问题或主题的标题,但问题是:当我滚动列表时,如何保持 ListView 中项目的状态(如所选项目)
  • 已完成,错误见谅

标签: android listview android-listview


【解决方案1】:

创建一个对象来设置一个arrayList,这个对象是这样的

 `public class FormaPagoVO {

    String id;
    Boolean anadir; 
    create the variables do you want whit getter and setter.....
    public Boolean getAnadir() {
        return anadir;
    }

    public void setAnadir(Boolean anadir) {
        this.anadir = anadir;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}`

然后在你的活动中像这样,并将数组传递给适配器

 List<FormaPagoVO> arrayForma = new ArrayList<FormaPagoVO>();
  FormaPagoAdapter adapterDos = new FormaPagoAdapter(getApplicationContext(),arrayForma);
 listViewForma.setAdapter(adapterDos);

在适配器的getView中这个

 if(getItem(position).getAnadir()){ // validate if check de listview or not and change de background
            imgClientes.setBackgroundResource(R.drawable.seleccion1);
        }else{
            imgClientes.setBackgroundResource(R.drawable.seleccion);
        }

点击列表视图

imgClientes.setOnClickListener( new android.view.View.OnClickListener() {
            public void onClick(View v)
            {
                if(getItem(position).getAnadir()){
                    // vuelve a blanco

                    getItem(position).setAnadir(false);
                }else{
                    // coloca verde

                    getItem(position).setAnadir(true);
                }
            }
        });

就是这样

【讨论】:

  • 嗨,不好意思,您能否在某些事情上更具体一些,我的意思是,例如,我必须推迟与 ViewHolder 相关的代码并放置您的代码?哪里是正确放置代码的正确位置;我的意思是,在 if(convertView==null) 之外或在 else 语句中......可以解释一下这部分吗?我已经尝试根据我的需要放置您的代码,但它不能正常工作,所以我认为我做错了什么......
  • 嗨,我的代码没有替换,只看到我使用 de objectVO 在 listview 的 clicekd 位置设置项目的 de 状态,当用户点击项目时,我设置 object.anadir = true,当 de该职位的 listview 通行证答案是真还是假
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多