问题
I'm attempting to implement a viewPager
which will allow me to change the Youtube playlist (String PLAYLIST
) depending on the position of the ViewPager. I was told in another StackOverflow post I need to: "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."
^ - SOURCE: Use a ViewPager / PagerAdapter to change a string
According to the response to my previous SO post linked above - I belive I'll need to use something like this:
@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;
}
^ - SOURCE: http://www.piwai.info/android-adapter-good-practices/
How My Current Source Works:
I have a JSON request which contains the String PLAYLIST that gets a response from YouTube :
@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");
I would like to use a swipeable footer image (pagerAdapter) to change the value of the string PLAYLIST. My primary concern is trying to figure out how to use the PagerAdapter shown below to set the following:
String PLAYLIST = "vevo";
String PLAYLIST = "TheMozARTGROUP";
String PLAYLIST = "TimMcGrawVEVO";
String PLAYLIST = "TiestoVEVO";
String PLAYLIST = "EminemVEVO";
then immediately after the value of PLAYLIST is set by the PagerAdapter, create a playlist using the new value of PLAYLIST:
new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();
I've created a new string array containing the values I'd like to use. My question now that all that is understood is: how can I modify the source below to set the value of PLAYLIST using one of the array values and execute the new playlist? (Currently my source compiles but when I swipe the PagerAdapter - nothing happens)
Screenshot:
CURRENT SOURCE:
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
}
});
}
}
}
回答1:
see the 'getView()' method at the bottom here, comparing it to your code where you orphan the imageView... Note that in the sample, the imageView thats populated with the bitmap BELONGS to a View that is contained in the inflated View...
R.layout.item owns R.id.text R.layout.item owns R.id.image
来源:https://stackoverflow.com/questions/20500893/android-java-updating-listarray-to-call-view-in-adapter