如何从ViewPager片段和ViewPagerAdapter制作FragmentStatePagerAdapter

时间:2015-01-28 17:51:14

标签: android listview android-fragments android-viewpager fragmentstatepageradapter

我有一个ViewPager片段和一个ViewPagerAdapter,我想制作一个FragmentStatePagerAdapter

所以我到处搜索,但我找不到任何好的解决方案。

有人可以帮我制作FragmentStatePagerAdapter我的ViewPager片段和ViewPagerAdapter


ViewPager片段

public class ViewPage extends Fragment {


    // Declare Variables
    ViewPager viewPager;
    ViewPagerAdapter adapter;
    Uebung.SessionItemAdapter adapter2;
    String[] Titel;
    ImageButton btnswipeview;

    List<String> data = new ArrayList<String>();



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from viewpager_main.xml
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View layout = inflater.inflate(R.layout.viewpage, container, false);

        ArrayList<String> strtext=getArguments().getStringArrayList("key");
        String frnames[]=strtext.toArray(new String[strtext.size()]);
        Titel = frnames;





        // Locate the ViewPager in viewpager_main.xml
        viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
        // Pass results to ViewPagerAdapter Class
        adapter = new ViewPagerAdapter(getActivity(), Titel);
        // Binds the Adapter to the ViewPager
        viewPager.setAdapter(adapter);


        return layout;
    }

ViewPagerAdapter

public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    String[] Titel;
    ArrayList<String> listItems;

    ArrayAdapter<String> adapter;

    List<String> data = new ArrayList<String>();

   // int[] images;
    LayoutInflater inflater;

    private static final Map<String, Integer> TITLE_IMAGES =
            new HashMap<String, Integer>();
    static {
        TITLE_IMAGES.put("Arme", R.drawable.ic_launcher);
        TITLE_IMAGES.put("Bauch", R.drawable.ic_launcher2);
    }

    public ViewPagerAdapter(Context context, String[] Titel) {
        this.context = context;

        this. Titel = Titel;
       // this.images = images;
    }


    @Override
    public int getCount() {
        return Titel.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // Declare Variables
        TextView txtTitel;

        ImageView image;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.swipeview, container,
                false);

        // Locate the TextViews in viewpager_item.xml
        txtTitel = (TextView) itemView.findViewById(R.id.swipeviewtitle);


        // Capture position and set to the TextViews
        txtTitel.setText(Titel[position]);


        // Locate the ImageView in viewpager_item.xml
        image = (ImageView) itemView.findViewById(R.id.swipeViewimage);
        // Capture position and set to the ImageView

        String title = Titel[position];
        Integer imageRes = TITLE_IMAGES.get(title);
        if (imageRes == null) { // not found in map
            image.setImageResource(0);
        } else {
            image.setImageResource(imageRes);
        }

        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);

        final ListView listView = (ListView) itemView.findViewById(R.id.listViewswipeview);
        final EditText editText = (EditText) itemView.findViewById(R.id.editTextswipeView);

        Button btnswipeview = (Button) itemView.findViewById(R.id.imagebuttonswipeview);

        listItems = new ArrayList<String>();

        listItems.add("First Item - added on Activity Create");

        adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listItems);

        listView.setAdapter(adapter);

        btnswipeview.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View View) {
                Toast toast = Toast.makeText(context,
                        "You clicked the button",
                        Toast.LENGTH_SHORT);
                toast.show();

                listItems.add(editText.getText().toString());
                adapter.notifyDataSetChanged();
            }
        });

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((LinearLayout) object);

    }

}

swipeview.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Titel"
        android:textSize="15pt"
        android:id="@+id/swipeviewtitle"
        android:layout_gravity="center_horizontal" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipeViewimage"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher"/>
</LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/editTextswipeView"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imagebuttonswipeview"
            android:src="@drawable/ic_menu_add"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/editText2"
            android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <ListView
            android:layout_margin="5dp"
            android:id="@+id/listViewswipeview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:clickable="true"
            android:drawSelectorOnTop="true"
            android:focusable="true"
            android:choiceMode="singleChoice"/>

        </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinnerswipeview"

            android:entries="@array/day"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1" />
        </LinearLayout>



</LinearLayout>

如果有人将代码编辑到FragmentStatePAgerAdapter或者我会解释我必须如何编辑它,那将会很好。

提前致谢。

2 个答案:

答案 0 :(得分:0)

FragmentStatePagerAdapterPagerAdapter的子类,因此您需要直接从ViewPagerAdapter延伸FragmentStatePagerAdapter课程,而不是PagerAdapter

答案 1 :(得分:0)

你在这里所做的事情非常复杂。

对于FragmentStatePagerAdapter,您应该使用FragmentStatePagerAdapter代替PagerAdapter