2015-02-08 11 views
0

我有一個列表,當你點擊右側的行與圖標的佈局是由其他更改。這使得它很好,但是當我在列表中滾動時,我注意到有一些行具有相同的圖標而沒有點擊。另外,如果我快速滾動,這些相同的圖標會丟失並且構建方式不同。我認爲這是對細胞的重用,但我找不到解決方案。我想要的是,圖片只在clicka所在的行保持活動狀態。在列表視圖中的行不保留改造,並出現重複

謝謝!

Adapter類:

public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> { 

    private Context context; 
    private LayoutInflater inflater; 
    private ArrayList<Votante> listaVotantes; 
    private int rowLayout; 
    private View mConverView; 

    public ListadoVotantesArrayAdapter(Context context, int resource, ArrayList<Votante> objects) { 
     super(context, resource,objects); 
     this.context = context; 
     this.inflater = LayoutInflater.from(context); 
     this.listaVotantes = objects; 
     this.rowLayout = resource; 
    } 

    public int getCount(){ 
     return this.listaVotantes.size(); 
    } 

    public Votante getVotante(int position){ 
     return this.listaVotantes.get(position); 
    } 

    private static class ViewHolder { 
     public TextView lblName; 
     public TextView lblLastName; 
     public TextView lblDni; 
     public TextView lblVoterNumber; 
     public RelativeLayout lytVoted; 
     public RelativeLayout lytCanVote; 
     public RelativeLayout lytUndo; 
    } 


    public View getView(int position, View converView, ViewGroup parent) { 

     final ViewHolder viewHolder; 
     mConverView = converView; 
     if (mConverView == null){ 
      viewHolder = new ViewHolder(); 

      this.mConverView = inflater.inflate(this.rowLayout, parent, false); 

      if (mConverView != null){ 
       viewHolder.lblName = (TextView) mConverView.findViewById(R.id.lblVoterName); 
       viewHolder.lblLastName = (TextView) mConverView.findViewById(R.id.lblVoterLastName); 
       viewHolder.lblDni = (TextView) mConverView.findViewById(R.id.lblDni); 
       viewHolder.lblVoterNumber = (TextView) mConverView.findViewById(R.id.lblNumber); 
       viewHolder.lytVoted = (RelativeLayout) mConverView.findViewById(R.id.lytVoted); 
       viewHolder.lytCanVote = (RelativeLayout) mConverView.findViewById(R.id.lytCanVote); 
       viewHolder.lytUndo = (RelativeLayout) mConverView.findViewById(R.id.lytUndo); 
       mConverView.setTag(viewHolder); 
      } 
     }else{ 
      viewHolder = (ViewHolder) mConverView.getTag(); 
     } 

     Votante votante = getVotante(position); 

     viewHolder.lblName.setText(votante.getNombre()); 
     viewHolder.lblLastName.setText(votante.getApellidos()); 
     viewHolder.lblDni.setText(votante.getDni()); 
     viewHolder.lblVoterNumber.setText(""+votante.getNumVotante()); 

     if (votante.getVotado()){ 
      viewHolder.lytVoted.setVisibility(View.VISIBLE); 
      viewHolder.lytCanVote.setVisibility(View.GONE); 
     }else{ 
      viewHolder.lytVoted.setVisibility(View.GONE); 
      viewHolder.lytCanVote.setVisibility(View.VISIBLE); 
     } 
     mConverView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       viewHolder.lytUndo.setVisibility(View.VISIBLE); 
       notifyDataSetChanged(); 
      } 
     }); 
     return mConverView; 
    } 
} 

佈局行:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/lyt_voter" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="1dp"> 

    <RelativeLayout 
     android:id="@+id/lytVoterNumber" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_alignParentLeft="true" 
     android:background="@color/lightBlueItemList"> 


     <TextView 
      android:id="@+id/lblNumber" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="1999" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/lytVoterData" 
     android:layout_width="wrap_content" 
     android:layout_height="70dp" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/lytCanVote" 
     android:layout_toRightOf="@+id/lytVoterNumber" 
     android:layout_toStartOf="@+id/lytCanVote" 
     android:background="@color/whiteItemList" 
     android:orientation="vertical" 
     android:padding="5dp"> 

     <TextView 
      android:id="@+id/lblVoterLastName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Suarez Garcia de la Serna" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/lblVoterName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="José Carlos" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/lblDni" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="44950962S" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/lytCanVote" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_alignParentRight="true" 
     android:background="@color/yellowItemList" 
     android:minHeight="30dp" 
     android:minWidth="30dp"> 

     <ImageView 
      android:id="@+id/imgVote" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:src="@drawable/flecha" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/lytVoted" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_alignParentRight="true" 
     android:background="@color/grrenAcceptList" 
     android:minHeight="30dp" 
     android:minWidth="30dp" 
     android:visibility="gone"> 

     <ImageView 
      android:id="@+id/imgAccept" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:src="@drawable/aceptar" 
      /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:minHeight="30dp" 
     android:minWidth="30dp" 
     android:background="@color/redD3" 
     android:id="@+id/lytUndo" 
     android:layout_alignParentRight="true" 
     android:visibility="gone"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imgUndo" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" 
      android:src="@drawable/undo"/> 
    </RelativeLayout> 


</RelativeLayout> 

回答

0

您需要按照以下思路

1)這行代碼mConverView = converView;不有道理。直接使用converView(View from getView param).從適配器中刪除mConvertView

2)添加一些機制,可以告訴你哪個行被點擊並保存該變量。

示例: - 有一個大小爲Rows.size的布爾數組。並將它們設置爲onClick。

Boolean[] array = new Boolean[getSize()]; 

in onClick你已經在你的getview中設置了這個。

public void onClick(View v) { 
    array[position] = !array[position]; 
    viewHolder.lytUndo.setVisibility(View.VISIBLE); 
    //You do not need to call notifyDatasetChange. 

3)getView(),當你在設定數據來排項/或只是onClick之前。有如下檢查。

if(array[position])// this will tell you if you need to show or hid lytUndo thing 
    viewHolder.lytUndo.setVisibility(View.VISIBLE); // Show it 
else 
    viewHolder.lytUndo.setVisibility(View.GONE); // hide it or do whatever you want 

您可能需要設置一些PARAMS作爲最終

我寫了這個儘可能簡單,評論回到你的結果。

+0

它的作品!謝謝! – jamarfal 2015-02-11 23:19:27

+0

它也是。 YW。 – AAnkit 2015-02-12 04:42:30