ListView与多个元素(图像视图)重叠

时间:2012-02-13 04:34:16

标签: android android-listview

陷入混乱我必须为平板电脑做书架。其中有书籍。所以我计划使用列表视图。我不想在网格视图中做,因为背景图像不能与项目一起滚动。所以我的时间不多了。所以想到使用列表视图。我能够在书架上展示书籍。但是书架上的书的图像正在重叠。请帮我解决这个问题。任何帮助对我都非常有价值。

代码有点冗长,就像其他列表视图一样。所以我猜它是来自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 这个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 个答案:

答案 0 :(得分:2)

好的问题解决了。 在book_shelf_row.xml中,imageview具有背景图像。所以图像重叠 当它动态地做。

结论:从imageview中删除src属性。因为我们在代码中动态更改src属性。