在 Android 中从 PagerAdapter 中删除视图
Delete a View from PagerAdapter in Android
我已经搜索了多个网站,android 开发者网站,在我的问题没有运气后不得不寻求社区帮助。我们将不胜感激所有帮助和建议。
问题:
我从服务器接收到总共 12 张图像(在此处解析),我在 PagerAdapter 中显示了每张图像。如果所有值都不为空,则适配器代码可以正常工作,但问题是我允许将空值存储在服务器中。当我从服务器取回整个列表时,我只想在适配器中包含那些不包含 null 的视图。
示例:假设我得到 5 个空路径,然后我的适配器总共显示 12 个视图(7 个带有图像,其余为空白页)。
我的适配器代码:
public class ProfileImageAdapter extends PagerAdapter implements OnTouchListener{
private Context localContext;
private LayoutInflater inflater;
private List<DataModel> parseObjects = new ArrayList<DataModel>();
private List<Integer> res = new ArrayList<Integer>();
// Declare Variables
private ImageView viewPagerDisplayImage;
/**
*
* @param context : The context where to display
* @param parseObjects : The ParseObject to work with
*/
public ProfileImageAdapter(Context context, List<DataModel> parseObjects) {
this.localContext = context;
this.parseObjects = parseObjects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return parseObjects.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == ((LinearLayout) object) && object != null);
}
/**
* What is the item to show
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) localContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.profile_image_item, container, false);
// Locate the Image View in viewpager_item.xml
viewPagerDisplayImage = (ImageView) itemView.findViewById(R.id.view_pager_display_image_view);
// Getting the image
try {
ParseFile imageFile = parseObjects.get(position).getImage();
if (imageFile != null) {
byte[] bitmapImageData = imageFile.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapImageData, 0, bitmapImageData.length);
viewPagerDisplayImage.setImageBitmap(bitmap);
viewPagerDisplayImage.setOnTouchListener(this);
// Add viewpager_item.xml to ViewPager
itemView.setTag("VALID");
((ViewPager) container).addView(itemView);
} else {
((ViewPager) container).addView(itemView);
itemView.setTag("INVALID");
destroyItem(container, position, itemView);
}
} catch (ParseException e) {
e.printStackTrace();
}
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
//((ViewPager) container).invalidate();
((ViewPager) container).removeView((View) object);
}
}
我的 PagerActivity
public class PageViewerProfileImages extends Activity {
private ViewPager viewPager;
private ProfileImageAdapter profileImageAdapter;
private UserModel userModel = new UserModel();
// Create the Page View for the Profile Images
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_pager);
List<DataModel> parseObjects = userModel.getDataModels();
viewPager = (ViewPager) findViewById(R.id.pagerProfile);
profileImageAdapter = new ProfileImageAdapter(this, parseObjects);
// Setting the adapter
viewPager.findViewWithTag("VALID");
viewPager.setAdapter(profileImageAdapter);
}
}
我的参考资料:
- http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#instantiateItem(android.view.ViewGroup, 整数)
- ViewPager PagerAdapter not updating the View
- 创建一个解析服务器响应的条件。
if(imageUrl!=null){
// write code to add image url in your list, which will you pass in your adapter.
}
使用后,它只会将适配器中具有某些值的那些值传递给适配器,然后它就会起作用。
当您传递图像-Url 列表并在您的适配器列表中赋值时,您可以将此条件放在适配器中。
然后在分配您的值之前,请检查相同的条件然后分配。
我已经搜索了多个网站,android 开发者网站,在我的问题没有运气后不得不寻求社区帮助。我们将不胜感激所有帮助和建议。
问题:
我从服务器接收到总共 12 张图像(在此处解析),我在 PagerAdapter 中显示了每张图像。如果所有值都不为空,则适配器代码可以正常工作,但问题是我允许将空值存储在服务器中。当我从服务器取回整个列表时,我只想在适配器中包含那些不包含 null 的视图。 示例:假设我得到 5 个空路径,然后我的适配器总共显示 12 个视图(7 个带有图像,其余为空白页)。
我的适配器代码:
public class ProfileImageAdapter extends PagerAdapter implements OnTouchListener{
private Context localContext;
private LayoutInflater inflater;
private List<DataModel> parseObjects = new ArrayList<DataModel>();
private List<Integer> res = new ArrayList<Integer>();
// Declare Variables
private ImageView viewPagerDisplayImage;
/**
*
* @param context : The context where to display
* @param parseObjects : The ParseObject to work with
*/
public ProfileImageAdapter(Context context, List<DataModel> parseObjects) {
this.localContext = context;
this.parseObjects = parseObjects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return parseObjects.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == ((LinearLayout) object) && object != null);
}
/**
* What is the item to show
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) localContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.profile_image_item, container, false);
// Locate the Image View in viewpager_item.xml
viewPagerDisplayImage = (ImageView) itemView.findViewById(R.id.view_pager_display_image_view);
// Getting the image
try {
ParseFile imageFile = parseObjects.get(position).getImage();
if (imageFile != null) {
byte[] bitmapImageData = imageFile.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapImageData, 0, bitmapImageData.length);
viewPagerDisplayImage.setImageBitmap(bitmap);
viewPagerDisplayImage.setOnTouchListener(this);
// Add viewpager_item.xml to ViewPager
itemView.setTag("VALID");
((ViewPager) container).addView(itemView);
} else {
((ViewPager) container).addView(itemView);
itemView.setTag("INVALID");
destroyItem(container, position, itemView);
}
} catch (ParseException e) {
e.printStackTrace();
}
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
//((ViewPager) container).invalidate();
((ViewPager) container).removeView((View) object);
}
}
我的 PagerActivity
public class PageViewerProfileImages extends Activity {
private ViewPager viewPager;
private ProfileImageAdapter profileImageAdapter;
private UserModel userModel = new UserModel();
// Create the Page View for the Profile Images
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_pager);
List<DataModel> parseObjects = userModel.getDataModels();
viewPager = (ViewPager) findViewById(R.id.pagerProfile);
profileImageAdapter = new ProfileImageAdapter(this, parseObjects);
// Setting the adapter
viewPager.findViewWithTag("VALID");
viewPager.setAdapter(profileImageAdapter);
}
}
我的参考资料:
- http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#instantiateItem(android.view.ViewGroup, 整数)
- ViewPager PagerAdapter not updating the View
- 创建一个解析服务器响应的条件。
if(imageUrl!=null){ // write code to add image url in your list, which will you pass in your adapter. }
使用后,它只会将适配器中具有某些值的那些值传递给适配器,然后它就会起作用。当您传递图像-Url 列表并在您的适配器列表中赋值时,您可以将此条件放在适配器中。 然后在分配您的值之前,请检查相同的条件然后分配。