如何重新使用一个片段类将不同的数据加载到相同的活动中

时间:2017-11-02 22:13:13

标签: java android android-fragments

我有一个标签布局,可以加载不同的数据。所有片段只有一个相同的UI。但对于每个数据,我有不同的片段类文件。

我不想为同一个UI创建更多5个片段类。我想重用相同的一个片段类代码并在其中显示不同的数据。

我想传递不同的String id = String.valueOf(1);为每个片段获取不同的数据。

这是我的代码

一个片段类

public class OneFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "OneFragment";

    DisplayData displayData = new DisplayData();
    DetailsData reqDetailnew;

    OneFragment context;
    TextView title, subject;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab, container, false);

        context = OneFragment.this;

        //text
        title = (TextView) view.findViewById(R.id.title);
        subject = (TextView) view.findViewById(R.id.subject);





            return view;
    }
}
}

主要活动类

public class MainActivity extends AppCompatActivity {

   private PageAdapter PageAdapter;

    private ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subject_information);
        Log.d(TAG, "onCreate: Starting.");

        PageAdapter = new PageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);


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


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:

                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    private void setupViewPager(ViewPager viewPager) {
        PageAdapter adapter = new PageAdapter(getSupportFragmentManager());

        adapter.addFragment(new Tab1Fragment(), "1");
        adapter.addFragment(new Tab2Fragment(), "2");
        adapter.addFragment(new Tab3Fragment(), "3");
        adapter.addFragment(new Tab4Fragment(), "4");
        adapter.addFragment(new Tab5Fragment(), "5");

        viewPager.setAdapter(adapter);


    }

}

寻呼机适配器

public class PageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

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

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

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

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

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

MainAcitivity xml

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



    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

       >


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="fill"
            app:tabTextColor="#ffffff"
            />


    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</LinearLayout>

Tab xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title:"
           />

           <TextView
            android:id="@+id/subject"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Subject:"
           />
 </LinearLayout>

2 个答案:

答案 0 :(得分:1)

在页面适配器中返回片段时传递一个包:

@Override
public Fragment getItem(int position) {
    Bundle bundle = new Bundle();
    bundle.putInt("position", position);

    YourFragmentClass frag = new YourFragmentClass();
    frag.setArguments(bundle);
    return frag;
}

然后在YourFragmentClass的onCreateView内部使用int position,它可以帮助您使用switchif语句决定要充气的内容:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

int position = getArguments().getInt("position",0);

...//Your logic here depending on the position you passed!
}

你的位置使用条件陈述根据你的位置来膨胀某些东西。

答案 1 :(得分:1)

在PageAdapter中:

  @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return newInstance("1");
            case 1:
                return newInstance("2");
            case 2:
                return newInstance("3");
            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "1";
            case 1:
                return "2";
            case 2:
                return "3";
            default:
                return "";
        }
    }

  public OneFragment newInstance(String param1) {
    OneFragment fragment = new OneFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    fragment.setArguments(args);
    return fragment;
}

并在OneFragment中:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String string = getArguments().getString(ARG_PARAM1);
    // do stuff
     }