• 控件:Gallery 1.(自定义适配器)


    发现setAdapter()设置的时候可以使用的子类:ArrayAdapter、SimpleAdapter、BaseAdapter。
    1.BaseAdapter实际上是一个用户自己去实现的操作,包括定义显示的组件等等。

    main.xml

    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id
    ="@+id/MyLayout"
    android:orientation
    ="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent">
    <Gallery
    android:id="@+id/myGallery"
    android:gravity
    ="center_vertical"
    android:spacing
    ="3px"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"/>
    </LinearLayout>

    ImageGalleryAdapter.java

    View Code
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.LinearLayout.LayoutParams;

    public class ImageGalleryAdapter extends BaseAdapter {
    // Context对象
    private Context myContext;
    private int imgRes[] = new int[] {
    R.drawable.ispic_a, R.drawable.ispic_b,
    R.drawable.ispic_c, R.drawable.ispic_d,
    R.drawable.ispic_e };
    // 接收Context
    public ImageGalleryAdapter(Context c) {
    this.myContext = c;
    }
    @Override
    public int getCount() { // 返回图片个数
    return this.imgRes.length;
    }
    @Override
    public Object getItem(int position) { // 取得指定位置的图片
    return this.imgRes[position];
    }
    @Override
    public long getItemId(int position) { // 取得指定位置的图片
    return this.imgRes[position];
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView img = new ImageView(this.myContext);
    img.setBackgroundColor(0xFFFFFFFF);
    // 给ImageView设置资源
    img.setImageResource(this.imgRes[position]);
    // 居中显示
    img.setScaleType(ImageView.ScaleType.CENTER);
    // 布局参数
    img.setLayoutParams(new Gallery.LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT));
    return img;
    }

    }

    MyGalleryDemo.java

    View Code
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Gallery;
    import android.widget.Toast;

    public class MyGalleryDemo extends Activity {
    // 图片浏览
    private Gallery gallery = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 调用布局文件
    super.setContentView(R.layout.main);
    // 取得组件
    this.gallery = (Gallery) super.findViewById(R.id.myGallery) ;
    // 设置图片集
    this.gallery.setAdapter(new ImageGalleryAdapter(this));
    // 设置单击事件
    this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;
    }
    private class OnItemClickListenerImpl implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    // 显示图片编号
    Toast.makeText(MyGalleryDemo.this,
    "" + position, Toast.LENGTH_SHORT).show();
    }

    }
    }

    AndroidManifest.xml

    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="org.lxh.demo"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <uses-sdk android:minSdkVersion="3" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MyGalleryDemo"
    android:label
    ="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    </application>
    </manifest>

             

  • 相关阅读:
    【C++缺省函数】 空类默认产生的6个类成员函数
    iOS 关于 UIKit 专栏应该写在最前面的话
    openssl之EVP系列之8---EVP_Digest系列函数具体解释
    二进制中1的个数
    UVA 548(二叉树重建与遍历)
    Leetcode Best Time to Buy and Sell Stock III
    javascript new Date()函数在不同浏览器上返回不同的值
    【网络流】 HDU 3468 Treasure Hunting
    树莓派玩耍笔记1 -- 开箱 &amp; 安装系统以及简单配置
    POJ 1195 Mobile phones (二维树状数组)
  • 原文地址:https://www.cnblogs.com/androidsj/p/2379338.html
Copyright © 2020-2023  润新知