[英]Delete a View from PagerAdapter in Android
I have searched multiple websites, android developer sites and after no luck to my issue had to seek the community help. 我已经搜索了多个网站,android开发者网站,但碰巧遇到了麻烦,我不得不寻求社区的帮助。 All help and suggestion will be highly appreciated.
所有帮助和建议将不胜感激。
ISSUE : 问题 :
I have in total 12 images that I receive from the server (Parse in here) and I show each of them in a PagerAdapter. 我总共从服务器收到12张图像(在此处解析),并在PagerAdapter中显示它们。 If all the values are not null the adapter code works fine, the twist is I allow null values to be stored in server.
如果所有值都不都是null,则适配器代码可以正常工作,但有一个缺点是我允许将null值存储在服务器中。 When I get the whole list back from server, I just want to have those views in adapter which contains not null.
当我从服务器取回整个列表时,我只想将那些视图包含在不包含null的适配器中。 Example : Suppose 5 null paths I get then my adapter shows in total 12 views (7 with images, rest as blank pages).
示例:假设我得到5个空路径,然后我的适配器总共显示12个视图(其中7个带有图像,其余为空白页)。
My Adapter Code: 我的适配器代码:
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);
}
}
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);
}
}
if(imageUrl!=null){ // write code to add image url in your list, which will you pass in your adapter. }
if(imageUrl!=null){ // write code to add image url in your list, which will you pass in your adapter. }
After using it, it will pass in adapter only those value in adapter which have some value, then it will work. if(imageUrl!=null){ // write code to add image url in your list, which will you pass in your adapter. }
使用它之后,它将仅将那些具有一定值的值传递给适配器,然后它将起作用。
You can place this condition in adapter when you pass your image-Url list and assign value in your adapter list. 当您通过图像URL列表并在适配器列表中分配值时,可以将此条件放置在适配器中。 then before assigning your value please check same condition then assign.
然后在分配您的值之前,请检查相同条件,然后分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.