android - Getting current view from ViewPager -
i'm trying current page view viewpager viewpager.findviewwithtag. when try current view viewpager returns 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());
... , here's 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 ; }
here's 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>
and here's 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>
i don't know did mistake couldn't fix it. thanks
you can try -
@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 ; }
and when need current view-
view myview = pager.findviewwithtag("view"+pager.getcurrentitem());
Comments
Post a Comment