我正在尝试制作一个功能类似于 android 市场的画廊,您可以在其中滚动(左/右)以查看免费或付费应用程序等......也可以在布局中上下滚动。
到目前为止,我只加载了两个具有简单“Hello World!”的布局。文本视图和“嘿!你好吗?” 文本视图。
它们加载良好,除了最初画廊位置 0 处的文本显示为暗淡,直到我滚动并返回它。有什么我想念的吗?
public class HelloGallery extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery)findViewById(R.id.gallery);
gallery.setAdapter(new ViewAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
public class ViewAdapter extends BaseAdapter
{
public Context mContext;
public static final Integer[] viewId = { R.layout.helloworld, R.layout.heyhowareyou };
public int mGalleryItemBackground;
public ViewAdapter(Context context)
{
this.mContext = context;
TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
@Override
public int getCount()
{
return viewId.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = LayoutInflater.from(mContext).inflate(viewId[position], null);
convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
convertView.setBackgroundResource(mGalleryItemBackground);
return convertView;
}
}
我也参考了一点。Aavon 在这个线程中所做的正是我想要达到的......
主题链接:获取按钮以在具有膨胀布局的画廊中工作
有什么帮助吗?
提前致谢。