安卓图库,第一个缩略图作为封面

时间:2011-04-13 18:51:07

标签: android gallery

感谢阅读!

某些背景:

我正在构建教程here

中的图库应用

我对此代码所做的更改只是替换

i.setLayoutParams(new Gallery.LayoutParams(150, 100));

i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

一次只显示一个图库图像(有点像幻灯片显示器)。

问题:

我希望图库的第一个缩略图作为相册封面,并带有两个TextView来显示相册信息。

实验: 所以,我创建了一个cover.xml,如下所示:

 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:padding="6dip">
    <ImageView android:id="@+cover/imgImage" android:adjustViewBounds="true"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    </ImageView>
    <TextView android:id="@+cover/tvCoverText1"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:maxLines="2" android:text="Text1" />
    <TextView android:id="@+cover/tvCoverText2"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:singleLine="true" android:maxLines="1" android:layout_below="@cover/tvCoverText1"
        android:text="Text2" />
</RelativeLayout>

这是Java代码。如果位置为0(第一个缩略图),我会检查getView(),然后查看视图。


package com.sagar.sample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

    private LayoutInflater mInflater = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gallery g = (Gallery) findViewById(R.main.gallery);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
                0, R.drawable.bp1, R.drawable.bp2, R.drawable.bp3 
        };

        public ImageAdapter(Context c) {
            mContext = c;
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
//            mGalleryItemBackground = a.getResourceId(
//                    R.styleable.HelloGallery_android_galleryItemBackground, 0);
//            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            View view = convertView;
            if(view == null) {
                view = mInflater.inflate(R.layout.cover, null);

                viewHolder = new ViewHolder();
                viewHolder.tvCoverText1 = (TextView)view.findViewById(R.cover.tvCoverText1);
                viewHolder.tvCoverText2 = (TextView)view.findViewById(R.cover.tvCoverText2);
                viewHolder.imgView = (ImageView)view.findViewById(R.cover.imgImage);
                view.setTag(viewHolder);             
            }
            else {
                viewHolder = (ViewHolder)view.getTag();
            }

            if(position == 0) {
                viewHolder.tvCoverText1.setVisibility(View.VISIBLE);
                viewHolder.tvCoverText2.setVisibility(View.VISIBLE);
                viewHolder.imgView.setVisibility(View.GONE);
            }
            else {
                viewHolder.tvCoverText1.setVisibility(View.GONE);
                viewHolder.tvCoverText2.setVisibility(View.GONE);
                viewHolder.imgView.setVisibility(View.VISIBLE);
                //viewHolder.imgView = new ImageView(mContext);
                viewHolder.imgView.setImageResource(mImageIds[position]); //Album cover is at 0th position
                viewHolder.imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                viewHolder.imgView.setScaleType(ImageView.ScaleType.FIT_XY);
                viewHolder.imgView.setBackgroundResource(mGalleryItemBackground);
            }
            return view;
        }
    }

    static class ViewHolder {
        TextView tvCoverText1, tvCoverText2;
        ImageView imgView;
    }
}

最终结果: 当应用程序加载时,我首先看到一个空白屏幕一段时间,然后视图更改以显示AlbumCover。滚动图像非常慢。

嗯......显然,我做错了什么。我真诚地希望有人能在这里帮助我:(

谢谢!

更新:添加main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <Gallery
        android:id="@+main/gallery"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
    />
</RelativeLayout>

UPDATE2:所以,这里有一些伪造的代码来解释我想要实现的目标:


if(position == 0)
    //show toptext and bottomtext from cover.xml
else
    //show tvTitle1 and tvTitle2 (may later include tvTitle3 and tvTitle4) from main.xml

现在,只有位置0的情况起作用,当我滑动到位置1并向后滑动到位置0时,TextView s显示为灰色,几乎看不见。 :(

3 个答案:

答案 0 :(得分:0)

您需要每次都在文本视图中设置文本,而不仅仅是在视图膨胀时。

答案 1 :(得分:0)

您应该有一个单独的布局文件,用于保存库中的ui组件。现在,您有2个TextView个组件独立于Gallery。相反,创建一些这样的布局资源: gallery_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:background="@android:drawable/picture_frame"
    android:layout_centerInParent="true" />
    <TextView 
        android:id="@+main/tvTitle1" android:text="Title 1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_above="@+main/tvTitle2">
    </TextView>
    <TextView  
        android:id="@+main/tvTitle2" android:text="Title 2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" android:layout_alignParentLeft="true">
     </TextView>
</RelativeLayout>

所以在getView

        View v = convertView;
        if(v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.gallery_item, null);

            holder = new ViewHolder();
            holder.text1 = (TextView)v.findViewById(R.id.tvTitle1);
            holder.text2 = (TextView)v.findViewById(R.id.tvTitle2);
            holder.imageView = (ImageView)v.findViewById(R.id.imageView);

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

        if(position == 0) {
          holder.text1.setVisibility(View.VISIBLE);
          holder.text2.setVisibility(View.VISIBLE);
          holder.imageView.setVisibility(View.GONE);
        }
        else {
          holder.text1.setVisibility(View.GONE);
          holder.text2.setVisibility(View.GONE);
          holder.imageView.setVisibility(View.VISIBLE);
        }

        return v;

您可能会发现滚动项目时会出现一些奇怪的行为,因此您可能不得不直接访问每个UI组件而不是使用持有者:

((TextView)v.findViewById(R.id.tvTitle1)).setVisibility(View.GONE);

您可能也有兴趣为getView设置不同类型的观看次数:http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int

答案 2 :(得分:0)

在尝试了此处建议的所有方法后,我无法按照我想要的方式获得自定义图库。我一直遇到ClassCastException。所以,here's到现在为止对我有用。这只是一个解决方法,并且有人想出一个更好的方法来设计自定义库 - 请在这里发布您的答案,以便我接受它。

感谢您的帮助!