即使视图寻呼机显示另一个片段,Recycler视图适配器也会获取旧数据(适配器)

时间:2017-05-12 13:20:52

标签: android android-recyclerview android-viewpager recycler-adapter android-contextmenu

我有一个带有3个选项卡的ViewPager,每个选项卡都加载一个片段,相同片段类的新实例,但带有一个额外的参数。

下面你可以找到片段类,我在该片段中有一个RecyclerView,当用户长按CyclelerView中的一个项目时,我试图打开一个上下文菜单。

我能够显示上下文菜单并获取单个片段实例所选项目的位置,当我滑动到下一个片段时,我得到相同的索引位置,当我进一步调试时,我发现我我在前一个片段上获得了与RecyclerView相同的适配器。当我记录RecyclerView的适配器大小时,可以看到这一点。

当我在ViewPager中刷到该片段的完全不同的实例时,任何人都可以帮助我理解为什么我得到以前的适配器数据,即使我能在片段中看到正确的数据并获得正确的值onBindViewHolder方法。

您可以在下面找到片段类的简化代码

public class HistoryFragment extends Fragment{
    private static final String LOG_TAG = HistoryFragment.class.getSimpleName();

    private RecyclerView mRecyclerView;
    private HistoryAdapter mHistoryAdapter;
    private List<Group> mGroups;

    private String mGroupType;

    /**
     * tried using a static int value to track which context menu was opened and on which fragment
     * refer post https://caughtinthemobileweb.wordpress.com/2013/10/22/android-viewpager-holds-reference-to-old-fragments/
     * BUG LOGGED AT https://code.google.com/p/android/issues/detail?id=19211
     * does not work :(
     */
    /*private static int selectedContextMenu;*/ // returns valid index as int is static, but adapter still has old data

    /**
     * The fragment argument for this fragment.
     */
    private static final String ARG_GROUP_TYPE = "group_type";

    public HistoryFragment() { }

    /**
     * Returns a new instance of this fragment
     */
    public static HistoryFragment newInstance(String groupType) {
        HistoryFragment fragment = new HistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_GROUP_TYPE, groupType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_history, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerViewHistoryItem);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
        mGroups = new ArrayList<>();
        mHistoryAdapter = new HistoryAdapter(getActivity(), mGroups);
        mRecyclerView.setAdapter(mHistoryAdapter);


        Bundle bundle = getArguments();
        mGroupType = bundle.getString(ARG_GROUP_TYPE);
        if(mGroupType != null && !mGroupType.equals("")){
            loadDataFromDb();
        } else {
            if(IS_DEBUG_ON) Log.d(LOG_TAG, "invalid fragment arguments");
            getActivity().finish();
        }
        return rootView;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){
        // int id = item.getItemId();
        Log.d(LOG_TAG, "position2: " + mHistoryAdapter.getSelectedItemPosition() + 
                " " + mGroups.get(mHistoryAdapter.getSelectedItemPosition()).getName()); // returns old data
        Log.d(LOG_TAG, "size2: " + mHistoryAdapter.getItemCount()); // returns old data
        // Log.d(LOG_TAG, "position3: " + selectedContextMenu); // returns correct index, but cant use as adapter still has old data
        Log.d(LOG_TAG, "position4: " + ((HistoryAdapter) mRecyclerView.getAdapter()).getSelectedItemPosition()); // returns old data
        return true;
    }

    private void loadDataFromDb(){
        new LoadGroups().execute();
    }

    class HistoryAdapter extends RecyclerView.Adapter<HistoryFragment.HistoryAdapter.ViewHolder> {
        private Context mContext;
        private List<Group> mList;
        private int mSelectedItemPosition;

        HistoryAdapter(Context mContext, List<Group> mList) {
            this.mContext = mContext;
            this.mList = mList;
        }

        class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
            View mView;
            TextView mTextViewName;
            TextView mTextViewDescription;
            ImageView mImageViewProfile;
            ImageView mImageCheck;

            public ViewHolder(View itemView) {
                super(itemView);
                this.mView = itemView;
                this.mTextViewName = (TextView) itemView.findViewById(R.id.textViewGroupName);
                this.mTextViewDescription = (TextView) itemView.findViewById(R.id.textViewGroupDescription);
                this.mImageViewProfile = (ImageView) itemView.findViewById(R.id.groupImage);
                this.mImageCheck = (ImageView) itemView.findViewById(R.id.imageCheck);
                this.mView.setOnCreateContextMenuListener(this); // registering listener
            }

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                if(mGroupType.equals(GroupContract.GroupEntry.GROUP_TYPE_HISTORY_GROUP)){
                    menu.add(Menu.NONE, R.id.action_resubscribed_group, Menu.NONE, R.string.action_resubscribed_group);
                } else if(mGroupType.equals(GroupContract.GroupEntry.GROUP_TYPE_SUBSCRIBED_GROUP)){
                    if(!mList.get(getAdapterPosition()).isDefault()){
                        menu.add(Menu.NONE, R.id.action_unsubscribe_group, Menu.NONE, R.string.action_unsubscribe_group);
                    }
                    menu.add(Menu.NONE, R.id.action_mute, Menu.NONE, R.string.action_mute);
                    menu.add(Menu.NONE, R.id.action_view_group_info, Menu.NONE, R.string.action_view_group_info);

                }
            }
        }

        @Override
        public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_history, parent, false);
            return new HistoryAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final HistoryAdapter.ViewHolder holder, final int position) {
            if(mGroupType.equals(GroupContract.GroupEntry.GROUP_TYPE_SUBSCRIBED_GROUP)){
                holder.mImageCheck.setVisibility(View.VISIBLE);
            } else {
                holder.mImageCheck.setVisibility(View.GONE);
            }
            holder.mTextViewName.setText(mList.get(position).getName());
            holder.mTextViewDescription.setText(mList.get(position).getDescription());

            holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    // mSelectedItemPosition = position; // tried this, returns correct 
                    // selectedContextMenu = position; // also tried this, returns correct
                    mSelectedItemPosition = holder.getAdapterPosition(); // this also returns correct 
                    Log.d(LOG_TAG, "position1 - onBind: " + mSelectedItemPosition + " "+ mList.get(mSelectedItemPosition).getName()); // returns correct data
                    return false;
                }
            });

        }

        @Override
        public void onViewRecycled(ViewHolder holder) {
            holder.mView.setOnLongClickListener(null); // clearing listener to avoid collision between list items
            super.onViewRecycled(holder);
        }

        @Override
        public int getItemViewType(int position) {
            return 0;
        }

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

        int getSelectedItemPosition() {
            Log.d(LOG_TAG, "getter: " + mSelectedItemPosition); // this returns wrong data, shows that fragment is referring old adapter data
            return mSelectedItemPosition;
        }
    }

    // get data from db
    class LoadGroups extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... voids) {
            mGroups = GroupContract.getGroupEntriesFromDB(getActivity(), mGroupType);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            mHistoryAdapter = new HistoryAdapter(getActivity(), mGroups);
            mRecyclerView.setAdapter(mHistoryAdapter);
        }
    }
}

您可以在下面找到ViewPager Adapter类

public class HomeTabsPagerAdapter extends FragmentPagerAdapter {
    private Context mContext;

    HomeTabsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return HistoryFragment.newInstance("TYPE_1");
            case 1:
                return HistoryFragment.newInstance("TYPE_2");
            case 2:
                return HistoryFragment.newInstance("TYPE_3");
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return mContext.getString(R.string.text_type_1);
            case 1:
                return mContext.getString(R.string.text_type_2);
            case 2:
                return mContext.getString(R.string.text_type_3);
        }
        return null;
    }

    // Doesn't work even when I use this or not
    /*@Override
    public int getItemPosition(Object object) {
        // try http://stackoverflow.com/a/8024557
        // return super.getItemPosition(object);
        return POSITION_NONE;
    }*/
}

2 个答案:

答案 0 :(得分:0)

我怀疑,使用RecyclerView.LayoutManager可能是一个问题,因为你有一个静态类,前两个创建的片段将具有相同的内容(第二个)。对于下一个片段也是如此。此外,getItemPosition(Object object) - &gt; return POSITION_NONE仅可用于FragmentStatePagerAdapter。 FragmentAdapter一旦创建,就永远不会更改碎片。  所以,我建议而不是:

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false); mRecyclerView.setLayoutManager(的layoutManager);

使用

mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

//或设置GridLayoutManager

如果您计划更新片段的内容,请使用FragmentStatePagerAdapter。

答案 1 :(得分:0)

我遇到了类似的问题,并通过将其放在我的片段上来修复它:

@Override
public void onDetach() {
    super.onDetach();
    recyclerView.getRecycledViewPool().clear();
}

在您的情况下,您可以在更改选项卡时调用 getRecycledViewPool。