Newbie trying to add images to Android ViewPager and Fragments -
hi i'm new programming. i'm struggling, limited knowledge, horizontal view swiping in android project. downloaded viewpager tutorial , implemented code project , works great but, shows text numbers (1,2,3,4,etc...). able horizontally view swipe images (with maybe text) , not numbers. i'm frustrated, tried mixing , matching code other viewpager tutorials crashes , throws errors can't understand. there simple changes make code able horizontally swipe images , text? thanks.
main activity
package com.project.viewswipingtest; import java.util.arraylist; import java.util.list; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; public class page5 extends fragmentactivity { mypageadapter pageadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.pg5); list<fragment> fragments = getfragments(); pageadapter = new mypageadapter(getsupportfragmentmanager(), fragments); viewpager pager = (viewpager)findviewbyid(r.id.viewpager); pager.setadapter(pageadapter); } private list<fragment> getfragments(){ list<fragment> flist = new arraylist<fragment>(); flist.add(myfragment.newinstance("fragment 1")); flist.add(myfragment.newinstance("fragment 2")); flist.add(myfragment.newinstance("fragment 3")); flist.add(myfragment.newinstance("fragment 4")); return flist; } private class mypageadapter extends fragmentpageradapter { private list<fragment> fragments; public mypageadapter(fragmentmanager fm, list<fragment> fragments) { super(fm); this.fragments = fragments; } @override public fragment getitem(int position) { return this.fragments.get(position); } @override public int getcount() { return this.fragments.size(); } } }
fragment class
package com.project.viewswipingtest; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class myfragment extends fragment { public static final string extra_message = "extra_message"; public static final myfragment newinstance(string message) { myfragment f = new myfragment(); bundle bdl = new bundle(1); bdl.putstring(extra_message, message); f.setarguments(bdl); return f; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { string message = getarguments().getstring(extra_message); view v = inflater.inflate(r.layout.myfragment_layout, container, false); textview messagetextview = (textview)v.findviewbyid(r.id.textview); messagetextview.settext(message); return v; } }
viewpager xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </relativelayout>
fragment xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:textappearance="?android:attr/textappearancelarge" /> </relativelayout>
in fragment xml
add <imageview>
. in fragment class.oncreateview()
, can set load bitmap
it.
Comments
Post a Comment