你好我有两个ListViews活动,我可以先调用第二个活动但是当我关闭第二个活动并返回第一个活动时,OnItemClick不起作用,所以你知道问题在哪里以及如何解决它?
第一项活动onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_words_activity);
list=(ListView) findViewById(R.id.add_words_list);
getClassData();
wordItems=new ArrayList<WordItem>();
adapter=new AddWordsAdapter(this, wordItems);
list.setAdapter(adapter);
list.setOnItemClickListener(itemClick);
list.setOnTouchListener(listViewTouchListener);
}
第一个活动ItemClick
private OnItemClickListener itemClick=new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
isEditWordDilaog=true;
start=words.get(position);
Log.d(start.getWord(),"what the fuck");
end=new WordData();
final int langId=start.getLanguageId();
end.setLanguageId(langId);
showDialog(WORD_DIALOG);
wordViewHolder.language.setImageResource(RProvider.
getLangImageId(languages.get(langId)));
wordViewHolder.word.setText(start.getWord());
wordIndex=position;
}
};
触摸的第一个活动:
private OnTouchListener listViewTouchListener =new OnTouchListener() {
float startX,endX;
boolean isMove=false;
@Override
public boolean onTouch(View view, MotionEvent event) {
int action=event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
startX=event.getX();
break;
case MotionEvent.ACTION_MOVE:
isMove=true;
break;
case MotionEvent.ACTION_UP:
endX=event.getX();
if(isMove==true && startX<endX){
Rect rect=new Rect();
int endY=(int)event.getY();
for(int i=0;i<list.getChildCount();i++){
View child=list.getChildAt(i);
int left=child.getLeft();
int right=child.getRight();
int bottom=child.getBottom();
int top=child.getTop();
rect.set(left, top, right, bottom);
if(rect.contains((int)endX,endY)){
Intent intent=new Intent(AddWordsActivity.this,AddTranslateActivity.class);
int id=words.get(i).getId();
intent.putExtra(Names.WORD_ID,id);
startActivity(intent);
break;
}
}
}
/*
endX=event.getX();
if(startX<endX){
isMove=true;
}*/
break;
}
isLongClick=!isMove;
return isMove;
}
};
在第二个活动中,我使用后退按钮关闭它。比itemClick没有工作
答案 0 :(得分:0)
我认为你不需要OnTouchListener
。除非它正在执行除Intent
之外的其他操作。您的OnClickListener
似乎只设置了一些变量,而不是以Intent
开头的方法。来自Android Developer文档:
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Added in API level 1
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.
我会删除OnTouchListener
并将其放在OnClickListener
:
Intent intent = new Intent(AddWordsActivity.this,AddTranslateActivity.class);
int id = words.get(position).getId();
intent.putExtra(Names.WORD_ID,id);
startActivity(intent);
您可以使用int id
给出的id
替换整个OnClickListener
变量,但不知道您的words
变量是什么,我无法分辨你肯定。
将Intent
声明添加到OnClickListener
表示每次点击该项时,都会启动Intent
。从那里,你添加额外的东西,你就完成了。