1.两个布局文件
第一个布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="10dp"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
</LinearLayout>
第二个布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/cell_imageview"
android:layout_width="50dp"
android:layout_height="80dp"
android:scaleType="fitXY"/>
</LinearLayout>
java部分
public class MainActivity extends Activity {
private Gallery gallery;
private ImageView imageview;
private int[] images = {R.drawable.p1,
R.drawable.p2, R.drawable.p3,
R.drawable.p4, R.drawable.p5,
R.drawable.p6,R.drawable.p7};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery);
imageview = (ImageView)findViewById(R.id.imageview);
MyAdapter adapter = new MyAdapter();
gallery.setAdapter(adapter);
gallery.setSelection(Integer.MAX_VALUE/2);
imageview.setImageResource(images[(Integer.MAX_VALUE/2)%images.length]);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
System.out.println("position="+position);
imageview.setImageResource(images[position%images.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater l = LayoutInflater.from(MainActivity.this);
View view = l.inflate(R.layout.cell, null);
ImageView imageview = (ImageView)view.findViewById(R.id.cell_imageview);
imageview.setImageResource(images[position%images.length]);
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}