android - ViewPager doesn't show its content - android

I have a recyclerView with images and it's supposed that an image opens in ViewPager by click. I added ViewPager in the root of my layout.
<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_centerInParent="true"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
This is pager_item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/black"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ivPhotoInPager"
android:scaleType="centerInside"/>
</RelativeLayout>
And here is my custom adapter.
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
User.Photo[] photos;
public CustomPagerAdapter(Context context,User.Photo[] photos) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.photos=photos;
}
#Override
public int getCount() {
return photos.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPhotoInPager);
loadRoundedImageToSrc(ApiHelper.API_HTTP + "/" + photos[position].photo.image, imageView, 15f, null);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
In the activity I do:
pager.setAdapter(new CustomPagerAdapter(getApplicationContext(),user.photo));
And in OnClick:
pager.setCurrentItem(position);
But nothing is shown. What can be the problem?

Related

Android ViewPageIndicator inside a Recylerview is not showing

I added a ViewPageIndicator inside recylcerview item, this is recyclerview item layout.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/background"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp"
android:fillViewport="true">
<LinearLayout
android:id="#+id/main_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="100dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/pager_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
ViewPageIndicator is not showing when scrolling down, but when I'm scrolling up it shows on some items, please help.
Issue was on the viewpager adapter, i used both FragmentStatePagerAdapter and FragmentPagerAdapter but no sucess. so finally change the adapter to PagerAdapter and my fragment view implemnted in instantiateItem in PagerAdapter
public class FeedImageAdapter extends PagerAdapter {
private ArrayList<FeedContentGroup> feedContentGroups;
private Context mContext;
private LayoutInflater layoutInflater;
public FeedImageAdapter(Context context, ArrayList<FeedContentGroup> feedContentGroups) {
this.mContext = context;
this.feedContentGroups = feedContentGroups;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
View v = layoutInflater.inflate(R.layout.feed_image_view, null);
ImageView image1 = (ImageView) v.findViewById(R.id.image1);
FeedContentGroup mFeedContentGroup = feedContentGroups.get(position);
if (mFeedContentGroup.getContent1() != null && mFeedContentGroup.getContent1().getThumbnail_file_path() != null) {
Glide.with(mContext).load(mFeedContentGroup.getContent1().getThumbnail_file_path()).into(image1);
} else {
image1.setVisibility(View.GONE);
}
((ViewPager) collection).addView(v, 0);
return v;
}
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public int getCount() {
return feedContentGroups.size();
}
}

Android viewpager is not loading the Image?

I asked very similar question in the morning but after trying something viewpager is not loading the expected images. This is my updated code:
layout_view_pager_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
</LinearLayout>
layout_data_listing_food
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Base.TextAppearance.AppCompat.Menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
.....
android:focusable="true" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
....
android:layout_alignStart="#id/profilePic" />
FoodListAdapter
public class FoodListAdapter extends ArrayAdapter<Food> {
private Context context;
private List<Food> roomList;
private ViewPager viewPager;
ArrayList<Integer> itemsimg = new ArrayList<Integer>();
public FoodListAdapter(Context context, int resource, List<Food> objects) {
super(context, resource, objects);
this.context = context;
this.roomList = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
itemsimg.add(R.drawable.temp_envelope);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.layout_data_listing_food, parent, false);
}
ViewPageAdapter slider = new ViewPageAdapter(context,itemsimg);
ViewPager vpPager = (ViewPager) convertView.findViewById(R.id.pager);
vpPager.setAdapter(slider);
return convertView;
}
}
ViewPagerAdapter
public class ViewPageAdapter extends PagerAdapter {
private ArrayList<Integer> pagerItems;
private LayoutInflater inflater;
private Context context;
private FragmentManager fragmentManager;
public ViewPageAdapter(Context context, ArrayList<Integer> pagerItems) {
super();
this.pagerItems = pagerItems;
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
try {
View imageLayout = inflater.inflate(R.layout.layout_view_pager_item, container, false);
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageView.setImageResource(pagerItems.get(position));
container.addView(imageLayout, 0);
return imageLayout;
}catch (Exception ex){
ex.printStackTrace();
return null;
}
}
#Override
public int getCount() {
return pagerItems.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
}
Expectation
List view should have to show viewpager data in each row (Right now I hardcoded the viewpager data)
Each row will contain Viewpager and TextView as we can see in layout_data_listing_food ViewPager ImageView just after the TextView
What I am doing wrong here? When I debug the code there was not Error :(
and data is also passing from one class to another. Any help would be appreciable
Problem
I can't see the Image loading through Viewpager but I can see the TextView content in layout file

Adding Textviews to ViewPager present in a ListView but the textviews are not visible

Layout of the row of list view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/linearLayout_template3">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/template3_textView"/>
<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="200dp"
android:orientation ="horizontal" />
</LinearLayout>
ListView Adapter:
Here I am using Holder class to save the Tag information of convertView.
and also giving a unique ID to viewPager.I basically intend to show Images in ViewPager but to test the code I am working with TextViews.
public class ListViewAdapter extends ArrayAdapter<ParsedItems> {
LinearLayout mainLinnerLayout;
ViewPagerAdapter pagerAdapter;
Context context;
ArrayList<ParsedItems> mObject;
ViewPagerAdapter adapter;
private ProgressDialog progressDialog;
public ListViewAdapter(Context context,int resource, ArrayList<ParsedItems> objects) {
super(context,resource,objects);
this.context = context;
this.mObject = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=null;
ParsedItems items = mObject.get(position);
String template = items.getTemplate();
ParsedItemsImage mInnerItem;
ArrayList<ParsedItemsImage> mInnerItems = items.getItems();
LinearLayout innerLinearLayout=null;
if((Holder)convertView.getTag() == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout_template3, parent,false);
ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.pager);
holder = new Holder();
holder.viewPager = viewPager;
holder.viewPager.setId(NotificationID.getID());
holder.textView = (TextView)convertView.findViewById(R.id.template3_textView);
convertView.setTag(holder);}
else
{
holder = (Holder)convertView.getTag();
}
holder.textView.setText("oyi!");
pagerAdapter = new ViewPagerAdapter(mInnerItems,context,convertView);
holder.viewPager.setAdapter(pagerAdapter);
return convertView;
}
class Holder
{
TextView textView;
ViewPager viewPager;
}
}
ViewPagerAdapter class:
public class ViewPagerAdapter extends PagerAdapter {
ArrayList<ParsedItemsImage> mInnerItems;
Context context;
private final WeakReference<View> ref;
public ViewPagerAdapter(ArrayList<ParsedItemsImage> items,Context context, View view)
{
mInnerItems = new ArrayList<ParsedItemsImage>(items);
this.context = context;
ref = new WeakReference<View>(view);
}
#Override
public int getCount() {
//return mInnerItems.size();
return 6;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return false;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View collection;
ParsedItemsImage mParsedItemsImage = mInnerItems.get(position);
String url=mParsedItemsImage.getImage();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
collection = inflater.inflate(R.layout.template3_imageview,container,false);
TextView textView = (TextView)collection.findViewById(R.id.text_temp3);
ViewPager vp = (ViewPager)container;
textView.setText("test");
vp.addView(collection);
return textView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((TextView) object);
}
}
Template3_imageview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text_temp3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/imageView_template3"/>
</LinearLayout>
Output:
SO the output shows the textview added in the ArrayAdapter's getView() method :
holder.textView.setText("oyi!");
but doesn't show the one in the ViewPager Adapter:
textView.setText("test");
Please help.
P.S.: I intend to add images finally but i was testing if textviews are getting added in pager or not.So you will see some extra code in the post.
The error was in isViewFromObject() method. I changed this
#Override
public boolean isViewFromObject(View view, Object o) {
return false;
}
to
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
and it worked!

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.

ViewPager does not show content

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);

Categories

Resources