0

Im trying to make an image slider, but the imageview is not appearing when running on the emulator, I tried on another project and it runs perfectly. I tried changing the id of the imageView and it seems to be working fine, but nothing is showing when I run the app. at the bottom there is my xml file

Adapter class:

public class CustomSwipeAdapter extends PagerAdapter {
    private int[] image_resources = {R.drawable.mario, R.drawable.dice, R.drawable.camera};
    private Context ctx;
    private LayoutInflater layoutInflater;
    public CustomSwipeAdapter(Context ctx){
        this.ctx =ctx;
    }
    @Override
    public int getCount() {
        return ScrollingActivity.image.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {

        return (view ==(NestedScrollView)o);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        layoutInflater = (LayoutInflater)ctx.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View item_view = layoutInflater.inflate( R.layout.swipe_layout, container, false );
        ImageView imageView = (ImageView) item_view.findViewById( R.id.img);
        imageView.setImageResource( image_resources[position] );
        container.addView( item_view );
        return item_view;

    }
    @Override
    public void destroyItem (ViewGroup container, int position, Object object){
        container.removeView( (NestedScrollView)object );
    }


}

Activity class:

public class ItemsDetailsActivity extends AppCompatActivity {
    ViewPager viewPager;
    CustomSwipeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_items_details );
        viewPager = findViewById( R.id.view_pager );
        adapter = new CustomSwipeAdapter( this );
        viewPager.setAdapter( adapter );

    }
}

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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">


    <LinearLayout
        android:id="@+id/testLay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="276dp"
            android:layout_marginTop="25dp"
            android:layout_weight="1"
            android:visibility="visible" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
cutiko
  • 9,887
  • 3
  • 45
  • 59
Werokk
  • 89
  • 2
  • 14
  • post your layout please , in which the imageView is present !! – Santanu Sur Mar 28 '18 at 18:38
  • I just included my layout at the bottom – Werokk Mar 28 '18 at 18:43
  • now finally post the ScrollingActivity , – Santanu Sur Mar 28 '18 at 18:47
  • Why the nestedscroll? Try removing it. Also a general suggestiong for the adapter is to keep the layoutinflater as a local variable and is usually instantiated like this: LayoutInflater inflater = LayoutInflater.from(container.getContext); – cutiko Mar 28 '18 at 18:50
  • Nested scroll is there because i'll add a textView in the same layout, so I'll be able to scroll down – Werokk Mar 28 '18 at 18:52
  • Thanks for your help guys, I fixed it changing return ScrollingActivity.image.size(); with return image_resources.length; – Werokk Mar 28 '18 at 19:00

0 Answers0