• 控件: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>

             

  • 相关阅读:
    选择排序
    插入排序
    洗牌算法
    访问性模式
    策略模式
    mysql EXPLAIN Join Types 手册解释 及数据实操
    Nginx Location和Rewrite总结
    json_decode 解析带BOM头文件错误
    laravel 集成 swagger插件
    php S3
  • 原文地址:https://www.cnblogs.com/androidsj/p/2379338.html
Copyright © 2020-2023  润新知