我正在使用库视图制作记忆游戏。我希望所有卡片都显示在底部,当用户从图库中选取一张卡片时,特定的卡片会显示在其上方(在图像视图中)。当用户触摸这张卡片时,卡片应该转动(使用翻转动画)并且卡片的正面应该被显示(这应该是新的活动)。在翻转过程中,我总是想在底部显示画廊。我怎样才能做到这一点?我有以下代码没有错误,但应用程序在开始之前崩溃。在查看日志猫时,它建议在开始新酸度时出现错误。如何开始一个新的活动,而始终保持画廊?
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
//---the images to display---
Integer[] imageIDs = {
R.drawable.you,
R.drawable.date,
R.drawable.competition,
R.drawable.contacts,
R.drawable.memory,
R.drawable.mission,
R.drawable.recognition,
R.drawable.report,
R.drawable.stallion,
R.drawable.strategy,
R.drawable.team
};
ImageView youview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),
"card" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
//---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
youview=(ImageView)findViewById(R.drawable.you);
youview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(GalleryActivity.this, You.class);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
这里是我使用的布局: -
<?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:orientation="vertical"
android:background="#006600">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose the card"
android:textSize="20dp"
android:textStyle="bold"/>
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="320dp"
android:layout_height="400dp"
android:layout_above="@+id/gallery1"
android:scaleType="fitXY" />
</RelativeLayout>
我建议在活动中使用的片段。画廊可以在其自己的片段,并将保持不受其余部分的变化影响。在这种情况下,像Master-Detail设计模式这样的东西可能会工作得很好。 – 2014-10-29 15:46:22
我们可以看到你的布局文件吗?我也会使用一个片段,并确保您将片段添加到相对或框架布局,以便它可以出现在图库上方等。 – 2014-10-29 15:48:12