Sunday 15 February 2015

android dynamic text with view pager -


I have implemented a View Pager in the Android app. I have to show the dynamic text view above and below the image view. How do I show different text on each page on pager? Tell me your suggestion.

Thank you.

You need to create custom this. I'd show you a small example:

Your activity:

  public class MainActivity activity extends {@Override public void OnCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState ); SetContentView (R.layout.view_main); & Lt; CustomObject & gt; Item = new arreelist & lt; Custom Object & gt; (); Items.add (new custom object ("first top", "first bot")); Items.add (new custom object ("second top", "second bot")); Items.add (new custom object ("Third Top", "Third Bot")); ViewPager viewPager = (ViewPager) findViewById (R.id.viewPager); CustomPagerAdapter CustomPaperAdapter = New CustomPagerAdapter (this, item); ViewPager.setAdapter (customPagerAdapter); }}   

Your adapter:

  Public class CustomPagerAdapter Pager Adapter {list & lt; CustomObject & gt; item; LayoutInflat Flatter; Public CustomPagerAdapter (context reference, from the list & lt; CustomObject & gt; items) {this.inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); this. Item = item; } @ Override Public Boolean isViewFromObject (View View, Object Object) {Return View == ((Relative Layout Object)); } @ Override Public In Receipt () {Return items. Size (); } @ Override Public Null destroyed item (ViewGroup container, condition of int, object object) {((Viewer) container) .removeView ((View) object); } @ Override public object instant item (see group container, int position) {see item see; ItemView = inflater.inflate (R.layout.arial_item_layout, container, incorrect); TextView topTextItem = (TextView) itemView.findViewById (rt.txttext); TextView ThattestItem = (TextView) itemView.findViewById (R.id.bottomText); Custom object custom object = items.jet (status); TopTextItem.setText (customObject.topText); BottomTextItem.setText (customObject.bottomText); (ViewPager) container) .addView (itemView); See Return Items; }}   

Main Layout - View_man:

  & lt; linearLayout xmlns: android = "http://schemas.android.com/apk/res/ Android" xmlns: device = "http://schemas.android.com/tools" Android: layout_width = "fill_parent" Android: layout_height = "Fill_parent" android: gravity = "center" android: orientation = "vertical" & gt; & Lt; android.support.v4.view.ViewPager android: id = "@ + id / viewPager" Android: layout_width = "match_parent" Android: layout_height = "match_parent" / & gt;   

Layout items - your_item_layout:

  & lt; Relative layout xmlns: android = "http://schemas.android.com / Apk / res / android" android: layout_width = "fill_parent" android: layout_height = "wrap_content" & gt; & Lt; TextView android: id = "@ + id / topText" style = "@ android: style / TextAppearance.Medium" Android: layout_marginTop = "20dp" Android: Layout_width = "Match_parent" Android: Layout_height = "Wrap_content" android: gravity = " Center "Android: text =" header "/> & Lt; ImageView Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: src = "@ android: drawable / Aisi_delit" / & gt; & Lt; TextView style = "@ android: style / TextAppearance.Medium" Android: layout_alignParentBottom = "true" android: id = "@ + id / bottomText" Android: Layout_width = "Match_parent" Android: Layout_height = "Wrap_content" Android: Layout_marginBottom = " 20dp "Android: gravity =" center "Android: text =" bottom text "/>   

Good luck.

No comments:

Post a Comment