代码之家  ›  专栏  ›  技术社区  ›  user5510527

尝试将我选择的列表项移动到回收视图的顶部

  •  1
  • user5510527  · 技术社区  · 7 年前

    收藏。交换(mMatches,0,位置); 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);
    
            }
    
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   user5510527 user5510527    7 年前

    我用这个替换了代码,它成功了:

    Collections.swap(mMatches,holder.getAdapterPosition() ,0 );  
    mRecyclerView.getAdapter().notifyItemMoved(holder.getAdapterPosition(), 0);
    
        2
  •  0
  •   Nabin Bhandari    7 年前

    notifyDataSetChanged()

        3
  •  0
  •   civic.LiLister    7 年前

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

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

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