android - Showing a fragment over a viewpager -



android - Showing a fragment over a viewpager -

i have viewpager 3 sliding fragments. each of fragment has listview contains images. i'm trying show new fragment on viewpager (like activity) when 1 of images in listview clicked , have ability navigate viewpager when button pressed.

my code listview onclick within fragment follows(for testing, i'm using onclick on entire listview , not images)

listview.setonitemclicklistener(new adapterview.onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.d(tag,"clicked"); itemdetailfragment fragment = itemdetailfragment.newinstance("ab","cd"); fragmenttransaction transaction = getfragmentmanager().begintransaction(); transaction.replace(r.id.containerr,fragment); transaction.addtobackstack(null); transaction.commit(); } });

the log statement prints out fine, fragment doesn't show up.

here's activity has viewpager

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:theme="@android:style/theme.withactionbar"> <com.firstapp.myapplication.customviews.slidingtablayout android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/> <android.support.v4.view.viewpager android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/viewpager"> </android.support.v4.view.viewpager> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/containerr"> </framelayout> </linearlayout>

please help.

also, in examples in android docs, interaction in fragment leads fragment transaction first passed parent activity handles adding or replacing new fragment. handling here in fragment itself. 1 right way , difference between 2 methods?

edit : need add together fragment on viewpager , not it. it's having new screen come on clicking element within viewpager's fragment.

thanks!

i solved adding viewpager in fragment(instead of having straight in activity itself).

my activity

<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />

and moved previous activity layout (mentioned in question) viewpagerfragment

now in activity's oncreate do

viewpagerfragment viewpagerfragment = new viewpagerfragment(); getsupportfragmentmanager().begintransaction() .add(r.id.fragment_container, viewpagerfragment) .commit();

and showing new fragment on viewpager on clicking item within viewpager's fragment,

listview.setonitemclicklistener(new adapterview.onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { mlistener.onfragmentinteraction(null); } });

the listener interface implemented in parent activity follows

@override public void onfragmentinteraction(uri uri) { itemdetailfragment itemdetailfragment = itemdetailfragment.newinstance("ab","cd"); getsupportfragmentmanager().begintransaction() .replace(r.id.fragment_container,itemdetailfragment) .addtobackstack(null) .commit(); }

i implemented via listener interface because per docs

all fragment-to-fragment communication done through associated activity. 2 fragments should never communicate directly.

and works now! :d

android

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -