ViewPager does not show content - android

I am trying to create a ViewPager that shows a list of ImageViews. I am able to implement the pager and adapter properly, and it works as expected on swiping. However, it does not show the content of the ImageView properly, i.e. the image itself. Its as if the images are there, but they are transparent. Only one image is visible out of the whole lot.
Here is my code-
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater inflater;
List<ImageView> views = new ArrayList<>();
boolean[] done = {false,false,false,false,false};
ItemDetailFragment idf;
public ImagePagerAdapter(ItemDetailFragment idf,
LayoutInflater inflater) {
this.idf = idf;
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(idf.getActivity());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
public ImagePagerAdapter(LayoutInflater inflater) {
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(inflater.getContext());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (done[position]) {
return views.get(position);
}
ImageView v = views.get(position);
views.set(position, v);
((ViewPager) container).addView(v);
done[position] = true;
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return true;
}
}
ItemDetailFragment.java : This is where I set the adapter.
public class ItemDetailFragment extends Fragment {
ViewPager pager;
ImagePagerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
pager = (ViewPager) rootView.findViewById(R.id.pager);
adapter = new ImagePagerAdaper(this, inflater);
pager.setAdapter(adapter);
}
fragment_item_detail.xml
<LinearLayout 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:orientation="vertical"
android:padding="16dp"
tools:context="com.trial.piclist.ItemDetailFragment" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip" >
</android.support.v4.view.ViewPager>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/item_title"
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textIsSelectable="true" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/controls_layout" />
</TableRow>
<ScrollView
android:id="#+id/descScrollView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/item_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Please Help. Thanks.

Use:
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater Inflater;
Activity act;
int count;
public ViewPagerAdapter(Activity a, int c) {
act = a;
count=c;
Inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return count;
}
#Override
public Object instantiateItem(View collection, int position) {
View v1 = Inflater.inflate(R.layout.element_image, null);
ImageView image = (ImageView) v1
.findViewById(R.id.about_image);
image.setImageResource(R.drawable.light_grey_background);
((ViewPager) collection).addView(v1, 0);
return v1;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
Make a layout element_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/smv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
Replace
adapter = new ImagePagerAdaper(this, inflater);
with
adapter = new ImagePagerAdaper(getActivity(), count_of_images);

initiate the ImageView in the instantiateItem() method, set the LayoutParams of the imageView
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
Also set the bg colour for debugging
imageView.setBackgroundColor(Color.GREEN);
viewPager.setBackgroundColor(Color.RED);

Related

design layout like this image what is use to design

[ http://i.stack.imgur.com/Re29U.png]
left right swipe center value automatically checked and related table show in layout.
try this
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView[] dots;
private LinearLayout dotsLayout;
// Declare Variables
ViewPager viewPager;
String[] CustomerNameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomerNameList = new String[]{"xxxxx", "sssss", "aaaaaa", "ffffff", "gggggggg", "jjjjj"};
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
addBottomDots(0);
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.first_customer_popup_view_pager);
// Pass results to ViewPagerAdapter Class
CustomPagerAdapter adapter = new CustomPagerAdapter(this, CustomerNameList);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
}
private void addBottomDots(int currentPage) {
dots = new TextView[CustomerNameList.length];
int colorsActive = Color.rgb(102, 41, 125);
int colorsInactive = Color.rgb(217, 217, 217);
dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(100);
dots[i].setTextColor(colorsInactive);
dotsLayout.addView(dots[i]);
}
if (dots.length > 0)
dots[currentPage].setTextColor(colorsActive);
}
// viewpager change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
addBottomDots(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
};
}
CustomPagerAdapter.java
public class CustomPagerAdapter extends PagerAdapter {
private Context context;
private String[] CustomerNameList;
LayoutInflater inflater;
public CustomPagerAdapter(Context context,String[] CustomerNameList) {
this.context = context;
this.CustomerNameList = CustomerNameList;
}
#Override
public int getCount() {
return CustomerNameList.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView AO_investments_list_popup_name;
TextView AO_investments_list_popup_na;
ListView all_listView;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.adapter_single_page, container, false);
// Locate the TextViews in viewpager_item.xml
AO_investments_list_popup_name = (TextView) itemView.findViewById(R.id.display);
AO_investments_list_popup_name.setText(CustomerNameList[position]);
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((LinearLayout) object);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.agthamays.sof_1.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/first_customer_popup_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="#+id/layoutDots"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/colorAccent"
android:gravity="center|center_vertical"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
</LinearLayout>
adapter_single_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#color/colorPrimaryDark"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/display"
android:layout_width="wrap_content"
android:text="xxxx"
android:textSize="24dp"
android:paddingLeft="100dp"
android:textStyle="bold"
android:textColor="#color/colorAccent"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</LinearLayout>

Getting nullpointer exception viewPager.getChildAt(viewPager.getCurrentItem()); android

I am getting null pointer exception whenever I am trying to viewPager.getChildAt
I am attaching code snippshot
// Scroll Page Limit
private int scrollPageLimit = Integer.MAX_VALUE / 2;
private int lastPageSelected = scrollPageLimit;
viewPager = (ViewPager) findViewById(R.id.calendar);
MyPagerAdapter adapter = new MyPagerAdapter(context);
//PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(scrollPageLimit);
LinearLayout view = (LinearLayout) viewPager.getChildAt(viewPager.getCurrentItem());
GridView calendar = (GridView) view.findViewById(R.id.gridView_calendar);
// My Adapter
private class MyPagerAdapter extends PagerAdapter {
// int NumberOfPages = scrollPageLimit*2 + 1;
Context context;
public MyPagerAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Log.v("ExtendedCalendarView", "instantiateItem : "+position );
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout) inflater.inflate(
R.layout.viewpager_calendar, null);
GridView calendar = (GridView) view
.findViewById(R.id.gridView_calendar);
<!-- Calendar generation code -->
view.setTag(new MonthYearTagPager(cal.get(Calendar.MONTH), cal.get(Calendar.YEAR)));
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
Log.v("ExtendedCalendarView", "destroyItem : "+position );
container.removeView((LinearLayout) object);
}
}
<!--layout viewpager_calendar -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F0EB"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceSmallInverse" />
<GridView
android:id="#+id/gridView_calendar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:listSelector="#android:color/transparent"
android:numColumns="7">
</GridView>
If I will access LinearLayout view = (LinearLayout) viewPager.getFocusedChild(); then it works but it returns next viewpager element.

CirclePageIndicator on top of ViewPager

In my layout I am trying to get the CirclePageIndicator to sit at the bottom of my image and for my image to be aligned at the top of the screen. Whatever I try it doesn't work. Here is how I want it to look like:
What it ends up looking like is this
Here is my code for this.
<RelativeLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="top">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="3dp"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pager"/>
</RelativeLayout>
And because it is a ViewPager I added the ability to scroll through each image. Here is the code for that.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int[] Images = new int[] { R.drawable.homephoneicon1, R.drawable.homephoneicon2,
R.drawable.homephoneicon3, R.drawable.homephoneicon4, R.drawable.homephoneicon5};
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return SlideshowFragment.newInstance(Images[position]);
}
#Override
public int getCount() {
return NUM_SLIDES;
}
}
Here is the Slideshow code
public class SlideshowFragment extends Fragment {
int imageResourceId;
public static SlideshowFragment newInstance(int i) {
SlideshowFragment fragment = new SlideshowFragment();
fragment.imageResourceId = i;
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
image.setImageResource(imageResourceId);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.setGravity(Gravity.TOP);
layout.addView(image, params);
return layout;
}
}
Thanks for any help!
Here is how I did it
Code
public static SlideshowFragment newInstance(int i) {
SlideshowFragment fragment = new SlideshowFragment();
fragment.imageResourceId = i;
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
image.setImageResource(imageResourceId);
image.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout layout = new RelativeLayout(getActivity());
layout.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
layout.addView(image, params);
return layout;
}
Layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
late comer but seems a better workaround
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="#dimen/height"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_alignBottom="#+id/pager"/>
activity_main.xml:-
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:background="#ffffff"
android:layout_height="300dp">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="300dp"></android.support.v4.view.ViewPager>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:background="#android:color/transparent"
android:gravity="center_horizontal"
android:textSize="50sp" />
</RelativeLayout>
view_pager_item.xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/pagerItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAlignment="center" />
</RelativeLayout>
ViewPagerAdapter.java:-
public class ViewPagerAdapter extends PagerAdapter {
Context con;
String[] data;
public ViewPagerAdapter(Context con, String[] data) {
this.data = data;
this.con = con;
}
#Override
public int getCount() {
return data.length;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view_pager_item, container, false);
try {
TextView textView = (TextView) view.findViewById(R.id.pagerItem);
textView.setText(data[position]);
((ViewPager) container).addView(view);
} catch (Exception ex) {
ex.printStackTrace();
}
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
MainActivity.java:-
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] data = new String[]{
"page 1",
"page 2",
"page 3",
"page 4"
};
final ViewPagerAdapter adapter = new ViewPagerAdapter(this, data);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(adapter);
final TextView textView = (TextView) findViewById(R.id.textView);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
textView.setText("");
for (int i = 0; i < adapter.getCount(); i++) {
Spannable word = new SpannableString(" " + ".");
if (i == position) {
word.setSpan(new ForegroundColorSpan(Color.RED), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.append(word);
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Simple way import pagerlibrary and use this code into .xml file.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_above="#+id/twoImage"
android:id="#+id/slideLay"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/offer_pager"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/page_indicatorr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
app:fillColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:padding="5dip"
>
</com.viewpagerindicator.CirclePageIndicator>
</RelativeLayout>

android dynamic text with view pager

I have implemented a view pager in android app. I want dynamic text views to be displayed at the top and bottom of the image view. How to set different texts for every image on view pager? Let me know your suggestions.
Thanks.
You need to create custom PagerAdapter for this. I will show you a littel example :
Your Activity :
public class MainActivity
extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
List<CustomObject> items = new ArrayList<CustomObject>();
items.add(new CustomObject("First Top","First Bot"));
items.add(new CustomObject("Second Top","Second Bot"));
items.add(new CustomObject("Third Top","Third Bot"));
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(this,items);
viewPager.setAdapter(customPagerAdapter);
}}
Your adapter :
public class CustomPagerAdapter
extends PagerAdapter{
List<CustomObject> items;
LayoutInflater inflater;
public CustomPagerAdapter(Context context, List<CustomObject> items)
{
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((RelativeLayout) object);
}
#Override
public int getCount()
{
return items.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((View)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
View itemView;
itemView = inflater.inflate(R.layout.your_item_layout, container, false);
TextView topTextItem = (TextView) itemView.findViewById(R.id.topText);
TextView bottomTextItem = (TextView) itemView.findViewById(R.id.bottomText);
CustomObject customObject = items.get(position);
topTextItem.setText(customObject.topText);
bottomTextItem.setText(customObject.bottomText);
((ViewPager) container).addView(itemView);
return itemView;
}}
Main layout - view_main :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Layout item - your_item_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<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="Top Text" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#android:drawable/ic_delete" />
<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" />
Best wishes.
you can do this in your Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.spannerbord_image_new, null);
// RelativeLayout
// layout=(RelativeLayout)v.findViewById(R.id.spanner_image_relative);
Log.v("TEST", "is this printing everytime");
TextView tv = (TextView ) v.findViewById(R.id.my_tv );
}
i hope this works

ViewPager doesn't instantiate

I'm having a weird problem and I can't figure out why my ViewPager won't instantiate. According to all kinds of SO questions and tutorials, I'm doing it correctly, but when I print to the stack, it looks like the instantiateItem() method doesn't run at all, even though I'm calling setAdapter(adapter) in the onCreateView of my parent fragment. Everything above the ViewPager inflates fine, but the ViewPager just shows blank nothing.
Here's the code.
public class Profile extends Fragment {
ListView userInfo;
FeedAdapter adapter;
ViewPager pager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.user_profile, container, false);
ProfilePagerAdapter adapter = new ProfilePagerAdapter();
pager = (ViewPager) view.findViewById(R.id.profileViewPager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
TextView userName = (TextView) view.findViewById(R.id.userName);
userName.setText(Utility.userName);
Log.i("Profile", "Fragment and Pager instantiated");
return view;
}
private class ProfilePagerAdapter extends PagerAdapter{
public ProfilePagerAdapter(){
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch(position){
case 0:
resId = R.layout.news_feed;
View view1 = inflater.inflate(resId,null);
ListView feedList;
FeedAdapter feedAdapter;
feedList = (ListView) view1.findViewById(R.id.feedList);
feedAdapter = new FeedAdapter(getActivity(), Utility.newsFeed);
feedAdapter.notifyDataSetChanged();
feedList.setAdapter(feedAdapter);
((ViewPager) container).addView (view1,0);
Log.i("Profile", "Instantiated case 0");
return view1;
case 1:
resId = R.layout.splash;
View view2 = inflater.inflate(resId,null);
((ViewPager) container).addView(view2,0);
Log.i("Profile", "Instantiated case 1");
return view2;
}
return resId;
}
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
And here's the xml layout.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/coverPhoto"
android:layout_width="match_parent"
android:layout_height="133dp"
android:layout_alignParentTop="true"
android:background="#color/grey_color" />
<ImageView
android:id="#+id/profilePhoto"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:background="#color/com_facebook_blue" />
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/coverPhoto"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#id/profilePhoto"
android:text="User&apos;s Name"
android:textSize="24sp" />
<LinearLayout
android:id="#+id/profileButtonBar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/profilePhoto"
android:layout_marginTop="5dp"
android:background="#33b5e5"
android:orientation="horizontal" >
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/profileViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/profileButtonBar"
android:layout_marginTop="5dp"
android:layout_weight="1" />
</RelativeLayout>
It's because you're telling it you have 0 items:
#Override
public int getCount() {
return 0;
}

Categories

Resources