我正在尝试实施viewPager
,这样我就可以根据ViewPager的位置更改Youtube播放列表(String PLAYLIST
)。我在另一篇StackOverflow帖子中被告知我需要:"update the listArray prior to calling 'notifyDataSetChanged()'. The call to notify is the signal to update the View in the adapter you are using. In your code, the getYouTube... needs to update and array object that the adapter has a reference to. You should be able to get it by implementing connections in your code."
^ - 来源:Use a ViewPager / PagerAdapter to change a string
根据我之前关联的SO帖子的回复 - 我相信我需要使用这样的东西:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(R.layout.home, parent, false);
}
ImageView imageView = new ImageView(context);
ImageView imageView = getItem(position);
imageView.setImageResource(imageView.getImage());
return convertView;
}
^ - 来源:http://www.piwai.info/android-adapter-good-practices/
我有一个JSON请求,其中包含获取YouTube响应的字符串PLAYLIST:
@Override
protected Void doInBackground(Void... arg0) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+PLAYLIST+"&v=2&alt=jsonc");
我想使用可滑动的页脚图像(pagerAdapter)来更改字符串PLAYLIST的值。我主要关心的是试图弄清楚如何使用下面显示的PagerAdapter来设置以下内容:
String PLAYLIST = "vevo";
String PLAYLIST = "TheMozARTGROUP";
String PLAYLIST = "TimMcGrawVEVO";
String PLAYLIST = "TiestoVEVO";
String PLAYLIST = "EminemVEVO";
然后在PagerAdapter设置PLAYLIST的值之后立即使用PLAYLIST的新值创建一个播放列表:
new GetYouTubeUserVideosTask(responseHandler,PLAYLIST).execute();
我创建了一个包含我想要使用的值的新字符串数组。我现在的问题是,所有可以理解的是:我如何修改下面的源代码,使用其中一个数组值设置PLAYLIST的值并执行新的播放列表? (目前我的源代码编译但是当我滑动PagerAdapter时 - 没有任何反应)
截图:
private class ImagePagerAdapter extends PagerAdapter {
public ImagePagerAdapter(Activity act, int[] mImages,
String[] stringArra) {
imageArray = mImages;
activity = act;
stringArray = stringArra;
}
// this is your constructor
public ImagePagerAdapter() {
super();
// setOnPageChangeListener(mPageChangeListener);
}
private int[] mImages = new int[] { R.drawable.selstation_up_btn,
R.drawable.classical_up_btn, R.drawable.country_up_btn,
R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
R.drawable.island_up_btn, R.drawable.latin_up_btn,
R.drawable.pop_up_btn, R.drawable.samba_up_btn };
private String[] stringArray = new String[] { "vevo",
"TheMozARTGROUP", "TimMcGrawVEVO", "TiestoVEVO",
"EminemVEVO" };
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Home.this;
ImageView imageView = new ImageView(context);
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(final int position) {
onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
mCurrentTabPosition = position;
}
};
protected void onTabChanged(final PagerAdapter adapter,
final int oldPosition, final int newPosition) {
// Calc if swipe was left to right, or right to left
if (oldPosition > newPosition) {
// left to right
} else {
// right to left
View vg = findViewById(R.layout.home);
vg.invalidate();
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
int oldPos = viewPager.getCurrentItem();
@Override
public void onPageScrolled(int position, float arg1, int arg2) {
if (position > oldPos) {
// Moving to the right
} else if (position < oldPos) {
// Moving to the Left
View vg = findViewById(R.layout.home);
vg.invalidate();
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
}
答案 0 :(得分:0)
查看bottom here上的'getView()'方法,将其与您孤立imageView的代码进行比较...请注意,在示例中,使用位图BELONGS填充到的ViewView包含在膨胀的视图中......
R.layout.item拥有R.id.text R.layout.item拥有R.id.image