2012-02-13 22 views
1

私はタブレットの本棚を使わなければなりません。その中には本が並んでいます。だから私はリストビューを使用する予定でした。背景画像がアイテムとスクロール可能になっていなかったので、グリッドビューではしたくなかった。だから私は時間がなくなっていた。だからリストビューを使用すると思った。私は本を​​棚に展示することができます。しかし、棚の本のイメージが重なっている。これを解決するために私を助けてください。どんな援助も私にとって非常に貴重です。複数の要素(画像ビュー)が重複しているリストビュー

コードは少し長く、他のリストビューと同じです。だから私はgetviewメソッドから来ていると思います。その部分を直接確認することができます。コードの残りは参考用です また添付されているスナップショットもご覧ください。

以下のコードを確認してください:

/* 
* Number of books in array - totalBooks 
* To display number of books in one row - bookItem 
* Quotient (Number of rows will hold books) - numberOfBookRows 
* Reminder (Number of extra books in row) - extraBooks 
*/ 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ImageView.ScaleType; 
import android.widget.LinearLayout; 
import android.widget.ListView; 

public class BookShelf extends Activity { 

    private static final String TAG = "Book-shelf"; 
    //calculation 
    private int totalBooks = 0; 
    private int bookItem = 0; 
    private int numberOfBookRows = 0; 
    private int extraBooks = 0; 

    private ArrayAdapter<String> adapter; 
    private ListView listview; 

    private String[] bookName = new String[5]; 
    private int[] bookImage = new int[5]; 

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

     // THIS ARE STATIC CONTENT 

     // Title of book 
     bookName[0] = "EMF"; 
     bookName[1] = "Ganddhi"; 
     bookName[2] = "The Code"; 
     bookName[3] = "The Code"; 
     bookName[4] = "The Code"; 

     // Image for book 
     bookImage[0] = R.drawable.book1; 
     bookImage[1] = R.drawable.book2; 
     bookImage[2] = R.drawable.book3; 
     bookImage[3] = R.drawable.book3; 
     bookImage[4] = R.drawable.book1; 

     listview = (ListView) findViewById(R.id.list); 
     calculateRowsAndColms(); 
     adapter = new MyCustomAdapter(BookShelf.this, R.layout.book_shelf_row); 
     listview.setAdapter(adapter); 
     addEmptyRow(5); 
    } 

    public void calculateRowsAndColms(){ 
     totalBooks = 0; 
     bookItem = 0; 
     numberOfBookRows = 0; 
     extraBooks = 0; 

     totalBooks = bookName.length; 
     bookItem = 3;//TODO: make it dynamic 
     if(totalBooks < bookItem && totalBooks > 0){ 
      numberOfBookRows = 1; 
     }else{ 
      numberOfBookRows = (totalBooks/bookItem); 
      extraBooks = (totalBooks % bookItem); 
      if(extraBooks != 0) 
       numberOfBookRows++; 
     } 

     Log.d(TAG," Total books:"+totalBooks); 
     Log.d(TAG," Number of books to be displayed:"+bookItem); 
     Log.d(TAG," Number of rows occupying the books:"+numberOfBookRows); 
     Log.d(TAG," Number of extra books(odd count):"+extraBooks); 
    } 

    public void addEmptyRow(int count) { 

     LinearLayout ll_addRows = (LinearLayout) findViewById(R.id.add_rows); 
     LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     count = count * 2; 
     for (int i = 0; i < count; i++) { 

      ImageView bg = new ImageView(this); 
      bg.setImageResource(R.drawable.book_shelf_bgdub); 
      bg.setLayoutParams(params); 
      bg.setScaleType(ScaleType.FIT_XY); 

      ImageView border = new ImageView(this); 
      border.setImageResource(R.drawable.book_shelf_border_dub); 
      border.setLayoutParams(params); 
      border.setScaleType(ScaleType.FIT_XY); 

      Log.d(TAG, "i:" + i); 
      if (i % 2 == 0) 
       ll_addRows.addView(bg, i); 
      else 
       ll_addRows.addView(border, i); 
      Log.d(TAG, "i:" + i); 
     } 
    } 

    public class MyCustomAdapter extends ArrayAdapter { 

     Context context; 
     int rowXmlId; 

     public MyCustomAdapter(Context context, int textViewResourceId) { 
      super(context, textViewResourceId); 
      rowXmlId = textViewResourceId; 
      this.context = context; 

     } 

     @Override 
     public int getCount() { 
      return numberOfBookRows; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      Log.d(TAG, " POSITION:" + position); 

      ViewHolder holder; 
      View v = convertView; 
      if (convertView == null) { 
       holder = new ViewHolder(); 
       LayoutInflater mInflater = (LayoutInflater) context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = mInflater.inflate(rowXmlId, null, false); 

       ImageView image1 = (ImageView) v.findViewById(R.id.image1); 
       ImageView image2 = (ImageView) v.findViewById(R.id.image2); 
       ImageView image3 = (ImageView) v.findViewById(R.id.image3); 

       holder.imgList.add(image1); 
       holder.imgList.add(image2); 
       holder.imgList.add(image3); 
       v.setTag(holder); 

      } else { 
       holder = (ViewHolder) v.getTag(); 
      } 


      if((position == numberOfBookRows-1) && extraBooks!=0) 
       for(int i=0; i<extraBooks; i++){ 
        int k = (position * bookItem)+i; 
        holder.imgList.get(i).setBackgroundResource(bookImage[k]); 
        holder.imgList.get(i).setOnClickListener(new OnBookClickListner()); 
        holder.imgList.get(i).setVisibility(View.VISIBLE); 
        holder.imgList.get(i).setId(k); 

        Log.d(TAG,"============> k:"+k+" i:"+i+" id:"+holder.imgList.get(i).getId()); 
       } 
      else 
       for(int i=0; i<bookItem; i++){ 
        int k = (position * bookItem)+i; 
        holder.imgList.get(i).setBackgroundResource(bookImage[k]); 
        holder.imgList.get(i).setOnClickListener(new OnBookClickListner()); 
        holder.imgList.get(i).setVisibility(View.VISIBLE); 
        holder.imgList.get(i).setId(k); 

        Log.d(TAG,"============> k:"+k+" i:"+i+" id:"+holder.imgList.get(i).getId()); 
       } 

      return v; 
     } 
    } 

    static class ViewHolder { 
     ArrayList<ImageView> imgList = new ArrayList<ImageView>(); 
     //ImageView image1, image2 ,image3; 
    } 

    public class OnBookClickListner implements OnClickListener{ 

     @Override 
     public void onClick(View v) { 
      Log.d(TAG, " ITEM SELECTED IN LIST VIEW:"+v.getId()); 
      Intent intent = new Intent(BookShelf.this, MyLessons.class); 
      startActivity(intent); 
     } 

    } 
} 

XMLは次のように装着されている:book_shelf.xml をこのリストと述べ、リストの先頭にブックシェルフのためのいくつかの境界線を追加することがあり、メインxmlです。ここでは何も大したことはありません。このxmlのリストを使用するだけです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/book_shelf_top_border" /> 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:divider="@drawable/book_shelf_border_dub" > 
</ListView> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/book_shelf_border_dub" /> 

<LinearLayout 
    android:id="@+id/add_rows" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
</LinearLayout> 

以下のXML book_shelf_row.xml iが行を表すために使用していこのXML。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/book_shelf_bgdub" 
android:orientation="horizontal" > 

<View 
    android:layout_width="50dp" 
    android:layout_height="120dp" /> 

<ImageView 
    android:id="@+id/image1" 
    android:layout_width="100dp" 
    android:layout_height="120dp" 
    android:layout_gravity="bottom" 
    android:src="@drawable/book1" 
    android:visibility="invisible" /> 

<View 
    android:layout_width="100dp" 
    android:layout_height="120dp" /> 

<ImageView 
    android:id="@+id/image2" 
    android:layout_width="100dp" 
    android:layout_height="120dp" 
    android:layout_gravity="bottom" 
    android:src="@drawable/book2" 
    android:visibility="invisible" /> 

<View 
    android:layout_width="100dp" 
    android:layout_height="120dp" /> 

<ImageView 
    android:id="@+id/image3" 
    android:layout_width="100dp" 
    android:layout_height="120dp" 
    android:layout_gravity="bottom" 
    android:src="@drawable/book3" 
    android:visibility="invisible" /> 

</LinearLayout> 

各画像ビューには本の画像が保持されます。

以下はスナップショットです。ブックイメージがどのように重なっているか。 the image is over lapping on each other

R.drawable.book1からbook3は、それぞれ画像内に見ることができます。 1行目では、2行目と同様に自己イメージが置き換えられています。 イメージを深く観察している場合。最初の行の最初のイメージ。重複している。 なぜこれが起こっているのか分かりません。あなたは私にこのことを助けてくれますか? 何か助けていただければ幸いです。ありがとうございました。

答えて

2

問題を解決しました。 in book_shelf_row.xml画像ビューに背景画像があります。それで、画像は動的になると と重なっていました。

結論:imageviewからsrc属性を削除します。コード内のsrc属性を動的に変更しているためです。

関連する問題