gpt4 book ai didi

android - 试图将我选择的 listItem 移动到 recyclerview 的顶部

转载 作者:行者123 更新时间:2023-11-30 00:22:13 25 4
gpt4 key购买 nike

我的 RecyclerView 适配器有点忙。这就是我正在尝试做的事情,但到目前为止还没有成功。当用户单击弹出菜单时,如果匹配项不是当前 Activity 的匹配项,则会显示 2 个选项(编辑、删除)。当用户选择编辑时,我将其设为当前匹配项,但我也想将其移至回收站 View 的顶部。我试过这个:

Collections.swap(mMatches, 0, position);notifyItemMoved(位置, 0);

但没有成功。我能做些什么?代码如下:

public class MatchItemAdapter extends RecyclerView.Adapter<MatchItemAdapter.ViewHolder> {

private List<Match> mMatches;
private final Context mContext;

public MatchItemAdapter(Context context, List<Match> items) {
this.mContext = context;
this.mMatches = items;
}

@Override
public MatchItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater inflater = LayoutInflater.from(mContext);
View itemView = inflater.inflate(R.layout.list_item_match, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);

return viewHolder;
}

@Override
public void onBindViewHolder(final MatchItemAdapter.ViewHolder holder, final int position) {
final Match match = mMatches.get(position);
holder.matchIsActive = false;
final Match globalMatch = GlobalMatch.getMatch();

try {
holder.tvName.setText(match.getMatchName());
holder.tvDate.setText(match.getMatchDate());
holder.imageView.findViewById(R.id.imageView);
holder.hiddenMatchId.setText(match.getMatchId());
holder.container.findViewById(R.id.list_item_container);


Typeface bold = Typeface.create("san-serif", Typeface.BOLD);

String itemMatchId = holder.hiddenMatchId.getText().toString();

if (itemMatchId.equals(globalMatch.getMatchId())) {
holder.tvName.setTypeface(bold);
holder.container.setBackgroundColor(Color.parseColor("#a4a4a4"));
holder.tvName.setTextSize(24);
holder.matchIsActive = true;

}

holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {

final PopupMenu popup = new PopupMenu(view.getContext(), holder.imageView);
if (!holder.matchIsActive) {
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit_match:

GlobalMatch.changeMatch(match); // make this match the current match

// place a string representation of the current match in SharedPreferences
String jsonString = MatchHelper.jsonStringFromMatch(holder.imageView.getContext(), match);
SharedPreferences.Editor editor = holder.itemView.getContext().getSharedPreferences(
MY_GLOBAL_PREFS, holder.itemView.getContext().MODE_PRIVATE).edit();
editor.putString(CURRENT_MATCH, jsonString);
editor.apply();

// place this match at the top of the list
Collections.swap(mMatches, 0, position);
notifyItemMoved(position, 0);



Intent intent = new Intent(holder.itemView.getContext(), EditMatchActivity.class);
holder.itemView.getContext().startActivity(intent);
break;
case R.id.delete_match:

mMatches.remove(match);
notifyItemRemoved(position);

// delete file from internal storage
File dir = holder.itemView.getContext().getFilesDir();
File file = new File(dir, "match." + match.getMatchId() + ".json");
boolean deleted = file.delete();
Log.d("TAG", "The file was deleted");

break;
}
return false;
}
});
} else {

// If the current item is selected and you click on the side menu -> don't show delete
// delete isn't an option for active Matches
popup.getMenuInflater().inflate(R.menu.popup_menu_no_delete, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit_match:

Intent intent = new Intent(holder.itemView.getContext(), EditMatchActivity.class);
holder.itemView.getContext().startActivity(intent);
break;
}
return false;
}
});
}

//displaying the popup menus
popup.show();
}
});

holder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

holder.matchIsActive = true; // maybe this is redundant
String jsonString = MatchHelper.jsonStringFromMatch(holder.imageView.getContext(), match);

GlobalMatch.changeMatch(match);

SharedPreferences.Editor editor = holder.itemView.getContext().getSharedPreferences(
MY_GLOBAL_PREFS, holder.itemView.getContext().MODE_PRIVATE).edit();
editor.putString(CURRENT_MATCH, jsonString);
editor.apply();

Intent intent = new Intent(holder.itemView.getContext(), MainActivity.class);
holder.itemView.getContext().startActivity(intent);
}
});

} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public int getItemCount() {
return mMatches.size();
}


public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView tvName;
public TextView tvDate;
public ImageView imageView;
public TextView hiddenMatchId;
public View container;

boolean matchIsActive = false;


public ViewHolder(final View itemView) {
super(itemView);

tvName = (TextView) itemView.findViewById(R.id.match_name);
tvDate = (TextView) itemView.findViewById(R.id.match_date);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
hiddenMatchId = (TextView) itemView.findViewById(R.id.match_id_hidden);
container = (View) itemView.findViewById(R.id.list_item_container);

}

}
}

最佳答案

我用这个替换了代码,它起作用了:

Collections.swap(mMatches,holder.getAdapterPosition() ,0 );  
mRecyclerView.getAdapter().notifyItemMoved(holder.getAdapterPosition(), 0);

关于android - 试图将我选择的 listItem 移动到 recyclerview 的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46065648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com