我已经开始使用ViewPager,无法通过按主布局上的按钮来了解如何添加新元素,如TextView。我尝试了不同的方法,但没有一个成功。
任何想法对我都有好处......
这是我的代码:
public class SimpleViewPagerActivity extends Activity {
private ViewPager myPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
myPager = (ViewPager) findViewById(R.id.mypager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
Button btn_add = (Button) findViewById(R.id.button1);
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private class MyPagerAdapter extends PagerAdapter {
@Override
public View instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.left;
break;
case 1:
resId = R.layout.middle;
break;
case 2:
resId = R.layout.right;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lt_lin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<android.support.v4.view.ViewPager
android:id="@+id/mypager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
middle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text" />
</LinearLayout>
答案 0 :(得分:4)
我没有看到任何获取ViewPager当前视图的方法很奇怪..但是你可以用其他方式实现你的适配器并在任何集合或地图中存储视图。我不知道它是否会起作用,从头脑中编写代码。
例如
private class MyPagerAdapter extends PagerAdapter {
private Map<Integer, View> views = new HashMap<Integer, View>();
...
@Override
public View instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.left;
break;
case 1:
resId = R.layout.middle;
break;
case 2:
resId = R.layout.right;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
views.put(position, view);
return view;
}
public View getView(int pos){
return views.get(pos);
}
}
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View view = adapter.getView(pager.getCurrentItem());
if (view instance ViewGroup){
view.add(your_new_view);
}
}
});