我是Android编程的新手。我有一个带有图片的图库,当我点击一个图像时,应该会出现一个带有全屏图像和标题的ViewPager,我可以滑过整个图库。我遵循这个教程enter link description here唯一的区别是我的图像也存储在一个数组中,标题。当我运行应用程序时,画廊正在显示,但Viewpager是空的而不是滑动。我没有错,我不知道应该是什么错。在此先感谢:)
FullScreenImageAdapter.java
package com.example.sherlockholmes.images;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.sherlockholmes.R;
public class FullScreenImageAdapter extends PagerAdapter {
private Activity context;
private LayoutInflater inflater;
private int _position;
private Integer[] mThumbIds;
private String[] description;
public FullScreenImageAdapter(Activity context,Integer[] mThumbIds,String[] description){
this.context=context;
this.mThumbIds = mThumbIds;
this.description = description;
}
@Override
public int getCount() {
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
false);
ImageView imageView = (ImageView) viewLayout.findViewById(R.id.full_image);
TextView textview = (TextView) viewLayout.findViewById(R.id.item_text);
Button btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
imageView.setImageResource(mThumbIds[_position]);
textview.setText(description[_position]);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.finish();
}
});
((ViewPager) container).addView(viewLayout,0);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
FullScreenViewActivity.java
package com.example.sherlockholmes.images;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.example.sherlockholmes.R;
public class FullScreenViewActivity extends Activity{
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
private Integer[] mThumbIds = {
R.drawable.acd1, R.drawable.acd1,
R.drawable.acd1, R.drawable.acd1,
R.drawable.acd1, R.drawable.acd1,
R.drawable.acd1, R.drawable.acd1,
};
private String[] description={
"apple","pinaple","rose","strawberry",
"orange","lemon","potato","banana"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
viewPager = (ViewPager) findViewById(R.id.pager);
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,mThumbIds,description);
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
}
答案 0 :(得分:2)
试试这个
@Override
public int getCount() {
return mThumbIds.length;
}
而不是
@Override
public int getCount() {
return 0;
}
在FullScreenImageAdapter
并改变这个
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
用
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
并且还改变了这个
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
使用
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
更新:您还需要更改此
imageView.setImageResource(mThumbIds[_position]);
textview.setText(description[_position]);
使用
imageView.setImageResource(mThumbIds[position]);
textview.setText(description[position]);
在你的FullScreenImageAdapter
。它是becoz你没有初始化_position这就是为什么视图寻呼机总是从位置0的数组中获取图像和标题。