在网格视图和滑动视图之间设置单击侦听器

时间:2016-03-07 18:08:02

标签: android android-studio gridview swipeview

我正在尝试制作一个壁纸应用程序,其中图像以网格视图排列。当单击图像时,它将在滑动视图中显示以下图像。我每次都进行了调整,但我没有得到正确的答案。这是我的gridview的代码

 package com.example.neelaysrivastava.pr7;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.content.Context;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.BaseAdapter;
 import android.widget.GridView;
 import android.widget.ImageView;
 import android.widget.Toast;

 public class MainActivity extends Activity {
 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
    ,R.drawable.img18,R.drawable.img19,R.drawable.img20,R.drawable.img21,R.drawable.img22,R.drawable.img23,R.drawable.img24,R.drawable.img25,R.drawable.img26,R.drawable.img27
    ,R.drawable.img28,R.drawable.img29,R.drawable.img30,R.drawable.img31,R.drawable.img32,R.drawable.img33,R.drawable.img34,R.drawable.img35,R.drawable.img36,R.drawable.img37
    ,R.drawable.img38,R.drawable.img39,R.drawable.img40,R.drawable.img41,R.drawable.img42,R.drawable.img43,R.drawable.img44,R.drawable.img45,R.drawable.img46,R.drawable.img47,
 R.drawable.img48};
    @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gridView =(GridView)findViewById(R.id.gridView);
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(),"pic"+(position+1)+"selected",Toast.LENGTH_SHORT).show();

        }
    });

}
     public class ImageAdapter extends BaseAdapter{
    private Context context;
    private Activity _activity;
  public ImageAdapter(Context c){
    context =c;
}
    @Override
    public int getCount() {
        return imageIDs.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView==null){

            imageView=new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(200,200));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setOnClickListener(new OnImageClickListener(position));

        }
        else {
            imageView=(ImageView)convertView;
        }
        imageView.setImageResource(imageIDs[position]);
        return imageView;
    }
    class OnImageClickListener implements View.OnClickListener {
        int _position;
        public OnImageClickListener(int position){
            this._position=position;
        }
        @Override
        public void onClick(View v) {
            Intent i =new Intent(_activity,Main2Activity.class);
            i.putExtra("position",_position);
            _activity.startActivity(i);

        }
    }
}

}

0 个答案:

没有答案