在FragmentActivity中重新加载片段的问题

时间:2014-02-26 08:06:00

标签: android android-fragments

当我开发Android应用程序时,我使用了FragmentActivity。它里面有一个FragamentPagerAdapter。

此活动中的一个片段是在onCreateView()中动态创建的。但是,当某个按钮点击该片段时,我想重新加载该片段以反映这些更改。有什么方法可以重新加载那些可以让android再次调用onCreateView()的片段吗?

显示动态片段中的代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_voucher,
                container, false);
        btnAddVoucher = (Button) rootView.findViewById(R.id.addVoucher);
        btnAddVoucher.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "ONE_D_MODE");
                startActivityForResult(intent, 0);
            }
        });

        // get the voucher list from the Payment Activity
        voucherList = ((PaymentActivity)getActivity()).getVouchers();

        LinearLayout layout = (LinearLayout)rootView.findViewById(R.id.dynamic);
        for (Voucher t: voucherList){
            Button b = new Button(getActivity());
            final String voucherID = t.getBarcodeID();
            final String voucherType = t.getBarcodeType();
            final String voucherName = t.getName();
            b.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){
                    Intent intent = new Intent(getActivity(),VoucherActivity.class);
                    intent.putExtra(EXTRA_VOUCHER_ID,voucherID);
                    intent.putExtra(EXTRA_VOUCHER_TYPE,voucherType);
                    intent.putExtra(EXTRA_VOUCHER_NAME,voucherName);
                    startActivity(intent);
                }
            });
            b.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    DatabaseHelper dbHelper = new DatabaseHelper (getActivity());
                    dbHelper.removeVoucher(voucherName);
                    ((PaymentActivity)getActivity()).refresh();
                    return true;
                }
            });
             b.setText(t.getName());
             layout.addView(b);
        }

        return rootView;
    }

1 个答案:

答案 0 :(得分:0)

您应该将要重用的代码(在onCreateView中)移动到其他方法,然后,当您单击按钮时调用此方法。

private LinearLayout layout;
private Voucher voucherList;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_voucher,
            container, false);
    btnAddVoucher = (Button) rootView.findViewById(R.id.addVoucher);
    btnAddVoucher.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "ONE_D_MODE");
            startActivityForResult(intent, 0);
        }
    });

    layout = (LinearLayout)rootView.findViewById(R.id.dynamic);

    // get the voucher list from the Payment Activity
    voucherList = ((PaymentActivity)getActivity()).getVouchers();

    initFragment();

    return rootView;
}

private void initFragment(){

    for (Voucher t: voucherList){
        Button b = new Button(getActivity());
        final String voucherID = t.getBarcodeID();
        final String voucherType = t.getBarcodeType();
        final String voucherName = t.getName();
        b.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(getActivity(),VoucherActivity.class);
                intent.putExtra(EXTRA_VOUCHER_ID,voucherID);
                intent.putExtra(EXTRA_VOUCHER_TYPE,voucherType);
                intent.putExtra(EXTRA_VOUCHER_NAME,voucherName);
                startActivity(intent);
            }
        });
        b.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                DatabaseHelper dbHelper = new DatabaseHelper (getActivity());
                dbHelper.removeVoucher(voucherName);

                refreshFragment();  //Here is the call to refresh function

                return true;
            }
        });
         b.setText(t.getName());
         layout.addView(b);
    }

}

private void refreshFragment(){
    layout.removeAllViews();
    initFragment();
}