2012-03-17 8 views
4

私はカスタムのListViewAdapterを持っています。これはリストのための行を作成しています。私の問題は、ImageViewを互いに区別しているようには見えないということです。私は上下にスクロールするとランダムにを掴んでチャックするようです。テキスト情報(このスニペットでは省略されています)は中断しません。期待通りに機能します。あなたがイメージがあるので、一度これを実行する必要がカスタムListViewアダプタ、奇妙なImageViewの動作

// find the image 
ImageView favImage = (ImageView)v.findViewById(R.id.toggle_favorite); 
//setting to default 
favImage.setImageResource(R.drawable.default_image); 
// when clicked... 
favImage.setOnClickListener.... 

:あなたは、右の参照を発見した後、デフォルトの画像を定義する必要が

public View getView(int position, View convertView, ViewGroup parent) 
    { 
    View v = convertView; 

    if(v == null) 
    { 
     LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.generic_row, null); 
    } 

    // find the image 
    ImageView favImage = (ImageView)v.findViewById(R.id.toggle_favorite); 

    // when clicked... 
    favImage.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) 
    { 
     // make the gray star a yellow one 
     int newImage = R.drawable.ic_star_yellow_embossed; 
     ((ImageView)v).setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(), newImage)); 
    } 

    }); 

    return v; 
    } 
+0

は** getView2 ** ........? – waqaslam

+0

@Waqas:それは私のコピー、貼り付け、私のクラスでのものを取り除いていたことを無視してください。これはコード内で正しく命名されています。 – Josh

答えて

3

をスクロールされたときに、デフォルトのイメージを使用するgetViewメソッドでそれを定義する必要があります(画像が変更された)ユーザによって操作された画像は、修正されていないはずであった。これを避けるには、リスト内のすべての行に対して何らかの理由でImageViewのステータスを保持し、このステータスを使用してgetView()メソッドで正しいイメージを設定する必要があります。どのようにアダプタを実装したのかがわからないので、簡単な例を示します。

まず、ImageViewのステータスを保存する必要があります。私はArrayList<Boolean>をカスタムアダプターのメンバーとして使用しました。このリストの位置(リスト内の行の位置に対応)がfalseであれば画像はデフォルトの画像、そうでない場合はtrueの場合はユーザーがクリックした

private ArrayList<Boolean> imageStatus = new ArrayList<Boolean>(); 

カスタムアダプタコンストラクタでこのリストを初期化します。たとえば、あなたのアダプタで何かのリストを置くならば、あなたはあなたのimageStatusは、としてそのリストとして大きなとfalseでいっぱいにする必要があります(デフォルト/ステータスを開始):

//... initialize the imageStatus, objects is the list on which your adapter is based 
for (int i = 0; i < objects.size(); i++) { 
    imageStatus.add(false); 
} 

その後、あなたのgetView()方法で:

View v = convertView; 

      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getContext() 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.adapters_adapter_with_images, null); 
      } 

      // find the image 
      ImageView favImage = (ImageView) v 
        .findViewById(R.id.toggle_favorite); 
      // Set the image bitmap. If the imageStatus flag for this position is TRUE then we 
      // show the new image because it was previously clicked by the user 
      if (imageStatus.get(position)) { 
       int newImage = R.drawable.ic_star_yellow_embossed; 
       favImage.setImageBitmap(BitmapFactory.decodeResource(
         getContext().getResources(), newImage)); 
      } else { 
       // If the imageStatus is FALSE then we explicitly set the image 
       // back to the default because we could be dealing with a 
       // recycled ImageView that has the new image set(there is no need to set a default drawable in the xml layout)          
       int newImage = R.drawable.basket_empty; //the default image 
       favImage.setImageBitmap(BitmapFactory.decodeResource(
         getContext().getResources(), newImage)); 
      } 
      // when clicked... we get the real position of the row and set to TRUE 
      // that position in the imageStatus flags list. We also call notifyDataSetChanged 
      //on the adapter object to let it know that something has changed and to update! 
      favImage.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Integer realPosition = (Integer) v.getTag(); //get the position from the view's tag 
        imageStatus.set(realPosition, true); //this position has been clicked be the user 
        adapter.notifyDataSetChanged(); //notify the adapter 
       } 

      }); 
      // set the position to the favImage as a tag, we later retrieve it 
      // in the onClick method 
      favImage.setTag(new Integer(position)); 
      return v; 

     } 

これは、リストを動的に変更する予定がない場合(行の削除/追加)にはうまくいくはずです。そうしないと、その変更を反映するためにそのリストimageStatusも変更する必要があります。あなたの行データは何か、他のアプローチは何かを言いませんでした。そして、ユーザーがそのイメージをクリックすると(何かを行う場合は(変更する以外に)何かをやろうとすれば)、行のデータモデルにイメージのステータスを組み込むことです。ここでは、このに関するいくつかのチュートリアルです:

Android ListView Advanced Interactive またはCommonsware-Android Excerpt(インタラクティブ行)

2

:ここ

は私 Adapterの関連する方法であり、変更し、ListViewをスクロールすると、ListViewがアイテムビューをリサイクルするため、再表示されます。だから、リストは ListViewは、リストを上下にスクロールし、このためのようあなたが行を取得行の景色をリサイクルするための行動が表示されていることを

+0

私は分かりません。私は画像をクリックしたときにこれをしようとしています。私は既に.xmlファイルにデフォルトイメージを設定しています。 – Josh

+0

これはxmlでの設定は 'if(v == null)'で実現されますが、その後ListViewがリサイクルするときにxmlから読み込まれず、単にビューがリサイクルされます。その場合、ImageViewにはクリックされると古い画像が定義されます。単にデフォルトのイメージをコードに設定するだけで、うまくいくと思います – waqaslam