how i can display my gallery in tabhost with fragment

21 views Asked by At

Here is the code of the fragment in which I am setting a custom adapter to the gallery, no errors but nothing to view it's empty. i want to display this gallery in one tab of tabhost.

Souar.java class fragment

package com.TP.dhikr;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class Souar extends Fragment {

//---the images to display---
    Integer[] imageIDs= {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img6,
        R.drawable.img7,
        R.drawable.img8,
        R.drawable.img9,
        R.drawable.img10,
        R.drawable.img11,
        R.drawable.img12,
        R.drawable.img13,
        R.drawable.img14,
        R.drawable.img15,
        R.drawable.img16,
        R.drawable.img17
    };
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
         View view = inflater.inflate(R.layout.lay_souar, container, false);
         //ImageView imageView = (ImageView) view.findViewById(R.id.image1);


         Gallery gallery =(Gallery) getView().findViewById(R.id.gallery1);

        gallery.setAdapter(new ImageAdapter(getActivity()));
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v,int position, long id){
                //Toast.makeText(getBaseContext(), "img"+(position + 1) + " selected", Toast.LENGTH_LONG).show();
                ImageView imageView = (ImageView) getView().findViewById(R.id.image1);
                imageView.setImageResource(imageIDs[position]);
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedState) {
        super.onActivityCreated(savedState);


    }


    public class ImageAdapter extends BaseAdapter
    {
        Context context;
        int itemBackground;
        public ImageAdapter(Context c){
            context = c;
            TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
            itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount(){
            return imageIDs.length;
        }

        public Object getItem(int position){
            return imageIDs.length;
        }

        public long getItemId(int position){
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent){
            ImageView imageView;
            if (convertView== null){
                imageView = new ImageView(context);
                imageView.setImageResource(imageIDs[position]);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams( new Gallery.LayoutParams(150, 120));
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setBackgroundResource(itemBackground);
            return imageView;
        }
    }

} 
0

There are 0 answers