疯狂猜歌 5-2 实现待选文字框点击事件 自定义的GirdView中,holder的值是a0...a23holder如果定义在getView 里就没事,点击a1就显示a1,但是,如果定义在成员变量里,无论点击哪个按钮,都只是显示a0,求解答以下是源码自定义的=====================================gridView====================== package cn.bashi.GuessMusic.mui;import java.util.ArrayList;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.GridView;import cn.bashi.GuessMusic.R;import cn.bashi.GuessMusic.model.ButtonWorld;import cn.bashi.GuessMusic.model.IWorldButtonClickListener;import cn.bashi.GuessMusic.util.Util;public class MyGridView extends GridView { private ArrayList<ButtonWorld> ButtonWorlds = new ArrayList<ButtonWorld>(); private myBaseAdapter myBase; private Context context; private Animation mScaleAnimation; private IWorldButtonClickListener WorldButtonClickListener; public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; myBase = new myBaseAdapter(); setAdapter(myBase); } public void updateData(ArrayList<ButtonWorld> ButtonWorlds){ this.ButtonWorlds = ButtonWorlds; setAdapter(myBase); } class myBaseAdapter extends BaseAdapter{ @Override public int getCount() { // TODO 自动生成的方法存根 return ButtonWorlds.size(); } @Override public Object getItem(int position) { // TODO 自动生成的方法存根 return ButtonWorlds.get(position); } @Override public long getItemId(int position) { // TODO 自动生成的方法存根 return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ButtonWorld holder; // 加载了所有item后,一起再放动画,动画没放完,没有view if(convertView == null){ // 加载动画 mScaleAnimation = AnimationUtils.loadAnimation(context, R.anim.scale); mScaleAnimation.setStartOffset(position * 100); convertView = Util.getView(context, R.layout.mygridview_item); holder = ButtonWorlds.get(position); holder.setWorldId(position); holder.setViewButtonWorld((Button) convertView.findViewById(R.id.worldBut_item)); holder.getViewButtonWorld().setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 WorldButtonClickListener.onWorldButtonClick(holder); } }); convertView.setTag(holder); }else{ holder = (ButtonWorld) convertView.getTag(); } holder.getViewButtonWorld().setText(holder.getWorldValue()); convertView.startAnimation(mScaleAnimation); return convertView; } } public void regestOnWorldButtonClick(IWorldButtonClickListener listener){ this.WorldButtonClickListener = listener; }}========================================holder================================package cn.bashi.GuessMusic.model;import android.widget.Button;/** 显示的文字按钮*/public class ButtonWorld { private int worldId; private String worldValue; private Button ViewButtonWorld; private boolean isVisibility; public ButtonWorld(){ worldValue = ""; isVisibility = true; } public int getWorldId() { return worldId; } public void setWorldId(int worldId) { this.worldId = worldId; } public String getWorldValue() { return worldValue; } public void setWorldValue(String worldValue) { this.worldValue = worldValue; } public Button getViewButtonWorld() { return ViewButtonWorld; } public void setViewButtonWorld(Button viewButtonWorld) { ViewButtonWorld = viewButtonWorld; } public boolean isVisibility() { return isVisibility; } public void setVisibility(boolean isVisibility) { this.isVisibility = isVisibility; }}==========================================MainActivity ============================package cn.bashi.GuessMusic.ui;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.AnimationUtils;import android.view.animation.LinearInterpolator;import android.widget.Button;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.Toast;import cn.bashi.GuessMusic.R;import cn.bashi.GuessMusic.model.ButtonWorld;import cn.bashi.GuessMusic.model.IWorldButtonClickListener;import cn.bashi.GuessMusic.mui.MyGridView;import cn.bashi.GuessMusic.util.Util;public class MainActivity extends Activity { /** 文字个数*/ public final int WORLD_COUNT = 24; /** 盘片动画*/ private Animation mPanAnim; private LinearInterpolator mPanLinear; /** 拨杆进入动画*/ private Animation mBarInAnim; private LinearInterpolator mBarInLinear; /** 拨杆退出动画*/ private Animation mBarOutAnim; private LinearInterpolator mBarOutLinear; /** 按键*/ private ImageButton mButtonPlay; private ImageView mViewPan; private ImageView mViewBar; private LinearLayout worldSelectView; /** 是否动画正在运行*/ private boolean mIsRunning = false; /** 文字数据*/ private ArrayList<ButtonWorld> ButtonSelectWorlds; private ArrayList<ButtonWorld> ButtonWorlds; /** 自定义的控件*/ private MyGridView myGrid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 盘片 mViewPan = (ImageView) findViewById(R.id.pan); // 摇杆 mViewBar = (ImageView) findViewById(R.id.bar); // 按钮 mButtonPlay = (ImageButton) findViewById(R.id.but_play); // 文字选区 worldSelectView = (LinearLayout) findViewById(R.id.world_select); myGrid = (MyGridView) findViewById(R.id.mgrid); myGrid.regestOnWorldButtonClick(new IWorldButtonClickListener(){ @Override public void onWorldButtonClick(ButtonWorld buttonWorld) { // TODO 自动生成的方法存根 Toast.makeText(MainActivity.this, buttonWorld.getWorldValue(), 1).show(); } }); // 初始化动画 mPanAnim = AnimationUtils.loadAnimation(this, R.anim.rotate); mPanLinear = new LinearInterpolator(); mPanAnim.setInterpolator(mPanLinear); mPanAnim.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mViewBar.setAnimation(mBarOutAnim); mIsRunning = false; mButtonPlay.setVisibility(View.VISIBLE); } }); mBarInAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_45); mBarInAnim.setFillAfter(true); mBarInLinear = new LinearInterpolator(); mBarInAnim.setInterpolator(mBarInLinear); mBarInAnim.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { // 盘片开始转动 mViewPan.startAnimation(mPanAnim); } @Override public void onAnimationRepeat(Animation animation) { } }); mBarOutAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_out_45); mBarOutAnim.setFillAfter(true); mBarOutLinear = new LinearInterpolator(); mBarOutAnim.setInterpolator(mBarOutLinear); // 开始按钮 mButtonPlay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 动画开始 if(!mIsRunning && mViewBar != null){ mIsRunning = true; mButtonPlay.setVisibility(View.GONE); mViewBar.startAnimation(mBarInAnim); } } }); LoadUnderView(); } /** 获取字体的布局*/ public void LoadUnderView(){ // 加载文字选框 LoadWorldSelectView(); // 加载文字待选区 LoadWorldView(); } /** 加载文字选框*/ public void LoadWorldSelectView(){ ButtonSelectWorlds = getWorldsSelectData(); for(int n = 0; n < ButtonSelectWorlds.size(); n++){ worldSelectView.addView(ButtonSelectWorlds.get(n).getViewButtonWorld(), new LayoutParams(140, 140)); } } /** 加载文字待选区*/ public void LoadWorldView(){ ButtonWorlds = getWorldsData(); // 加载数据 myGrid.updateData(ButtonWorlds); } @Override protected void onPause() { mViewPan.clearAnimation(); super.onPause(); } /** 获取文字数据*/ public ArrayList<ButtonWorld> getWorldsSelectData(){ ButtonSelectWorlds = new ArrayList<ButtonWorld>(); ButtonWorld worldSelect; for(int n = 0; n < 4; n++){ worldSelect = new ButtonWorld(); View v = Util.getView(MainActivity.this, R.layout.mygridview_item); worldSelect.setViewButtonWorld((Button)v.findViewById(R.id.worldBut_item)); worldSelect.getViewButtonWorld().setBackgroundResource(R.drawable.game_wordblank); worldSelect.getViewButtonWorld().setText(""); ButtonSelectWorlds.add(worldSelect); } return ButtonSelectWorlds; } /** 获取文字数据*/ public ArrayList<ButtonWorld> getWorldsData(){ ButtonWorlds = new ArrayList<ButtonWorld>(); ButtonWorld World; for(int n = 0; n < WORLD_COUNT; n++){ World = new ButtonWorld(); World.setWorldValue("好"+n); ButtonWorlds.add(World); } return ButtonWorlds; } }==============================================IWorldButtonClickListener ===============package cn.bashi.GuessMusic.model;public interface IWorldButtonClickListener { public void onWorldButtonClick(ButtonWorld buttonWorld);}
1 回答
- 1 回答
- 0 关注
- 1711 浏览
添加回答
举报
0/150
提交
取消