当前位置:编程学习 > wap >>

android 重写用来显示Listview的BaseAdapter后,通过ImageButton监听实现卡片翻转 ,不成功。。

--------------------编程问答-------------------- 没有人嘛?我知道我错在哪里了。是在BaseAdapter类时,里面的position有问题。我加载内容到listview后,显示的按钮返回的position竟然是没有规律的。就是一共10条数据,不是从0,1,2,3.....
而是错乱的。请问这个问题,是我的BaseAdapter类写错了嘛? --------------------编程问答-------------------- 能不能贴出完整的BaseAdapter的整个类。。。 --------------------编程问答--------------------
引用 2 楼 heaimnmn 的回复:
能不能贴出完整的BaseAdapter的整个类。。。


完整的BaseAdapter 如下:
package com.team.adapter;

import
...

public class CardAdapter extends BaseAdapter {
private ArrayList<HashMap<String, Object>> testData;
private LayoutInflater inflater;// 这个一定要懂它的用法及作用
private Context mContext;


// 构造函数:要理解(这里构造方法的意义非常强大,你也可以传一个数据集合的参数,可以根据需要来传参数)
public CardAdapter(Context context, ArrayList<HashMap<String, Object>> testData) {
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.testData = testData;
}

// 这里的getCount方法是程序在加载显示到ui上时就要先读取的,这里获得的值决定了listview显示多少行
@Override
public int getCount() {
// 在实际应用中,此处的返回值是由从数据库中查询出来的数据的总条数
return testData.size();
}

// 根据ListView所在位置返回View
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
//return testData.get(position);
return null;
}

// 根据ListView位置得到数据源集合中的Id
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
//return position;
return 0;
}

public final class ViewHolder {
// 卡片背景
//public ImageView cardImage;
// 卡片正面
public ImageButton back;
public ImageButton forward;
public ImageView editBtn;
public ImageView cardLogo;
public TextView cardId;
public TextView cardDate;
public TextView cardUser;
public TextView cardPublisher;
public TextView cardSecureCode;
public ImageView cardType;
// 卡片背面
public ImageView cardStrip;
public TextView cardOtherName1;
public TextView cardOtherValue1;
public TextView cardOtherName2;
public TextView cardOtherValue2;
public TextView cardOtherName3;
public TextView cardOtherValue3;
// 布局
public ViewGroup mContainer;
public RelativeLayout frontLayout;
public RelativeLayout backLayout;
//public ExecutorService exectutorService;

}

// 有很多例子中都用到这个holder,理解下??
ViewHolder holder = null;

// 重写adapter最重要的就是重写此方法,此方法也是决定listview界面的样式的
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;

//观察convertView随ListView滚动情况             
        Log.v("MyListViewBase", "getView " + position + " " + convertView);

// 思考这里为何要判断convertView是否为空 ??
if (convertView == null) {

holder = new ViewHolder();

// 把listview_item layout转换成View【LayoutInflater的作用】
convertView = inflater.inflate(R.layout.card, null);
Log.v("MyListViewBase", "inflater " + convertView);
 
holder.mContainer = (ViewGroup) convertView
.findViewById(R.id.container);
holder.mContainer.setTag(position);
holder.frontLayout = (RelativeLayout) convertView
.findViewById(R.id.frontLayout);
holder.backLayout = (RelativeLayout) convertView
.findViewById(R.id.backLayout);

holder.mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);

// 通过上面layout得到的view来获取里面的具体控件
holder.back = (ImageButton) convertView.findViewById(R.id.back);
holder.forward = (ImageButton) convertView.findViewById(R.id.forward);

holder.editBtn = (ImageView) convertView
.findViewById(R.id.editBtn);

holder.cardLogo = (ImageView) convertView
.findViewById(R.id.cardLogo);
holder.cardId = (TextView) convertView.findViewById(R.id.cardId);
holder.cardDate = (TextView) convertView
.findViewById(R.id.cardDate);
holder.cardUser = (TextView) convertView
.findViewById(R.id.cardUser);
holder.cardPublisher = (TextView) convertView
.findViewById(R.id.cardPublisher);
holder.cardSecureCode = (TextView) convertView
.findViewById(R.id.cardSecureCode);
holder.cardType = (ImageView) convertView
.findViewById(R.id.cardType);

holder.cardStrip = (ImageView) convertView
.findViewById(R.id.cardStrip);
holder.cardOtherName1 = (TextView) convertView
.findViewById(R.id.cardOtherName1);
holder.cardOtherValue1 = (TextView) convertView
.findViewById(R.id.cardOtherValue1);
holder.cardOtherName2 = (TextView) convertView
.findViewById(R.id.cardOtherName2);
holder.cardOtherValue2 = (TextView) convertView
.findViewById(R.id.cardOtherValue2);
holder.cardOtherName3 = (TextView) convertView
.findViewById(R.id.cardOtherName3);
holder.cardOtherValue3 = (TextView) convertView
.findViewById(R.id.cardOtherValue3);

//animate(holder.mContainer).setDuration(1000);

//绑定ViewHolder对象   
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 这里testData.get(position).get("cardImage")),其实就是从list集合(testData)中取出对应索引的map,然后再根据键值对取值

holder.editBtn.setBackgroundResource((Integer) testData.get(position)
.get("editBtn"));
holder.cardLogo.setBackgroundResource((Integer) testData.get(position)
.get("cardLogo"));
holder.cardId.setText((String) testData.get(position).get("cardId"));
holder.cardDate
.setText((String) testData.get(position).get("cardDate"));
holder.cardUser
.setText((String) testData.get(position).get("cardUser"));
holder.cardPublisher.setText((String) testData.get(position).get(
"cardPublisher"));
holder.cardSecureCode.setText((String) testData.get(position).get(
"cardSecureCode"));
holder.cardStrip.setBackgroundResource((Integer) testData.get(position)
.get("cardStrip"));

holder.cardType.setBackgroundResource((Integer) testData.get(position)
.get("cardType"));

/*holder.frontLayout.setBackgroundResource((Integer) testData.get(position)
.get("cardImage"));
holder.backLayout.setBackgroundResource((Integer) testData.get(position)
.get("cardImage"));*/
holder.mContainer.setBackgroundResource((Integer) testData.get(position)
.get("cardImage"));

String cardOtherName1s = (String) testData.get(position).get(
"cardOtherName1");
String cardOtherValue1s = (String) testData.get(position).get(
"cardOtherValue1");
String cardOtherName2s = (String) testData.get(position).get(
"cardOtherName2");
String cardOtherValue2s = (String) testData.get(position).get(
"cardOtherValue2");
String cardOtherName3s = (String) testData.get(position).get(
"cardOtherName3");
String cardOtherValue3s = (String) testData.get(position).get(
"cardOtherValue3");

holder.back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//applyRotation(0,90,R.id.backLayout);
animate(holder.mContainer).rotationYBy(180);
Log.v("MyListViewBase", "你点击了按钮back" + position);//打印Button的点击信息 
Toast.makeText(mContext, "单击back"+position, Toast.LENGTH_SHORT).show();  
}
});
// 为forward添加click监听
holder.forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//applyRotation(-1,180,R.id.frontLayout);
animate(holder.mContainer).rotationYBy(180);
Log.v("MyListViewBase", "你点击了按钮forward" + position);//打印Button的点击信息 
Toast.makeText(mContext, "单击forward"+position, Toast.LENGTH_SHORT).show();  
}
});

return convertView;
}

private void applyRotation(float start, float end, final int viewId) {
final float centerX = holder.mContainer.getWidth() / 2.0f;
final float centerY = holder.mContainer.getHeight() / 2.0f;
Rotate3d rotation = new Rotate3d(start, end, centerX, centerY, 200.0f,
true);
rotation.setDuration(500);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {

holder.mContainer.post(new Runnable() {
@Override
public void run() {
if (viewId == R.id.frontLayout) {
System.out.println("sdfsdfsd1");
holder.frontLayout.setVisibility(View.GONE);
holder.backLayout.setVisibility(View.VISIBLE);
holder.backLayout.setFocusable(true);
} else if (viewId == R.id.backLayout) {
System.out.println("sdfsdfsd2");
holder.backLayout.setVisibility(View.GONE);
holder.frontLayout.setVisibility(View.VISIBLE);
holder.frontLayout.setFocusable(true);
}
Rotate3d rotatiomAnimation = new Rotate3d(-90, 0,
centerX, centerY, 200.0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation
.setInterpolator(new DecelerateInterpolator());

holder.mContainer.startAnimation(rotatiomAnimation);
}
});

}

@Override
public void onAnimationRepeat(Animation arg0) {
}

@Override
public void onAnimationStart(Animation arg0) {

}
});
holder.mContainer.startAnimation(rotation);
}


}
补充:移动开发 ,  Android
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,