如何交易替换标签中的片段

时间:2018-07-18 10:30:44

标签: android android-fragments android-viewpager

我有一个包含3个片段和一个FAB的Android标签。我想在单击FAB时替换为新的片段,但是当我单击按钮时,它将替换为空白片段。当我单击选项卡时,它不显示其片段而是一个空片段(有时显示正确,但我不知道何时)

这是我的MainActivity:

 foreach (DriveInfo drive in DriveInfo.GetDrives())
 {
     if (drive.DriveType == DriveType.Removable)
     {
        Console.WriteLine(drive.Name);
     }
 }

这是我的适配器:

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;
    private TabLayout tabLayout;
    private FloatingActionButton fabAdd;
    FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewPager = (ViewPager) findViewById(R.id.container);
        PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager());

        pageAdapter.addFragment(new Wallet(), "Wallet");
        pageAdapter.addFragment(new History(),"History");
        pageAdapter.addFragment(new Setting(),"Setting");

        mViewPager.setAdapter(pageAdapter);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        fabAdd = (FloatingActionButton) findViewById(R.id.fabAdd);
        manager = getSupportFragmentManager();

        fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddAccount addAccount = new AddAccount();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.container,addAccount);
                transaction.commit();
            }
        });
    }
}

这是我的片段之一(另一个是相同的):

public class AccountAdapter extends BaseAdapter {

    private Context context;
    private int layout;
    private List<Account> accountList;

    public AccountAdapter(Context context, int layout, List<Account> accountList) {
        this.context = context;
        this.layout = layout;
        this.accountList = accountList;
    }



    @Override
    public int getCount() {
        return accountList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(layout,null);

        TextView accountName = (TextView) view.findViewById(R.id.tvAccount);
        TextView accountAddress = (TextView) view.findViewById(R.id.tvAddress);

        Account account = accountList.get(i);
        accountName.setText(account.getAccountName());
        accountAddress.setText(account.getAccountAddress());

        return view;
    }
}

我是Android的新手,英语不是很好,所以如果我语法错误,请原谅我。

编辑:有什么方法可以向viewpager显示新片段吗?

1 个答案:

答案 0 :(得分:0)

在onClick内使用此代码

Fragment fragment = new AddAccount();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragment);
ft.commit();

并将其用于您的适配器

class AccountAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}