我想在relativelayout上设置一个OnTouchListener,它由arrayadapter创建并发送到listview。但是,必须将由OnTouchListener触发的代码写入设置适配器的活动中。是否可以将侦听器设置为适配器中的视图,并在活动中定义(或编码)它?
我的适配器:
data-parsley-trigger="focusout"
TouchInterface:
public class MessageAdapter extends ArrayAdapter<FriendlyMessage> {
TouchInterface touchInterface;
protected AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f );
protected AlphaAnimation fadeIn = new AlphaAnimation( 0.0f , 1.0f );
ViewHolder viewHolder = new ViewHolder();
private static class ViewHolder {
TextView messageText;
TextView nameText;
TextView timeText;
ImageView imageView;
RelativeLayout relativeLayout;
int position;
}
public MessageAdapter(Context context, int resource, List<FriendlyMessage> objects) {
super(context, resource, objects);
}
public void setOnTouchListener(View.OnTouchListener touchListener){
this.touchInterface = (TouchInterface) touchListener;
viewHolder.relativeLayout.setOnTouchListener(touchListener);
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
FriendlyMessage message = getItem(position);
if (message.getPhotoUrl() != null){
if (Objects.equals(message.getKey(), String.valueOf(Build.SERIAL))){
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_image_right, null);
viewHolder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.rel);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.photoImageViewR);
viewHolder.nameText = (TextView) convertView.findViewById(R.id.nameTextView);
viewHolder.timeText = (TextView) convertView.findViewById(R.id.tvtimeimg);
Picasso.with(viewHolder.imageView.getContext())
.load(message.getPhotoUrl())
.into(viewHolder.imageView);
convertView.setTag(viewHolder);
}
else if (!Objects.equals(message.getKey(), String.valueOf(Build.SERIAL))){
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_image_left, null);
viewHolder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.rel);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.photoImageViewL);
viewHolder.nameText = (TextView) convertView.findViewById(R.id.nameTextView);
viewHolder.timeText = (TextView) convertView.findViewById(R.id.tvtimeimg);
Picasso.with(viewHolder.imageView.getContext())
.load(message.getPhotoUrl())
.into(viewHolder.imageView);
convertView.setTag(viewHolder);
}
}
else if (message.getPhotoUrl() == null){
if (Objects.equals(message.getKey(), String.valueOf(Build.SERIAL))) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_text_right, null);
viewHolder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.rel);
viewHolder.messageText = (TextView) convertView.findViewById(R.id.messageTextView);
viewHolder.nameText = (TextView) convertView.findViewById(R.id.nameTextView);
viewHolder.timeText = (TextView) convertView.findViewById(R.id.tvtime);
viewHolder.messageText.setText(message.getText());
convertView.setTag(viewHolder);
} else if (!Objects.equals(message.getKey(), String.valueOf(Build.SERIAL))) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_text_left, null);
viewHolder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.rel);
viewHolder.messageText = (TextView) convertView.findViewById(R.id.messageTextView);
viewHolder.nameText = (TextView) convertView.findViewById(R.id.nameTextView);
viewHolder.timeText = (TextView) convertView.findViewById(R.id.tvtime);
viewHolder.messageText.setText(message.getText());
convertView.setTag(viewHolder);
}
}
//some other code here
return convertView;
}}
我的活动,我想调用监听器(它是一个很长的代码,所以我删除了一些现在不重要的部分):
public interface TouchInterface {
void touch(View.OnTouchListener onTouchListener);}
答案 0 :(得分:0)
如果我理解你正确的问题,你想在列表元素上设置一个点击监听器来执行父活动的方法(我猜你会传递相应的参数)。如果我得到了正确的结果,你应该在父活动类型的Adapter
类中定义一个变量。像
private MessageActivity activity;
然后在你的Adapter
构造函数
activity = (MessageActivity) context;
然后,当您在视图上设置点击监听器时,您应该执行类似
的操作view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
activity.YourOnTouchMethodImplementation();
}
});