[url]http://www.ophonesdn.com/forum/thread-2918-1-1.html[/url]

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 myimage = new ArrayList();

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中引入:

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

更多相关文章

  1. Android(安卓)WebView使用深入浅出
  2. Android成长(三)——页面布局
  3. Qt5.2发布了
  4. Android(安卓)HAL(硬件抽象层)介绍以及调用
  5. Android(安卓)Makefile and build system 分析与梳理
  6. Android(安卓)NDK编程之Android.mk和Application.mk
  7. 基于 Android(安卓)NDK 的学习之旅-----Android.mk 介绍
  8. Android动态部署一:Google原生Split APK浅析
  9. Android模块化和组件化开发简单理解(一)

随机推荐

  1. Android 之 Dialog复选框获取值 .
  2. Android P正式发布,你需要尽快做适配了
  3. android studio 3.0 di 注入插件引入错误
  4. API 23及之后版本关于用android studio
  5. android 连接CMWAP
  6. android导出通讯录,通话记录,短信
  7. android rating bar style
  8. Android开发学习笔记:我的第一个Android程
  9. android 关于再按一次退出程序效果
  10. 【Android】Http请求