带有第一个缩略图作为封面的Android Gallery
|
谢谢阅读!
一些背景:
我正在从此处的教程构建Gallery应用程序
我对此代码所做的唯一更改是替换
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
与
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
一次仅显示一个画廊图像(像幻灯片播放器那样的Kinda)。
问题:
我希望图库的第一个缩略图充当带有两个two2ѭ的专辑封面,以显示专辑信息。
实验:
所以,我这样创建了一个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代码。我在getView()
中检查位置是否为0(第一个缩略图),然后在视图中播放。
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时也是如此--2ѭ变灰并且几乎看不到。 :(
没有找到相关结果
已邀请:
3 个回复
陷牡彭拈峰
蜗仓馈
组件,它们独立于
。而是创建一些布局资源,如下所示:
因此,在您的
中:
您可能会发现在滚动项目时会发生一些奇怪的行为,因此您可能必须直接访问每个UI组件而不是使用Holder:
等 您可能还想对ѭ15设置不同类型的视图:http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)
浮凰量