android封装gallery

来源:互联网 发布:nginx配置ssl证书 编辑:程序博客网 时间:2024/05/19 08:23

前言:

android 的UI设计有所见即所得的空间可用,设计UI的时候非常方便。我们在设计UI的时候也应该尽量复用,以提高效率。如整个页面都相同,或只是LIST的内容显示不同,我们应该使用同一个页面,而不应该配置多个页面。但如果只有页面的一部分内容相同,我们又不能公用整个页面,该怎么办呢?
我们可以对这个模块进行封装,只要在该使用的地方把它引入即可。看个封装gallery的小例子。

 

1.封装gallery的类:

package com.D_galleryPackage;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryLayout extends LinearLayout implements OnClickListener, OnItemClickListener{

    private Context mContext;
    private ImageView pre, next;
    private Gallery mGallery;
    private int FILL = ViewGroup.LayoutParams.FILL_PARENT;
    private int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
    private List<Integer> myimage = new ArrayList<Integer>();

    public GalleryLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
         
    }


    public void updateUI(){
        myimage.add(R.drawable.coupons_new);
        myimage.add(R.drawable.profile_ad_icon_new);
        myimage.add(R.drawable.profile_menu_icon_new);
        myimage.add(R.drawable.save_icon_new);
        myimage.add(R.drawable.video_icon_new);
         
        pre = new ImageView(mContext);
        pre.setImageDrawable(getResources().getDrawable(R.drawable.map_point_back));
         
        next = new ImageView(mContext);
        next.setImageDrawable(getResources().getDrawable(R.drawable.map_point_forward));
         
        mGallery = new Gallery(mContext, null);
        ImageAdapter adapter = new ImageAdapter(mContext);
        mGallery.setAdapter(adapter);
        mGallery.setLayoutParams(new LayoutParams(FILL, WRAP_CONTENT, 1));
        setGallerySelection(mGallery);
         
        pre.setOnClickListener(this);
        next.setOnClickListener(this);
        mGallery.setOnItemClickListener(this);
         
        setGravity(Gravity.CENTER);
        addView(pre);
        addView(mGallery);
        addView(next);
    }


    private void setGallerySelection(Gallery gallery) {
        if(myimage.size() >=2){
            gallery.setSelection(1);   
        }
    }


    @Override
    public void onClick(View v) {

        if(v == pre){
            int id = (int) mGallery.getSelectedItemId();
            if(id > 0 ){
               
                Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.push_left_in);
                mGallery.setAnimation(animation);
                mGallery.setAnimationCacheEnabled(true);
                mGallery.setSelection(--id, true);
            }
            
        }else if (v == next) {
            int id = (int) mGallery.getSelectedItemId();
            if(id < (myimage.size() -1)){
                mGallery.setSelection(++id, true);   
            }
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

           new AlertDialog.Builder(mContext).setMessage(String.valueOf(position)).setTitle(\"gallery Click event\").setPositiveButton(\"==OK==\", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
            
        }).show();
         
    }
   
    public class ImageAdapter extends BaseAdapter{

        private Context myContext;

        public ImageAdapter(Context c)
        {
          myContext = c;
        }

        @Override
        public int getCount() {
            return myimage.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView tv = new ImageView(myContext);
            tv.setImageDrawable(getResources().getDrawable(myimage.get(position)));
            tv.setPadding(20, 10, 29, 10);
            tv.setLayoutParams(new Gallery.LayoutParams(WRAP_CONTENT, WRAP_CONTENT ));
            return tv;
        }
    }
}


2.UI中引入:

<com.D_galleryPackage.GalleryLayout
    android:layout_gravity=\"center\"
    android:clickable=\"false\"
    android:focusable=\"false\"
    android:l
    android:id=\"@+id/galleryLayout\"
    android:layout_height=\"75dip\"
    android:layout_width=\"fill_parent\"
    android:background=\"#fff\" />

3.代码中(要使用gallery的地方,和UI搭配)调用:

    galleryLayout = (GalleryLayout) findViewById(R.id.galleryLayout);
    galleryLayout.updateUI();