Gallery_ImageSwitcher控件

by admin on 2020年1月5日
public class MainActivity extends Activity {
    private Gallery gallery;
    private ImageSwitcher switcher;
    private List<Integer> lst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery= (Gallery) findViewById(R.id.gallery1);
        switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
        gallery.setAdapter(new MyAdapter1(this, getData()));
        gallery.setOnItemSelectedListener(new MyItemSelectedListener());
        switcher.setFactory(new Mybug());

    }
    public void setListener(){

    }

    //bug
    class Mybug implements ViewFactory{

        @Override
        public View makeView() {
            ImageView img = new ImageView(MainActivity.this);
            return img;
        }

    }


    class MyItemSelectedListener implements OnItemSelectedListener{

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            switcher.setImageResource(lst.get(arg2));       
        }



        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    }

    //获取数据
    public List<Integer> getData(){
        lst = new ArrayList<Integer>();

          Field[] field=R.drawable.class.getFields();
        for(Field ff:field){

            Integer pic = null;
            if(ff.getName().indexOf("miku")!=-1){

                try {
                    pic=ff.getInt(R.drawable.class);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                lst.add(pic);   

            }

        }

        return lst;

    }


    class MyAdapter1 extends BaseAdapter{
        private Context context;
        private List<Integer> lt;

        public MyAdapter1(Context context,List<Integer> lt) {
            super();
            this.context = context;
            this.lt = lt;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return lt.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return lt.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int position, View view, ViewGroup arg2) {

            LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.picture, null);
            ImageView imageview = (ImageView) view.findViewById(R.id.imageView1);
            imageview.setImageResource(lt.get(position));


            return view;
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
-------------------------------------------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
         android:layout_weight="1"
        android:layout_height="wrap_content" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="match_parent" >
    </ImageSwitcher>

</LinearLayout>


-----------------------------------------------------------------------------------
<?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"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图