我已经实现了一个Recycler视图,其中的项目在Holder类中单击。每个项目单击都会打开一个新活动。问题是,当用户快速点击两次项目时(非常快的时间非常短),它会打开活动两次。
我无法尝试以下解决方案:
android:launchMode="singleInstance"
startActivityForResult()
调用新活动。android:launchMode="singleTask"
也无效。如何阻止它发生?请建议。
以下是我的适配器类:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case MY_VIEW_TYPE:
View v1 = inflater.inflate(R.layout.created_by_me_item, parent, false);
viewHolder = new ViewHolderCreatedByMe(v1);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolderCreatedByMe) {
// The data setting method is inside the Holder class
((ViewHolderCreatedByMe) holder).setDataCreatedByMe(mFeedRecordListData.get(position));
} else {
((ViewHolderFooter) holder).setFooterView();
}
}
以下是我的持有人类:
public class ViewHolderCreatedByMe extends RecyclerView.ViewHolder implements View.OnClickListener {
// ... some class members are declared here
public ViewHolderCreatedByMe(View itemView) {
super(itemView);
// a few more UI components are initialized here
rl_reaction_item_main_layout = (RelativeLayout) itemView.findViewById(R.id.rl_reaction_item_main_layout);
rl_reaction_item_main_layout.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl_reaction_item_main_layout:
Intent intent = new Intent(mActivity, NewActivity.class);
intent.putExtra(NewActivity.KEY_EVENT_ID, UIModel.getEventID());
intent.putExtra(NewActivity.KEY_EVENT_TIME_STAMP, UIModel.getEventLocalTimeStamp());
intent.putExtra(NewActivity.KEY_EVENT_POSITION, getAdapterPosition());
mFragment.startActivityForResult(intent, Constants.REQUEST_CODE);
break;
}
}
答案 0 :(得分:0)
你应该使用一个布尔标志来检查天气行项目是否被点击。
boolean isItemClicked = false;
private void onRecyclerViewItemClick()
{
if(!isItemClicked)
{
isItemClicked = true;
startActivity(new Intent(this, MyActivity.class));
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isItemClicked = false;
}
*注意 - 我在这里使用名称onRecyclerViewItemClick(),它将替换为listView&on; onItemClick
答案 1 :(得分:0)
创建一个拦截界面,点击您的recyclerview项目视图。
例如:
public interface CustomClickListener {
void onClick(UIModel uiModel, int position);
}
在适配器类中创建一个方法来设置定义的接口:
private CustomClickListener mCustomClickListener;
public void setClickListener( CustomClickListener customClickListener ) {
this.mCustomClickListener = customClickListener;
}
将您的视图持有者类修改为:
public class ViewHolderCreatedByMe extends RecyclerView.ViewHolder implements View.OnClickListener {
// ... some class members are declared here
public ViewHolderCreatedByMe(View itemView) {
super(itemView);
// a few more UI components are initialized here
rl_reaction_item_main_layout = (RelativeLayout) itemView.findViewById(R.id.rl_reaction_item_main_layout);
rl_reaction_item_main_layout.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if( position != RecyclerView.NO_POSITION && mCustomClickListener != null ) {
mCustomClickListener.onClick(UIModel, position);
}
}
在您的片段类中,您将定义适配器,在此处设置单击侦听器:
CustomClickListener customClickListener = new CustomClickListener() {
@Override
void onClick(UIModel uiModel, int position) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra(NewActivity.KEY_EVENT_ID, uiModel.getEventID());
intent.putExtra(NewActivity.KEY_EVENT_TIME_STAMP, uiModel.getEventLocalTimeStamp());
intent.putExtra(NewActivity.KEY_EVENT_POSITION, position);
getActivity().startActivityForResult(intent, Constants.REQUEST_CODE);
adapter.setCustomClickListener(null); //To prevent the event being triggered twice
}
}
adapter.setCustomClickListener( customClickListener );
在您已覆盖onActivityResult的片段中,执行以下操作:
@Override
public void onActivityResult( int requestCode, int resultCode, Intent intent ) {
if( requestCode == Constants.REQUEST_CODE ) {
//Do something with the result
adapter.setCustomClickListener(customClickListener); // Make customClickListener a global variable
}
}
希望这会有所帮助。如果这不符合您的需求,请告诉我您是否找到了解决问题的更好方法。基于布尔值的答案将起作用,但是在打开和关闭应用程序时跟踪布尔值会很麻烦。
答案 2 :(得分:0)
添加这些标记Intent.FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_CLEAR_TOP
您的点击事件应如下所示
case R.id.rl_reaction_item_main_layout:
Intent intent = new Intent(mActivity, NewActivity.class);
intent.putExtra(NewActivity.KEY_EVENT_ID, UIModel.getEventID());
intent.putExtra(NewActivity.KEY_EVENT_TIME_STAMP, UIModel.getEventLocalTimeStamp());
intent.putExtra(NewActivity.KEY_EVENT_POSITION, getAdapterPosition());
//add these following line
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mFragment.startActivityForResult(intent, Constants.REQUEST_CODE);
break;