[英]How to make FragmentStatePagerAdapter from ViewPager fragment and ViewPagerAdapter
I have a ViewPager
fragment and a ViewPagerAdapter
and I want to make a FragmentStatePagerAdapter
. 我有一个
ViewPager
片段和一个ViewPagerAdapter
,我想制作一个FragmentStatePagerAdapter
。
So I searched everywhere but I can't find any good solutions. 因此,我到处搜索,但找不到任何好的解决方案。
Can someone help me to make a FragmentStatePagerAdapter
of my ViewPager
fragment and my ViewPagerAdapter
. 谁能帮我做一个
FragmentStatePagerAdapter
我的ViewPager
片段和我的ViewPagerAdapter
。
ViewPager fragment 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 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>
It would be nice if someone edit the code to a FragmentStatePAgerAdapter or would me explain how I must edit it. 如果有人将代码编辑到FragmentStatePAgerAdapter,或者让我解释如何编辑它,那将是很好的。
Thanks in advance. 提前致谢。
FragmentStatePagerAdapter
是PagerAdapter
的子类,因此您需要使ViewPagerAdapter
类直接从FragmentStatePagerAdapter
扩展,而不是从PagerAdapter
。
What you have done here is very complex. 您在这里所做的工作非常复杂。
For FragmentStatePagerAdapter
you should use FragmentStatePagerAdapter
instead of PagerAdapter
. 对于
FragmentStatePagerAdapter
,应使用FragmentStatePagerAdapter
而不是PagerAdapter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.