Android viewpager is not loading the Image? - android

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

Related

Swiping through Bitmap Images by using a ViewPager

I'm trying to create an app where I can swipe through photos. I currently have the images saved as bitmaps. How can I use ViewPager to swipe through the images? (Preferably without Picasso or Glide)
You said you have the bitmaps, store it in arraylist. First, make an Adapter for your ViewPager.
public class AdapterPagerImageSlider extends PagerAdapter {
private ArrayList<Bitmap> bitmaps;
private Context context;
private LayoutInflater layoutInflater;
public AdapterPagerImageSlider(Context context, ArrayList<Bitmap> bitmaps) {
this.context = context;
this.bitmaps= bitmaps;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.inflater_imageslider, container, false);
ImageView imageView = view.findViewById(R.id.image);
imageView.setImageBitmap(bitmaps.get(position)); //this set image from bitmap
container.addView(view);
return view;
}
#Override
public int getCount() {
return bitmaps.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return (view == o);
}
}
Then, make a layout and name it inflater_imageslider
<?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="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Last, declare that adapter and set it to your ViewPager.
ArrayList<Bitmap> bitmaps = new ArrayList<>();
//code of storing your bitmaps to arraylist here : bitmaps.add(yourbitmap);
ViewPager vp = findViewById(R.id.vp);
AdapterPagerImageSlider adapter = new AdapterPagerImageSlider(MainActivity.this, bitmaps);
vp.setAdapter(adapter);
Hope it helps.

Android Studio: Simple BaseAdapter Setup Fail

I would like to create a simple populated GridView using BaseAdapter but I keep getting empty list - application runs but there are no Views displayed.
Below are all files are use for this task:
Main:
String[] items = {"Some", "items", "to", "display"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView grid = (GridView)findViewById(R.id.grid);
final TextView text = (TextView)findViewById(R.id.text);
MyAdapter adapter = new MyAdapter(getApplicationContext(), items);
grid.setAdapter(adapter);
MyAdapter:
LayoutInflater inflater;
Context context;
String[] myData;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.myData = myData;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return myData[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.grid, parent, false);
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(myData[position]);
return convertView;
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.random.MainActivity">
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="40dip"
android:horizontalSpacing="5dip"
android:numColumns="auto_fit"
android:columnWidth="100dip"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</android.support.constraint.ConstraintLayout>
grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
I have checked in various sources countles times and I did not manage to find an error in my code. It would be amazing if someone would take a look at it and find a reason for empty activity as the reason is probably obvious.
Your getCount method always return 0. Return the number of elements in your myData array instead.
#Override
public int getCount() {
return myData.length;
}
You can try this my friend
public class MyAdapter extends BaseAdapter {
Context context;
String[] data;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.data = myData;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.grid, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(data[position]);
return convertView;
}
}

android - ViewPager doesn't show its content

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?

How to add ImageView viewPager in ListFragment in android?

I have a ViewPager inside ListView in android screen:
<?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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="55dip"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_below="#+id/imageView"
android:autoText="false"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:padding="10dp"
android:text="08/01 MidTown"
android:textColor="#21252D"
android:textSize="20sp" />
</RelativeLayout>
My ListFragment class
This class is calling the ArrayAdapter to load the data into above Textview.
public class FoodTabFragment extends ListFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setDivider(null);
final AppDelegate contextScope = (AppDelegate) getActivity().getApplicationContext();
FoodListAdapter adapt = new FoodListAdapter(getActivity().getApplicationContext(), R.layout.layout_listing_food, contextScope.foodList);
setListAdapter(adapt);
}
}
FoodListAdapter
public class FoodListAdapter extends ArrayAdapter<Food> {
private Context context;
private List<Food> foodList;
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) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.layout_listing_food, parent, false);
}
return convertView;
}
SlidingImageAdapter
public class SlidingImageAdapter extends PagerAdapter {
private ArrayList<Integer> IMAGES;
private LayoutInflater inflater;
private Context context;
public SlidingImageAdapter(Context context, ArrayList<Integer> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public int getCount() {
return IMAGES.size();
}
#Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.framelayout_listing_image_slider, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageView.setImageResource(IMAGES.get(position));
view.addView(imageLayout, 0);
return imageLayout;
}
Question
How can I merge slidingImageAdapter int FoodListAdapter. So that I can add viewpager in Listview in layout class?
In general I would suggest you to move to RecyclerView.
Below you can find simple solution for your current implementation.
Changes for FoodListAdapter:
Change list of food to map, where Food is id and related SlidingImageAdapter is value.
private Map<Food, SlidingImageAdapter> foodMap;
This will avoid recreating of adapter every getView call.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.layout_listing_food, parent, false);
}
Food food = getItem(position);
SlidingImageAdapter adapter;
// adapter creation or reuse
if (foodMap.contains(food)) {
adapter = foodMap.get(food);
} else {
adapter = new SlidingImageAdapter(...);
foodMap.put(food, adapter);
}
convertView.findViewById(R.id.pager).setAdapter(adapter);
return convertView;
}
P.S. do not forget to override hashCode() and equals(Object o) in your Food entry.

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!

Categories

Resources