封面流与android中的textview

时间:2014-07-14 07:13:16

标签: android xml coverflow

您好我正在创建一个我正在使用CoverFlow的应用。但我想用imageview添加textview。我已经改变了AbstractCoverFlowImageAdapter,如下所示。

/**
 * This class is an adapter that provides base, abstract class for images
 * adapter.
 * 
 */
public abstract class AbstractCoverFlowImageAdapter extends BaseAdapter {

    /** The Constant TAG. */
    private static final String TAG = AbstractCoverFlowImageAdapter.class.getSimpleName();

    /** The width. */
    private float width = 0;

    /** The height. */
    private float height = 0;

    /** The bitmap map. */
    private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();

    public AbstractCoverFlowImageAdapter() {
        super();
    }

    /**
     * Set width for all pictures.
     * 
     * @param width
     *            picture height
     */
    public synchronized void setWidth(final float width) {
        this.width = width;
    }

    /**
     * Set height for all pictures.
     * 
     * @param height
     *            picture height
     */
    public synchronized void setHeight(final float height) {
        this.height = height;
    }

    @Override
    public final Bitmap getItem(final int position) {
        final WeakReference<Bitmap> weakBitmapReference = bitmapMap.get(position);
        if (weakBitmapReference != null) {
            final Bitmap bitmap = weakBitmapReference.get();
            if (bitmap == null) {
                Log.v(TAG, "Empty bitmap reference at position: " + position + ":" + this);
            } else {
                Log.v(TAG, "Reusing bitmap item at position: " + position + ":" + this);
                return bitmap;
            }
        }
        Log.v(TAG, "Creating item at position: " + position + ":" + this);
        final Bitmap bitmap = createBitmap(position);
        bitmapMap.put(position, new WeakReference<Bitmap>(bitmap));
        Log.v(TAG, "Created item at position: " + position + ":" + this);
        return bitmap;
    }

    /**
     * Creates new bitmap for the position specified.
     * 
     * @param position
     *            position
     * @return Bitmap created
     */
    protected abstract Bitmap createBitmap(int position);

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public final synchronized long getItemId(final int position) {
        return position;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */


    public final synchronized View getView(final int position, final View convertView, final ViewGroup parent) {
      //  ImageView imageView;
        View root;
        if (convertView == null) {
             final Context context = parent.getContext();
             LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            root = mInflater.inflate(R.layout.coverflowitem, null);
            root.setLayoutParams(new CoverFlow.LayoutParams(150, 150));
        } else {
            Log.v(TAG, "Reusing view at position: " + position + ":" + this);
            root = convertView;
        }
        ImageView imageView = (ImageView)root.findViewById(R.id.coverflow_image);
        TextView text = (TextView)root.findViewById(R.id.descriptionText);
        imageView.setImageBitmap(getItem(position));
        text.setText("You Win!");

        return root;
    }

}

我已将getView()方法的返回类型更改为从ImageView查看。

我的coverflow xml和coverflow项目xml分别被打击

coverflow.xml

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

    <com.android.jigsawpuzzle.puzzle.coverflow.CoverFlow
        xmlns:coverflow="http://schemas.android.com/apk/res-auto/org.worldsproject.puzzle"
        android:id="@+id/coverflow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        coverflow:imageHeight="250dip"
        coverflow:imageReflectionRatio="0.2"
        coverflow:imageWidth="250dip"
        coverflow:reflectionGap="2dip"
        coverflow:withReflection="true" />

    <Button
        android:id="@+id/difficulty"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:text="@string/easy" 
        android:layout_marginBottom="10dp"
        android:background="@drawable/profile_border"/>

</RelativeLayout>

coverflowitems.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:id="@+id/coverflow_image"
               android:layout_width="100dp"
               android:layout_height="100dp"
               android:layout_gravity="center"
               android:contentDescription="image"/>

    <ScrollView android:id="@+id/sv"
                android:layout_width="250dp"
                android:layout_height="150dp"
                android:background="@drawable/profile_border"
                android:layout_gravity="center_horizontal">

        <TextView android:id="@+id/descriptionText"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="left"
                  android:textColor="@color/black"
                  android:text="Image description"/>

    </ScrollView>


</LinearLayout>

现在我的问题是我在运行应用程序时遇到错误: -

07-14 12:43:03.089: E/AndroidRuntime(9976): FATAL EXCEPTION: main
07-14 12:43:03.089: E/AndroidRuntime(9976): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
07-14 12:43:03.089: E/AndroidRuntime(9976):     at CoverFlow.getChildStaticTransformation(CoverFlow.java:288)

请帮助摆脱此错误并将我重定向到正常工作的代码。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您正在使用的Coverflow实现仅适用于ImageView,而不适用于一般视图。

以此为例看一下 https://github.com/applm/ma-components

答案 1 :(得分:0)

我曾经遇到过这个问题并找到了解决方案。我在这里发布了答案:

Coverflow with custom adapter