如何在Fragment中单击Button时删除FragmentPagerAdapter项

时间:2015-10-02 07:20:20

标签: android android-fragments

我想删除&刷新ViewPager中的FragmentPagerAdapter项目

首先,我制作mTicketViewPager(ViewPager)和mTicketAdapter(FragmentPagerAdapter)。

在AppCompatActivity中;

mTicketViewPager = mPagerContainer.getViewPager();
mTicketAdapter = new TicketViewPagerAdapter(getSupportFragmentManager(), mOrderList, TicketActivity.this);
mTicketViewPager.setAdapter(mTicketAdapter);
mTicketViewPager.setOffscreenPageLimit(mTicketAdapter.getCount());
mTicketViewPager.setClipChildren(false);


其次,我要删除destroyItem方法我的Fragment。

FragmentPagerAdapter中的

;

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    if (position >= getCount()) {
        FragmentManager manager = ((Fragment) object).getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.remove((Fragment) object);
        transaction.commit();
    }
}

我制作了FragmentPagerAdapter的片段getItem方法


最后,我的片段代码;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.row_ticket_viewpager, container, false);

    mRemoveImageButton = (ImageButton) view.findViewById(R.id.row_ticket_btn_remove);


    mRemoveImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog dialog = new AlertDialog.Builder(getActivity()).create();
            dialog.setCancelable(true);
            dialog.setTitle("cancel?");
            dialog.setMessage("test.");
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
// try this one?
                    dialog.dismiss();
                }
            });
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "no", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog.show();

            Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
            if (positiveButton != null)
                positiveButton.setTextColor(Color.parseColor("#73C9FA"));

            Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
            if (negativeButton != null)
                negativeButton.setTextColor(Color.parseColor("#9B9B9B"));
        }
    });


    return view;
}

如何在BUTTON_POSITIVE上点击事件区域调用destroyItem方法?


感谢您的帮助。!

0 个答案:

没有答案