gpt4 book ai didi

android - 动态添加元素到 ViewPager

转载 作者:搜寻专家 更新时间:2023-11-01 08:08:47 26 4
gpt4 key购买 nike

我已经开始使用 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

}

}

}

主.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>

中间.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>

最佳答案

我在 ViewPager 上看不到任何获取当前 View 的方法,这很奇怪。但是您可以通过其他方式实现适配器并将 View 存储在任何集合或映射中。我不知道它是否有效,从我的头脑中编写代码。

例如

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);
}
}
});

关于android - 动态添加元素到 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11854192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com