(转载请声明出处:http://www.cnblogs.com/linguanh/)
前言:
接上次分享的 ListView 动态加载类,入口:http://www.cnblogs.com/linguanh/p/4645115.html 这次分享给大家的是,刚些写好的类似社交软件打字时地下弹出的表情布局。 先看下我的默认效果图。
该效果图里面使用的图片资源,是默认的IC_lanucher,在我的类里面,你可以自定义,包括布局,几行几列,什么的,都可以自定义。底下的是小点标。
下集预告:我将在使用这个框架的前提下实现,在输入框中添加表情图片,实现评论、发帖功能!
使用简介:
无论你是使用我提供的默认功能,还是使用接口自定义自己的功能,不变的有,它从底部弹出,拥有点标随ViewPager侧滑而跟踪显示,拥有侧滑功能。
你可以做的是:
1,使用我默认的布局,图片,页面;
2,自定义所有图片,自定义布局,自定义行列数,自定义页面数目,自定义ViewPager 里面要显示的 View,自定义所有的点击,触屏事件。
共有3个类,分别是GridViewActivity、GridViewPagerAdapter、GridViewConfigtion。
关系:你的Activity先实例化GridViewActivity,然后它会实例化GridViewPagerAdapter,最后的GridViewConfigtion被Adapter实例化,你也可以单独使用最后一个。
分别负责:
底部弹出的父布局配置和先行数据配置,例如默认的行列计算;
ViewPager 数据适配器;
ViewPager 中所要显示的页面的配置。
GridViewActivity:
该类没继承Activity,它采用动态引入我定义的xml 文件,里面FrameLayout是所有View的父布局,它将会包裹 ViewPager 和底下的 点标 父亲布局。
1,共设置两个构造函数:
第一个是供用户你选择自己定义 ViewPager 里面的子页面时调用的,需要你传入上下文context 和页面数目;
第二个是在采用我提供的默认布局时,调用的,所谓默认布局就是如上图所示,只有一张表情图片,有行,有列,需要你传 入上下文、和是否采用默认行列算法的boolean值。因为我在此类还留有你在选择默认布局时的自定义行列计算算法 接口。
2,底下点标的的配置和显示也在此类内完成;
3,ViewPager 的OnPageChangeListener 接口也在此类实现,在这里的目的是实现 点标的变化;
4,所留接口,customConfigGridViewListener,作用是让你自己定义所以要显示的表情图片,点标图片,行列数:
共重写4个函数:
public void setCustomGridSize(); 设置自定义行、列、页数目,
public int[] setIntExpressionImages(); 设置传入的图片类型,这里是图片id数组
public Bitmap[] setBitmapExpressionImages(); 这里是bitmap类型
public int[] setIntPointImages(); 点标图片,int数组类型
public Bitmap[] setBitmapPointImages(); 点标数组,bitmap类型。
注意!上述自定义的自定义图片,二选一,若两种都传入,则优先覆盖Bitmap的。setCustomGridSize()要求你必须要设置好行、列、页数。
GridViewPagerAdapter:
该类继承于 PagerAdapter,主要负责页面数目的配置。
1,三个构造函数:
第一个是供用户你选择自己定义 ViewPager 里面的子页面时调用的,需要你传入上下文context 和页面数目;
第二个和第三个都是你选择默认布局时要调用的,区别在于,一个传入是int数组型图片id,另一个是Bitmap。
GridViewConfigtion:
这个类只继承了两个接口,一个是onClick,另一个是onTouch,因为它只要是负责构建 ViewPager 里面要显示的 View。
1,三个构造函数,具体意义见 GridViewPagerAdapter 的构造函数介绍;
2,所留接口共两个,自定义页面接口 addCustomView,默认页面几点、Touch事件接口。
注意!如果你使用的是自定义的View,那么请在这个接口的函数里面连带 点击或者触屏事件写进去。
用法介绍:
我标题起的是三行代码接入。就是这么简单。首先你需要在你自己的 activity、fragment或者其他,实例化一个GridViewActivity 对象,然后使用该对象的 getFather()函数,就能返回整个View了。
最后一行是,实现自定义监听事件,显示继承GridViewConfigtion.addDefaultViewClick 接口,
然后使用这句绑定接口:
GridViewActivity 对象.myVPAdapter.gridViewConfiger.setDefaultViewClickListener(this); 即可。
其他的自定义,你要设置就自己实现接口,重写即可。
代码:
按照顺序。提示,GridViewActivity 里面默认的 R.drawable.ic_launcher 是我R文件里面的,如果提示找不到就自己改了。
package com.lgh.addItemListview.mymodule.app2; import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout; /**
* Created by LinGuanHong(林冠宏) on 2015/7/17.
*/
public class GridViewActivity{ private View gva;
private Boolean isFristTime = true;//是否第一次进入到 ViewPager
private customConfigGridViewListener coustomConfigtion;
private Boolean isSetDefault = false;//用户是否使用默认的 行列数计算,默认是否
private Context context;
private FrameLayout father;
private Animation animShow;
private ViewPager myViewPager;
public GridViewPagerAdapter myVPAdapter;
private LinearLayout container,secondFather,thridFather; public int intTypeEachPagerLines=0;
public int intTypeEachPagerColumns=0;
public int intTypeViewPagerSize;//一共有多少页 public int[] expressionImage = null;
public int[] pointImages = null;
public Bitmap[] expressionBitmaps = null;
public Bitmap[] pointBitmaps = null; public GridViewActivity(Context context,int pagerSize){ // viewPager里面的页面完全自定义构造函数
this.context = context;
this.intTypeViewPagerSize = pagerSize;
if(coustomConfigtion!=null) { //先判断店标图片接口对象是否是空
pointBitmaps = getCustomGridViewListener().setBitmapPointImages();
pointImages = getCustomGridViewListener().setIntPointImages();
} //点标布局是全局的,不属于ViewPager,所以这里也要加入
if (pointImages == null && pointBitmaps == null) {
setDefaultPointImages(); //启用默认点标 图片
}
initAll();
} public GridViewActivity(Context context,Boolean isSetDefault){
this.context = context;
this.isSetDefault = isSetDefault; //采用先调用接口,再判断,防止用户传入空数组,且,二选一,若两种都传入,则优先覆盖Bitmap的
if(coustomConfigtion!=null) { //先判断接口对象是否是空
expressionBitmaps = getCustomGridViewListener().setBitmapExpressionImages();
expressionImage = getCustomGridViewListener().setIntExpressionImages();
pointBitmaps = getCustomGridViewListener().setBitmapPointImages();
pointImages = getCustomGridViewListener().setIntPointImages(); }
if (expressionImage == null && expressionBitmaps == null) {
setDefaultExpressionImages(); //启用默认表情图片
}
if (pointImages == null && pointBitmaps == null) {
setDefaultPointImages(); //启用默认点标 图片
} if(this.isSetDefault){//判断用户是否使用默认 行、列、页
setDefaultGridSize();//启用默认行列页配置
}else{
//如果用户没采用默认的,那么调用接口,该接口要求用户必须自定义设置:行数,页数,列数
getCustomGridViewListener().setCustomGridSize();
}
initAll();
} private void initAll(){
gva = LayoutInflater.from(context).inflate(R.layout.gridview,null,true);
myViewPager = new ViewPager(context); animShow = new TranslateAnimation( //设置 弹出动画,这个不想提供接口,你想的话就改我类吧
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0);
animShow.setDuration(500); if(expressionImage!=null){ //如果 是 int 型图片id 数组,采用第一类构造函数
myVPAdapter = new GridViewPagerAdapter
(
context,
expressionImage,//传入 表情 图片
intTypeEachPagerLines,//行数
intTypeEachPagerColumns,//列数
intTypeViewPagerSize//页数
);
}else if(expressionBitmaps!=null){
myVPAdapter = new GridViewPagerAdapter
(
context,
expressionBitmaps,//传入 表情 图片
intTypeEachPagerLines,//行数
intTypeEachPagerColumns,//列数
intTypeViewPagerSize//页数
);
}else{ //第三类构造,整个 ViewPager 页面,你来自定义
myVPAdapter = new GridViewPagerAdapter
(
context,
intTypeViewPagerSize//页数
);
}
if(gva!=null) {
container = (LinearLayout) gva.findViewById(R.id.container);
container.setMinimumHeight(200);
secondFather = (LinearLayout) gva.findViewById(R.id.secondFather);
secondFather.setMinimumHeight(180);
thridFather = (LinearLayout) gva.findViewById(R.id.thridFather);
thridFather.setMinimumHeight(20);
}
//设置滑动时的小标记点
LinearLayout.LayoutParams layoutParamTypePointsSize =
new LinearLayout.LayoutParams
(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
LinearLayout pointLinear = new LinearLayout(context);
pointLinear.setLayoutParams(layoutParamTypePointsSize);
pointLinear.setGravity(Gravity.CENTER_HORIZONTAL);
final ImageView[] points = new ImageView[intTypeViewPagerSize];
for (int k =0;k<points.length;k++){
points[k] = new ImageView(context);
points[k].setScaleType(ImageView.ScaleType.CENTER_INSIDE);
if(pointImages!=null){
points[k].setImageResource(pointImages[0]);
}else{
points[k].setImageBitmap(pointBitmaps[0]);
}
LinearLayout.LayoutParams pointSize = new LinearLayout.LayoutParams(10, 10);
pointSize.setMargins(5, 5, 5, 5);
points[k].setLayoutParams(pointSize);
pointLinear.addView(points[k]);
} myViewPager.setAdapter(myVPAdapter);
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//设置首次显示时,第一个点标变色,onPageSelected 第一次进入是不会取到 position 的
if(position==0 && isFristTime){
if(pointImages!=null) {
points[position].setImageResource(pointImages[1]);
}
}
} @Override
public void onPageSelected(int position) {
isFristTime = false;
Log.d("", "position is " + position);
if(pointImages!=null){
if (null != points[position]) {//当前页下标 变色
points[position].setImageResource(pointImages[1]);
}
//当前页的前一页和后一页恢复颜色
if (((position - 1) != -1 && (position + 1) < points.length)) {//中间
points[position - 1].setImageResource(pointImages[0]);
points[position + 1].setImageResource(pointImages[0]);
}
if ((position - 1) != -1 && (position + 1) == points.length) {//最右边
points[position - 1].setImageResource(pointImages[0]);
}
if ((position - 1) == -1 && (position + 1) < points.length) {//最左边
points[position + 1].setImageResource(pointImages[0]);
}
}else{
if (null != points[position]) {//当前页下标 变色
points[position].setImageBitmap(pointBitmaps[1]);
}
//当前页的前一页和后一页恢复颜色
if (((position - 1) != -1 && (position + 1) < points.length)) {//中间
points[position - 1].setImageBitmap(pointBitmaps[0]);
points[position + 1].setImageBitmap(pointBitmaps[0]);
}
if ((position - 1) != -1 && (position + 1) == points.length) {//最右边
points[position - 1].setImageBitmap(pointBitmaps[0]);
}
if ((position - 1) == -1 && (position + 1) < points.length) {//最左边
points[position + 1].setImageBitmap(pointBitmaps[0]);
}
}
} @Override
public void onPageScrollStateChanged(int state) { }
});
secondFather.addView(myViewPager);
thridFather.addView(pointLinear);
father = (FrameLayout) gva.findViewById(R.id.father);
father.clearAnimation();
father.startAnimation(animShow);//设置弹出的动画
} public void setCoustomGridViewListener(customConfigGridViewListener coustomConfigtion){
this.coustomConfigtion = coustomConfigtion;
}
private customConfigGridViewListener getCustomGridViewListener(){
return coustomConfigtion;
} private void setDefaultGridSize(){
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
float phoneScreen = wm.getDefaultDisplay().getWidth()/wm.getDefaultDisplay().getHeight();
if(phoneScreen<0.6){
intTypeEachPagerLines=3;
intTypeEachPagerColumns=3;
//imageSizeHeight = context.getResources().getDrawable(expressionImage[0]).getIntrinsicHeight()*3;
}else if(phoneScreen<0.75){
intTypeEachPagerLines=3;
intTypeEachPagerColumns=2;
//imageSizeHeight = context.getResources().getDrawable(expressionImage[0]).getIntrinsicHeight()*3;
}else{
intTypeEachPagerLines = 1;
//imageSizeHeight = context.getResources().getDrawable(expressionImage[0]).getIntrinsicHeight()+10;
if (phoneScreen >= 1.75) {
intTypeEachPagerColumns = 6;
} else if (phoneScreen >= 1.5) {
intTypeEachPagerColumns = 5;
} else if (phoneScreen >= 1.3) {
intTypeEachPagerColumns = 4;
} else {
intTypeEachPagerColumns = 3;
}
}
//计算出 ViewPager 含多少页
intTypeViewPagerSize = expressionImage.length/(intTypeEachPagerLines*intTypeEachPagerColumns);
//上面是取整
if(expressionImage.length%(intTypeEachPagerColumns*intTypeEachPagerColumns)>0){
//如果还有余数就增 1
intTypeViewPagerSize++;
}
} public FrameLayout.LayoutParams getParams(){
FrameLayout.LayoutParams fatherSize = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
fatherSize.gravity=Gravity.BOTTOM;
return fatherSize;
} public FrameLayout getFather(){
return father;
} public interface customConfigGridViewListener{
public void setCustomGridSize();
public int[] setIntExpressionImages();
public Bitmap[] setBitmapExpressionImages();
public int[] setIntPointImages();
public Bitmap[] setBitmapPointImages();
} private void setDefaultExpressionImages(){ // 配置默认表情图片集
expressionImage = new int[]{ //默认
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher, com.lgh.addItemListview.mymodule.app2.R.drawable.ic_launcher,
}; }
private void setDefaultPointImages() { // 配置默认点标图片集
pointImages = new int[]{//默认
com.lgh.addItemListview.mymodule.app2.R.drawable.gray_point,
com.lgh.addItemListview.mymodule.app2.R.drawable.white_point
};
}
}
第二个
package com.lgh.addItemListview.mymodule.app2; import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup; import java.util.ArrayList;
import java.util.List; /**
* Created by LinGuanHong(林冠宏) on 2015/7/17.
* 在这里设置有多少页 ViewPager 的 childen
*/
public class GridViewPagerAdapter extends PagerAdapter { public GridViewConfigtion gridViewConfiger;
private int intTypeEachPagerLines=0;
private int intTypeEachPagerColumns=0;
private int intTypeViewPagerSize;//一共有多少页
private List<View> gridViewLinears;
private Context context;
private int[] expressionImage = null;
private Bitmap[] expressionBitmaps = null; public GridViewPagerAdapter(Context context,int pagerSize){ //ViewPager 页面自定义构造
this.context = context;
this.intTypeViewPagerSize = pagerSize;
initAll();
} public GridViewPagerAdapter(Context context,int [] expressionImage,int line,int column,int pagerSize){
this.context = context;
this.expressionImage = expressionImage;
this.intTypeEachPagerLines = line;
this.intTypeEachPagerColumns = column;
this.intTypeViewPagerSize = pagerSize;
initAll();
} public GridViewPagerAdapter(Context context,Bitmap [] expressionBitmaps,int line,int column,int pagerSize){
this.context = context;
this.expressionBitmaps = expressionBitmaps;
this.intTypeEachPagerLines = line;
this.intTypeEachPagerColumns = column;
this.intTypeViewPagerSize = pagerSize;
initAll();
} private void initAll(){
if(intTypeEachPagerLines!=0 && intTypeEachPagerColumns!=0){
if(expressionImage!=null){
gridViewConfiger = new GridViewConfigtion
(
context,
intTypeEachPagerLines,//传进去 行数
intTypeEachPagerColumns,//传进去 列数
expressionImage//传进去 表情图片数组
);
}else if(expressionBitmaps!=null){
gridViewConfiger = new GridViewConfigtion
(
context,
intTypeEachPagerLines,//传进去 行数
intTypeEachPagerColumns,//传进去 列数
expressionBitmaps//传进去 表情图片数组
);
}else{
gridViewConfiger = new GridViewConfigtion
(
context
);
}
gridViewLinears = new ArrayList<View>();
addGridViews();
}
} private void addGridViews(){
for(int i = 0 ; i < intTypeViewPagerSize;i++){
gridViewLinears.add(gridViewConfiger.getGridViews()); }
} @Override
public int getCount() {
return gridViewLinears.size();//返回一共有多少页
} @Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
container.removeView(gridViewLinears.get(position));
} @Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(gridViewLinears.get(position));
return gridViewLinears.get(position);
} @Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
第三个
package com.lgh.addItemListview.mymodule.app2; import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout; /**
* Created by LinGuanHong(林冠宏) on 2015/7/17.
*
*/
public class GridViewConfigtion implements View.OnClickListener,View.OnTouchListener{ private addCustomView myCustonView;
private addDefaultViewClick myOnClilkTouch;
private Context context;
private int imagesIndex = -1;//统计已经加入九宫格的图片数量
private int intTypeEachPagerLines=0;
private int intTypeEachPagerColumns=0; private int[] expressionImage = null;
private Bitmap[] expressionBitmaps = null; //下面3个 构造函数 所占内存不会增加,除非用户你3个都使用,否则只是增加了 几行 代码
public GridViewConfigtion( //采用 页面自定义构造函数,
Context context)
{
this.context = context;
} public GridViewConfigtion( //采用 int id 型的图片构造函数,
Context context,int intTypeEachPagerLines,int intTypeEachPagerColumns,int[] expressionImage)
{
this.context = context;
this.expressionImage = expressionImage;
this.intTypeEachPagerLines = intTypeEachPagerLines;
this.intTypeEachPagerColumns = intTypeEachPagerColumns;
}
public GridViewConfigtion(//采用 Bitmap 型的图片构造函数
Context context,int intTypeEachPagerLines,int intTypeEachPagerColumns,Bitmap[] expressionBitmaps)
{
this.context = context;
this.expressionBitmaps = expressionBitmaps;
this.intTypeEachPagerLines = intTypeEachPagerLines;
this.intTypeEachPagerColumns = intTypeEachPagerColumns;
} public void setCustomViewListener(addCustomView myCustonView){
this.myCustonView = myCustonView;
} public void setDefaultViewClickListener(addDefaultViewClick myOnClilkTouch){
this.myOnClilkTouch = myOnClilkTouch;
} private addCustomView getCustomViewListener(){
return myCustonView;
} private addDefaultViewClick getDefaultViewClickListener(){
return myOnClilkTouch;
} public View getGridViews(){
View result = null;
if(myCustonView!=null) {
result = getCustomViewListener().setCustomView();
}
if(result==null){
return getDefaultView();
}else{
return result;
}
} private View getDefaultView(){
LinearLayout resultViews = new LinearLayout(context);
LinearLayout.LayoutParams layoutParamTypeResultViewSize =
new LinearLayout.LayoutParams
(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
resultViews.setLayoutParams(layoutParamTypeResultViewSize);
resultViews.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParamTypeLineSize =
new LinearLayout.LayoutParams
(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
layoutParamTypeLineSize.weight = 1;
//layoutParamTypeLineSize.gravity=LinearLayout.VERTICAL;
LinearLayout.LayoutParams layoutParamTypeViewSize =
new LinearLayout.LayoutParams
(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
layoutParamTypeViewSize.weight = 1;
for(int i =0;i<intTypeEachPagerLines;i++){
LinearLayout eachLineLinear = new LinearLayout(context);
eachLineLinear.setLayoutParams(layoutParamTypeLineSize);
eachLineLinear.setPadding(5, 0, 5, 0);
resultViews.addView(eachLineLinear);//把行,父View添加到当前的grid中
for(int j=0;j<intTypeEachPagerColumns;j++){
Log.d("----","index is "+imagesIndex);
imagesIndex++;
if(imagesIndex>=expressionImage.length){
//到这里,已全部加完
break;
}
LinearLayout eachExpressionLinear = new LinearLayout(context);//表情的直系父View
eachExpressionLinear.setGravity(Gravity.CENTER);//设置子控件居中
eachExpressionLinear.setLayoutParams(layoutParamTypeViewSize);
ImageView imageViewTypeExpression = new ImageView(context);
imageViewTypeExpression.setOnClickListener(this);//加上监听事件
if(expressionImage!=null) { //int的不是空,证明 不是 bitmap
imageViewTypeExpression.setImageResource(expressionImage[imagesIndex]);
}else{
imageViewTypeExpression.setImageBitmap(expressionBitmaps[imagesIndex]);
}
eachExpressionLinear.addView(imageViewTypeExpression);//添加到 直系父 View
eachLineLinear.addView(eachExpressionLinear); // 把图片的直系View 添加到 行View
}
}//所有行的结束,组成一页 gridView
return resultViews;
} @Override
public void onClick(View v) {
if(myOnClilkTouch!=null) {
getDefaultViewClickListener().setOnClick(v);
}
} @Override
public boolean onTouch(View v, MotionEvent event) {
return myOnClilkTouch != null && getDefaultViewClickListener().setOnTouch(v, event);
} public interface addCustomView{ //自定义页面接口
public View setCustomView();
} public interface addDefaultViewClick{
public boolean setOnTouch(View v, MotionEvent event); // onTouch 接口
public void setOnClick(View v); //onClick 接口
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/father"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#64cfcfcf"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/container"
android:layout_gravity="bottom"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp">
<LinearLayout
android:id="@+id/secondFather"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp"> </LinearLayout>
<LinearLayout
android:id="@+id/thridFather"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </LinearLayout> </LinearLayout> </FrameLayout>
测试例子 activity
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast; import com.lgh.addItemListview.mymodule.app2.GridViewActivity;
import com.lgh.addItemListview.mymodule.app2.GridViewConfigtion; public class MainActivity extends Activity implements GridViewConfigtion.addDefaultViewClick { private GridViewActivity grid; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
grid = new GridViewActivity(this,true);
addContentView(grid.getFather(),grid.getParams());
//设置每张表情图片的点击,触屏监听,如果你选择的是完全自定义,请别这样做
//完全自定义的 ViewPager 页面,请在接口处设置监听对象
grid.myVPAdapter.gridViewConfiger.setDefaultViewClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(com.lgh.addItemListview.mymodule.app2.R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == com.lgh.addItemListview.mymodule.app2.R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} @Override
public boolean setOnTouch(View v, MotionEvent event) {
// 自定义onTouch
return false;
} @Override
public void setOnClick(View v) {
//我这里自定义了 ImageView 的点击事件,在继承我的接口的前提下
Toast.makeText(this,"i am the fucking picture!",Toast.LENGTH_LONG).show();
}
}