FragmentStatePagerAdapter

FragmentStatePagerAdapter

本文介绍了如何从ViewPager片段ViewPagerAdapter使FragmentStatePagerAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ViewPager fragment and a ViewPagerAdapter and I want to make a FragmentStatePagerAdapter.

所以,我到处找,但我找不到什么好的解决办法。

So I searched everywhere but I can't find any good solutions.

有人可以帮助我做一个 FragmentStatePagerAdapter 我的 ViewPager 片段和我的 ViewPagerAdapter

Can someone help me to make a FragmentStatePagerAdapter of my ViewPagerfragment and my ViewPagerAdapter.

ViewPager片段

ViewPager fragment

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

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

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>

这将是很好,如果有人编辑code到FragmentStatePAgerAdapter或将我解释,我必须对其进行编辑。

It would be nice if someone edit the code to a FragmentStatePAgerAdapter or would me explain how I must edit it.

在此先感谢。

推荐答案

FragmentStatePagerAdapter 是子类 PagerAdapter 的,所以你需要让你的 ViewPagerAdapter 类直接从 FragmentStatePagerAdapter 延伸,而不是从 PagerAdapter

FragmentStatePagerAdapter is a child class of PagerAdapter, so you need to make your ViewPagerAdapter class extend directly from FragmentStatePagerAdapter, not from PagerAdapter.

这篇关于如何从ViewPager片段ViewPagerAdapter使FragmentStatePagerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 20:01