Gallery 画廊使用

首先在界面文件中添加控件
<Gallery 
        android:id="@+id/gallery"
        
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center_vertical"
        android:spacing="20dp"
        android:layout_margin="30dp"
        
        android:clipChildren="false"
        />
 

 

然后在Activity中获取控件并设置适配器 ImageAdapter
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
 
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
        mContext = c;
}
public int getCount() {
        return arrayList.size();
}
public Object getItem(int position) {
        return arrayList.get(position);
}
public long getItemId(int position) {
        return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
        //在这个位置可以选择加载布局或者直接输入图片
if(convertView==null){
convertView=LayoutInflater.from(mContext).inflate(R.layout.item,null);
}
 
return convertView;
}
private Context mContext;
}
@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position,
        long id) {
//mSwitcher.setImageResource(mImageIds[position]);
}
 

 

demo: http://note.youdao.com/yws/public/redirect/share?id=43e64b9bd0cd16956a40fad4befadf12&type=false
Gallery.zip

 

posted on 2016-07-06 16:22  Just_Boy  阅读(208)  评论(0)    收藏  举报