我正在尝试制作一个项目,其中图像排列在Gridview
中,当点击图像时,它将打开ViewPager
中的图像。但我无法设置,因为我是Android的初学者。以下是我的GridView
和Swipeview
。
GridView的
package com.example.neelaysrivastava.pr19;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class Main2Activity extends Activity {
Integer []imgID={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};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
GridView gridView=(GridView)findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),"pic"+(position)+"select",Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter{
private Context context;
public ImageAdapter(Context c){
context =c;
}
@Override
public int getCount() {
return imgID.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);}
else{
imageView=(ImageView)convertView;
}
imageView.setImageResource(imgID[position]);
return imageView;
}
}
}
SwipeView
package com.example.neelaysrivastava.pr19;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MainActivity extends Activity {
ViewPager viewPager;
CustomeSwipeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager =(ViewPager)findViewById(R.id.viewpager);
adapter =new CustomeSwipeAdapter(this);
viewPager.setAdapter(adapter);
}
public void back(View v){
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
}
SwipeViewAdapter
package com.example.neelaysrivastava.pr19;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class CustomeSwipeAdapter extends PagerAdapter {
private Context ctx;
private LayoutInflater layoutInflater;
private int []imgID={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};
public CustomeSwipeAdapter(Context ctx) {
this.ctx=ctx;
}
@Override
public int getCount() {
return imgID.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view =layoutInflater.inflate(R.layout.extra,container,false);
ImageView imageView=(ImageView)item_view.findViewById(R.id.imageView);
Button button=(Button)item_view.findViewById(R.id.button);
Button button1=(Button)item_view.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
imageView.setImageResource(imgID[position]);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
答案 0 :(得分:0)
这对我来说很有效。
<强> Main2Activity.java 强>
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
intent.putExtra("picture", position);
startActivity(intent);
}
});
<强> MainActivity.java 强>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
adapter = new CustomeSwipeAdapter(this);
viewPager.setAdapter(adapter);
// add this
viewPager.setCurrentItem(getIntent().getIntExtra("picture", 0));
}