如何动态添加几个片段

时间:2014-10-30 15:50:54

标签: java android xml android-fragments

我创建了一个带有3个静态片段和视图寻呼适配器的小应用程序。但我想要的是创建只有一种片段和一个按钮“添加”,添加其他片段将由视图寻呼机调整..这是我做了什么

FragmentTab1.java; FragmentTab2; FragmentTab3具有相同的代码

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTab2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab2.xml
        View view = inflater.inflate(R.layout.fragmenttab2, container, false);
        return view;
    }

}

这是我的MainActivity.java

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity {

    //Button adfg = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);        
        // Locate the viewpager in activity_main.xml
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        // Set the ViewPagerAdapter into ViewPager
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    }   
}

ViewPagerAdapter.java

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class ViewPagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 3;
    // Tab Titles
    private String tabtitles[] = new String[] { "Tab1", "Tab2", "Tab3" };

    Context context;

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

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {

            // Open FragmentTab1.java
        case 0:
            FragmentTab1 fragmenttab1 = new FragmentTab1();
            return fragmenttab1;


            // Open FragmentTab2.java
        case 1:
            FragmentTab2 fragmenttab2 = new FragmentTab2();
            return fragmenttab2;

            // Open FragmentTab3.java
        case 2:
            FragmentTab3 fragmenttab3 = new FragmentTab3();
            return fragmenttab3;
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabtitles[position];
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:textColor="#000000" />

</android.support.v4.view.ViewPager>

fragmenttab1.xml; fragmenttab2.xml; fragmenttab3.xml具有相同的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1" />

    <Button
        android:id="@+id/bajou"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ajouter fragment" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

你需要创建一个方法来在FragmentPagerAdapter的子类上添加片段,或多或少像这样:

//use those lists
private List<Fragment> listFragment = new ArrayList<Fragment>();
private List<String> listFragmentTitle = new ArrayList<String>();
private List<Integer> listFragmentIcon = new ArrayList<Integer>();

public void addTab(Fragment fragment,String title, int icon) {
    listFragment.add(fragment);
    listFragmentTitle.add(title);
    listFragmentIcon.add(icon);
}

@Override
public Fragment getItem(int position) {
    // TODO Auto-generated method stub
    return listFragment.get(position);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listFragment.size();
}

然后你只需要在Activity的子类上使用它,例如:

//create a private method to set a new - dynamic fragment in the ACTIVITY
private Fragment setFragment(int pos)
{
    MyFragment myFragment = new MyFragment();
    Bundle data = new Bundle();
    data.putString("test", "test");
    myFragment.setArguments(data);
    return myFragment;
}

最后,添加片段(例如,在onClickListener内):

//INSIDE an onClickListener
pagerAdapter.addTab(setFragment(position), getItem(position).getName(), R.drawable.ic_launcher);

答案 1 :(得分:0)

这是我的MainActivity.java类@BlazeTama

package com.light.frag;

import com.light.frag.R;
import com.light.frag.ViewPagerAdapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends FragmentActivity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         // Locate the viewpager in activity_main.xml
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        // Set the ViewPagerAdapter into ViewPager
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); 
        Button ajou = (Button) findViewById(R.id.bajou);
        ajou.setOnClickListener(this);
    }
    private Fragment setFragment(int pos)
    {
        MyFragment myFragment = new MyFragment();
        Bundle data = new Bundle();
        data.putString("test", "test");
        myFragment.setArguments(data);
        return myFragment;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bajou:
            ViewPagerAdapter.addTab(setFragment(position), getItem(position).getName(), R.drawable.ic_launcher);
            break;
        }

    }    
}