Android片段生命周期在按下返回按钮时将更多项目添加到列表中

时间:2019-01-15 19:27:11

标签: android android-fragments

当我使用FragNav库时,我将一些项目放入List中,当我打开其他片段,并按下BackButton后,我还有其他一些项目进入该列表中,< / p>

例如,通常在打开其他片段并按电话上的后退按钮后,我将List中的一项放入RecyclerView中,然后按电话上的后退键,我将RecyclerView中的2项放入onSaveInstanceState中并使用onActivityCreatedpublic class HomeFragment extends BaseFragment implements IAnimationListener, View.OnClickListener, InstagramFeedsAdapter.OnClickListener { ... // constructor public static HomeFragment createInstance(int instance) { HomeFragment fragment = new HomeFragment(); Bundle bundle = new Bundle(); bundle.putInt(BaseFragment.ARGS_INSTANCE, instance); fragment.setArguments(bundle); return fragment; } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.instagram_post_container, container, false); activity = getActivity(); context = getActivity().getBaseContext(); mediaUrls = new ArrayList<>(); feedsSchema = new FeedsSchema(); feedsSchema.setId(1); mediaUrls.add("http://gizasystems.com/wp-content/uploads/2014/08/natural-world-hero3.jpg"); feedsSchema.setFeed_media_url(mediaUrls); feedsSchema.setFollowers_count(10); feedsSchema.setFollowings_count(20); feedsSchema.setPage_name("my test"); feedsSchema.setType(FeedsSchema.feed_types.IMAGE); items.add(feedsSchema); instagram_feeds = mView.findViewById(R.id.instagram_feeds); layoutManager = new LinearLayoutManager(context); instagram_feeds.setLayoutManager(layoutManager); instagram_feeds.setNestedScrollingEnabled(false); instagramFeedsAdapter = new InstagramFeedsAdapter(activity, context, this, items); return mView; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Bundle arguments = getArguments(); } @Override public void onAnimationEnd() { } @Override public void onClick(View view) { int clickedItem = view.getId(); } @Override public void onStart() { super.onStart(); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public void onStop() { super.onStop(); } @Override public void onItemClick(View view, String transitionName, int position) { } @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelableArrayList("someVarA", new ArrayList<>(items)); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null) items = savedInstanceState.getParcelableArrayList("someVarA"); } } 无法解决我的问题

我的片段:

public class InstagramFeedsAdapter extends RecyclerView.Adapter<InstagramFeedsAdapter.InstagramFeedsViewHolder>
        implements PreviewView.OnPreviewChangeListener {

    private OnClickListener listener;
    private List<FeedsSchema> feedsItems;
    private Context context;
    private boolean hoveredOnPopup = false;
    private ExoPlayerManager exoPlayerManager;
    private Activity activity;

    private enum feedType {
        IMAGE,
        IMAGESLIDER,
        VIDEO
    }

    public InstagramFeedsAdapter(Activity activity, Context context, OnClickListener listener, List<FeedsSchema> items) {
        this.listener = listener;
        this.feedsItems = items;
        this.context = context;
        this.activity = activity;
    }

    @Override
    public void onBindViewHolder(@NonNull final InstagramFeedsViewHolder holder, int position) {
        int feedType = feedsItems.get(position).getType().ordinal();
        implementingGeneralViewData(holder, position);
        if (feedType == 0) {
            if (feedsItems.get(position).getFeed_media_url().size() > 1) {
                implementingImageSliderView(holder, position);
            } else {
                implementingImageView(holder, position);
            }
        } else {
            implementingVideoPlayerView(holder, position);
        }
    }

    private void implementingVideoPlayerView(InstagramFeedsViewHolder holder, int position) {
    }

    @SuppressLint("CheckResult")
    private void implementingImageSliderView(final InstagramFeedsViewHolder holder, final int position) {

    }

    @SuppressLint("CheckResult")
    private void implementingImageView(final InstagramFeedsViewHolder holder, final int position) {

    }

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

    public class InstagramFeedsViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, PopupInflaterListener, PopupStateListener, PopupOnHoverListener {
        ...

        public InstagramFeedsViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onClick(View view) {

        }

        public void bind(int position) {
            ...
        }

}

和我的适配器:

c#

0 个答案:

没有答案