我在ViewPager中有一个EditText
,一个ListView
和一个EditText
。
Button
将EditText
的文字添加到ListView
。
所以我的问题是,当用户想要添加文字时,文本将被添加但不会添加到ViewPager
的同一页面中,而是在下一页中。
我的代码
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>
答案 0 :(得分:1)
这里有一个范围问题。 ViewPager的工作方式是加载和缓存从适配器获取的视图。它显示了它已缓存的其中一个视图。但是添加内容的列表属于适配器,而不是显示的视图。
因此,当ViewPager要求下一个视图时(它可以顺利滚动而无需在事件时加载下一个视图),它将替换适配器中的List。但是侦听器会写入最后创建的列表,该列表会更新下一个视图列表。
我建议使用FragmentPagerAdapter(如果你有很多页面,则使用FragmentStatePagerAdapter)。这样,列表和它显示的数据都包含在同一个片段中。
编辑: 你可以试试这样的东西
使用FragmentPagerAdapter
private class ViewPagerAdapter extends FragmentPagerAdapter {
private HashMap<Integer, Fragment> currentViews;
private String[] titles;
private int[] resIds;
public ViewPagerAdapter(FragmentManager fm, String[] titles, int[] resIds) {
super(fm);
currentViews = new HashMap<>();
assert titles.length = resIds.length : Log.wtf("ViewPagerAdapter", "title and image arrays must be the same length."); //there's probably a better way to do this.
this.titles = titles;
this.redIds = resIds;
}
...
@Override
public Fragment getItem(int position) {
if (currentViews.containsKey(position))
return currentViews.get(position);
else {
SwipeViewFragment fragment = SwipeViewFragment.newInstance(YourActivity.this, title[position], reIds[position]); //Context for the ArrayAdapter threw me for a second. this assumes you're using an activity. If this is in Fragment use "YourFragment.getContext()"
currentView.put(position, fragment);
return fragment;
}
}
...
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.onDestroyItem(container, position, object);
if (currentViews.containsKey(position))
currentViews.remove(position);
}
}
碎片
private static class SwipeViewFragment extends android.support.v4.app.Fragment {
ArrayList<String> list;
EditText textInput;
Context mContext
ArrayAdapter<String> adapter;
...
public SwipeViewFragment() { //required to be blank
}
...
@Override
public void onCreate(Bundle savedInstanceState) {
list = new ArrayList<>();
}
...
@Override
public View onCreateView(Inflater inflater, ...) {
View root = inflater.inflate(R.layout.swipeview, container, false);
Bundle args = getArguments() //this is where you pass data to the fragment thats present at instance time
String title = args.getString("title");
int resId = args.getInt("image");
TextView titleTextView = (TextView) root.findViewById(R.id.swipeviewtitle);
titleTextView.setText(title);
textInput = (EditText) root.findViewById(R.id....)
...
Button addButton = (Button) root.findViewById(R.id.imagebuttonSwipeView);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = textInput.getText().toString;
list.add(text);
adapter.notifyDatasetChanged();
});
adapter = new ArrayAdapter(mContext, R.layout.list_item, list);
return root
}
public static SwipeViewFragment new instance(Context context, String title, int resId){
SwipeViewFragment fragment = new SwipeViewFragment();
mContext = context;
Bundle args = new Bundle();
args.putInt("image", resId); //do better than this, use String constants, I'm being lazy
args.putString("title", title);
fragment.setArguments(args)
return fragment;
}
...
//included methods to manipulate data in the Fragment
public void doSomethig(args...)
}
然后,Activity可以执行类似
的操作public doSomething(args...) {
SwipeViewFragment fragment = pagerAdapter.getItem(pagerAdapter.currentItem());
fragment.doSomething(args...);
}
底线是ViewPager显示的每个视图都需要自己的List。基本的View类没有这个地方。因此,您需要从布局创建自定义视图。这几乎就是一个片段。
编辑2:我可能在这个错误中得到了一些东西。但它应该给你这个想法。
答案 1 :(得分:0)
我记得instantiateItem被调用两次,位置0和1(预先缓存的一部分)。
所以在启动时你已经有2个listView,listItems,editText,...
的实例也许尝试根据职位检查您的参考资料