2

我似乎找不到一种方法来为我的页面查看器支持的应用程序动态创建的片段提供不同的文本。我已经到了我陷入困境的地步。对于我的应用程序,我希望有 400-500 个动态创建的片段,您可以在其中水平滑动它们,并且片段的每个内容都相同(重复相同的片段),唯一不同的是它们上的文本。这就是我陷入编码的地方:

package com.example.testarearg;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity   {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;


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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
        }    

    });
}




/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }


}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */ private String mText; // display this text in your fragment

     public static Fragment getInstance(String text) {
       Fragment f = new Fragment();
       Bundle args = new Bundle();
       args.putString("text", text);
       f.setArguments(args);
       return f;
     }

     public void onCreate(Bundle state) {
       super.onCreate(state);
       setmText(getArguments().getString("text"));
       // rest of your code
     }

    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }

    public String getmText() {
        return mText;
    }

    public void setmText(String mText) {
        this.mText = mText;
    }
}

}
4

1 回答 1

2

您需要创建自己的自定义片段:

import android.support.v4.app.Fragment;

public class YourFragment extends Fragment {

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

        TextView tv = (TextView) v.findViewById(R.id.yourtextview);
        tv.setText(getArguments().getString("text"));

        return v;
    }

    public static YourFragment newInstance(String text) {

        YourFragment f = new YourFragment();
        Bundle b = new Bundle();
        b.putString("text", text);

        f.setArguments(b);

        return f;
    }
}

然后修改你的适配器。基本思想是适配器包含您要显示的字符串,方法the getItem(...) 将选择文本。

public class MyPagerAdapter extends FragmentPagerAdapter {

        private String [] strings;

        public MyPagerAdapter(FragmentManager fm, String [] stringstodisplay) {
            super(fm);
            this.strings = stringstodisplay;
        }

        @Override
        public Fragment getItem(int pos) {
           return YourFragment.newInstance(strings[pos]);
        }

        @Override
        public int getCount() {
            return 500; // or strings.length to be save
        }       
    }

从您的活动中,您将包含不同字符串的字符串数组移交给适配器。

public class MainActivity extends FragmentActivity {

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

        // get the content that your fragments will display
        String [] strings = new String[500];

        for(int i = 0; i < 500; i++) {
            strings[i] = "This is Fragment " + i;
        }

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), strings));
    }
}
于 2013-09-08T12:42:07.147 回答