尝试将我选择的listItem移动到recyclerview的顶部

时间:2017-09-06 01:51:27

标签: android android-recyclerview

我的RecyclerView适配器变得有点忙。这就是我要做的事,但到目前为止还没有成功。当用户单击弹出菜单时,如果匹配不是当前活动的匹配,则会显示2个选项(编辑,删除)。当用户选择编辑时,我将其设为当前匹配,但我也想将其移动到recyclerview的顶部。我试过这个:

Collections.swap(mMatches,0,position); notifyItemMoved(position,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);

        }

    }
}

3 个答案:

答案 0 :(得分:0)

如果它不起作用,请在交换数据后尝试使用notifyDataSetChanged()作为最后的手段。

答案 1 :(得分:0)

我不确定导致RecyclerView未更新其项目的代码有什么问题。但是从你的代码如下所示

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

我发现,通过交换集合中的两个项目,您应该移动两个项目,而不仅仅是一个项目。 也就是说,例如,原始列表是[0,1,2,3,4],交换(列表,0,3)将获得[3,1,2,0,4]。因此,您需要:

notifyItemMoved(position, 0); 
notifyItemMoved(1, position);

答案 2 :(得分:0)

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

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