gpt4 book ai didi

android - 从 ViewPager 获取当前 View

转载 作者:太空狗 更新时间:2023-10-29 15:45:19 29 4
gpt4 key购买 nike

我正在尝试使用 Viewpager.findViewWithTag 从 ViewPager 获取当前页面 View 。当尝试从 ViewPager 获取当前 View 时,它返回 Null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_main);
Bundle bundle = getIntent().getExtras();
chapter = bundle.getInt("chapter");


ViewPagerAdapter adapter = new ViewPagerAdapter(this, chapter);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

View myView = pager.findViewWithTag(pager.getCurrentItem());

...这是我的 ViewPageAdapter

 @Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater;
if(position != 10){
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
itemView.setTag(position);

} else {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.results, container,
false);
}
container.addView(itemView);
return itemView ;
}

这里是viewpager_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

这里是 viewpager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:gravity="center_horizontal" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#fff"
android:scaleType="fitCenter"
/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">

<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:text="1" />

<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="2"
android:text="2" />

<Button
android:id="@+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="3"
android:text="3" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >

<Button
android:id="@+id/Button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="4"
android:text="4" />

<Button
android:id="@+id/Button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="5"
android:text="5" />
</LinearLayout>

我不知道我哪里做错了,但无法修复。谢谢

最佳答案

你可以试试这样的——

@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater;
if(position != 10){
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
itemView.setTag("View"+position);

} else {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.results, container,
false);
}
container.addView(itemView);
return itemView ;
}

当您需要当前 View 时-

View myView = pager.findViewWithTag("View"+pager.getCurrentItem());

关于android - 从 ViewPager 获取当前 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36728306/

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