android 封装一个view模块

本文介绍了如何在Android中封装一个自定义的Gallery布局组件,通过创建一个名为GalleryLayout的类,实现图片轮播的功能。内容包括封装的步骤、UI引入方式以及在代码中如何使用这个封装好的组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 封装一个view模块

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值