2015-11-04 51 views
0

我有一个列表视图的问题;我在两个片段中创建了两个ListView以在同一个布局中显示它们当我滚动列表时,如何从列表视图中保持状态(如所选项目)?

而且,当我从左侧ListView中选择一个项目时,根据所选项目自动刷新右侧ListView。 问题是,当我从右侧的ListView中选择一个项目时,它将颜色更改为绿色,这没问题,因为我是以这种方式编程的,如果我选择另一个项目,它也必须更改为绿色,并保留原始从第一个选择项的颜色,就像我告诉你下面的图片

First selection

但是当我滚动左侧ListView中选择另一个项目,我返回到列表的顶部,的状态选择的项目已经走了,正如我在下面

The new view, without keep the state selected

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

的列表项的构造:

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

    } 

请,任何帮助???

+2

什么peating? – mubeen

+0

更改您的问题的标题,以便更有意义。 – Henry

+0

嗨,我很抱歉,但由于某种原因,这不会让我写自己的标题,而是从另外的问题或主题写下另一个问题或主题,但问题是:**我该如何保持状态在ListView的项目中(如选中的项目)当我滚动列表** –

回答

0

创建一个对象设置一个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); 
     } 

int点击列表视图

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

就是所有

+0

嗨,请问,在某些事情中,您可能更具针对性,例如,我必须推迟与ViewHolder相关的代码,并放置你的?哪里是准确放置代码的正确位置;我的意思是,从外部** if(convertView == null)**或在其他语句中...可以解释这部分吗?我已经试图根据我的需要放置你的代码,但它不能正常工作,所以我认为我做错了什么... –

+0

嗨没有替换我的代码,只看到即时通讯使用de objectVO设置de状态的项目在位置clicekd在列表视图,当用户点击项目我设置object.anadir = true,当de列表视图传递该位置答案是真或假为了使东西 –

相关问题