Android Code using Fragments with listView - java

So my coding got a few problems in its fragment the line "super(FragmentDishcover.this, R.layout.listviewdishcover, LV_Dishcover);" goes red line. i copied most of its files on the net and got no idea whats "getLayoutInflater()" is for. i thought it a new class or something but well tell me if it its please.
my FragmentD.class
public class FragmentD extends Fragment {
public FragmentD(){}
private List<NavItemViewlistD> LV_D = new ArrayList<NavItemViewlistD>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_d, container, false);
populateListViewD();
populateListView();
return rootView;
}
private void populateListViewD(){
LV_D.add(new NavItemViewlistD("Trololololol", R.drawable.kittylogotemp, R.drawable.ic_paw_white, 10));
LV_D.add(new NavItemViewlistD("booom", R.drawable.kittylogotemp, R.drawable.ic_paw_white, 20));
LV_D.add(new NavItemViewlistD("ahahahaha", R.drawable.kittylogotemp, R.drawable.ic_paw_white, 30));
}
private void populateListView(){
ArrayAdapter<NavItemViewlistD> adapter = new dListAdapter();
ListView list = (ListView) getActivity().findViewById(R.id.listViewD);
list.setAdapter(adapter);
}
private class dListAdapter extends ArrayAdapter<NavItemViewlistD>{
private dListAdapter(){
super(FragmentD.this, R.layout.listviewd, LV_D);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.listviewd, parent, false);
}
return itemView;
}
}
}
this is my NavItemViewList.java
public class NavItemViewlist {
private String title;
private int image;
private int icon;
private int points;
public NavItemViewlist(String title, int image, int icon, int points){
super();
this.title = title;
this.image = image;
this.icon = icon;
this.points = points;
}
public String getTitle(){
return title;
}
public int getImage(){
return image;
}
public int getIcon(){
return icon;
}
public int getPoints(){
return points;
}
}

YOUR DOING IN WRONG WAY.i suggested you to not include adapter in fragment.use as separate class like this
public class SampAdapter extends ArrayAdapter<Contact> {
/**
* #param context
* #param resource
* #param objects
*/
public SampAdapter(Context context, int resource, Contact[] objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
#Override
public int getCount() {
return super.getCount();
}
}
and set yout adapter to your list view in fragment like this
SampAdapter ad=new SampAdapter(getActivity(), R.layout.your_root_view, YourArrayObject);
if its not working change to base adapter and write your own Constructor with Context,YourArrayObject and try.

I think this would help you make a java class adapter to call the items
public class DListViewAdapter extends ArrayAdapter<DListView> {
public DListViewAdapter(Context context, List<DListView> items) {
super(context, R.layout.listviewd, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listviewd, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.icon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.title);
viewHolder.tvpoints = (TextView) convertView.findViewById(R.id.points);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
DListView item = getItem(position);
viewHolder.ivIcon.setImageDrawable(item.iconAdapter);
viewHolder.tvTitle.setText(item.titleAdapter);
viewHolder.tvpoints.setText(item.pointsAdapter);
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDescription;
}
}
create an listviewItem java class like this one i made for you :D
public class DListView {
public final Drawable iconAdapter;
public final String titleAdapter;
public final String pointsAdapter;
public DListView(Drawable icon, String title, String points) {
this.iconAdapter = icon;
this.titleAdapter = title;
this.pointsAdapter = points;
}
}
and create the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewD"
android:layout_centerHorizontal="true" />
</RelativeLayout>
and i kinda forgot what to put at the fragment be right back on it :(

Related

Wrap content of items individually in a Recycler View?

I'm trying to figure out how to do this in the most simple way possible. Setting the recycler view to wrap the content of course, adjusts the recycler to the biggest item. Setting it to match_parent adjusts all the items to be the maximum at all times.
Setting the maximum width in the Adapter makes all the items be that size no matter what. Dumbfounded at the moment.
My Adapter class
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private ArrayList<Message> messageList;
private Context context;
private MessageSelectListener messageSelectListener;
public MessageAdapter(Context context, ArrayList<Message> messageList){
this.messageList = messageList;
this.context = context;
}
//Create an interface
public interface MessageSelectListener{
void onMessageClick(Message message, int position);
}
public void setMessageClickListener(MessageSelectListener messageSelectListener){
this.messageSelectListener = messageSelectListener;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
//View view = new TextView(context);
//View rootView = LayoutInflater.from(context).inflate(R.layout.order_list, parent, false);
return new MessageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MessageViewHolder holder, final int position) {
final Message message = messageList.get(position);
holder.text.setText(message.getMessageText());
holder.text.setBackgroundResource(R.drawable.rounded_corner_white_noborder);
// passing Order and Position as parameter to interface method,
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
messageSelectListener.onMessageClick(message, position);
}
});
}
#Override
public int getItemCount() {
return messageList == null ? 0 : messageList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView text;
public Message message;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
text = (TextView) itemView;
}
}
}
The Solution was to change the layout parameters in the onCreateViewHolder() method to WRAP_CONTENT
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.**WRAP_CONTENT**, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
//View view = new TextView(context);
//View rootView = LayoutInflater.from(context).inflate(R.layout.order_list, parent, false);
return new MessageViewHolder(view);
}

Universal Image Loader image scaling distortion

I have a ListView that presents a picture and some text on each row. The picture is loaded dynamically at run time using Universal Image Loader. It works, but the problem is that the pictures are not scaled correctly. I want them to be scaled to retain their original aspect ratio, but instead they are stretched to fill the square dimension of their container thereby distorting many of the images.
The listview adapter is this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<ImageView
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="6dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/my_img"/>
<TextView
android:layout_toRightOf="#+id/image"
android:textSize="16sp"
android:id="#+id/coach_name"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
The image gets loaded from a server at run time. This is the adapter class:
public class CoachAdapter extends ArrayAdapter<String> {
private static Context context;
private ArrayList<String> coach_list;
private DisplayImageOptions doption=null;
private ImageLoadingListener animateFirstListener =null;
public CoachAdapter(Context context, int textViewResourceId, ArrayList<String> coach_list) {
super(context, textViewResourceId, coach_list);
this.context = context;
this.coach_list = coach_list;
doption=new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.my_img)
.showImageOnFail(R.drawable.my_img)
.showImageOnLoading(R.drawable.my_img)
.cacheInMemory(true)
.cacheOnDisk(true)
.displayer(new RoundedBitmapDisplayer(8))
.imageScaleType(ImageScaleType.EXACTLY)
.postProcessor(new BitmapProcessor() {
#Override
public Bitmap process(Bitmap bmp) {
return Bitmap.createScaledBitmap(bmp, 32, 32, false);
}
})
.build();
animateFirstListener = new AnimateFirstDisplayListener();
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_coach, parent, false);
holder = new ViewHolder();
holder.text = (TextView) row.findViewById(R.id.coach_name);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
String coach_name = coach_list.get(position);
// get the url string from the database
Coach this_coach = myDB.getCoachFromName(coach_name);
String coach_picture_url = this_coach.getCoachPictureURL();
holder.text.setText(coach_name);
holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(url, holder.image, doption, animateFirstListener);
return row;
}
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
private class ViewHolder {
public TextView text;
public ImageView image;
}
}
As I read the UniversalImageLoader docs, this is supposed scale the images based on what is specified in the XML file. I've tried fitCenter and fitXY. No matter what I specify, the image is always square. I've also verified that the dimensions of many of the original images are NOT square, so I know that the scaling distortion is happening here.
Any one see what I am doing wrong? Thank you!

Attempt to invoke virtual method ' 'on a null object reference

I am getting the following error
Attempt to invoke virtual method 'void
android.widget.StackView.setAdapter(android.widget.Adapter)' on a null
object reference
on this line
stackView.setAdapter(adapter);
The complete fragment EventsFragment.java is
public class EventsFragment extends android.support.v4.app.Fragment {
private StackView stackView;
private ArrayList<Stack_Items> list;
TypedArray eventLogo ;
String eventName[];
#Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
eventLogo= getResources().obtainTypedArray(R.array.event_stack_icon);
eventName = getResources().getStringArray(R.array.event_stack);
stackView = (StackView) getActivity().findViewById(R.id.stackView1);
list = new ArrayList<Stack_Items>();
//Adding items to the list
for (int i = 0; i < eventLogo.length(); i++) {
list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i,-1)));
}
//Calling adapter and setting it over stackView
Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list );
stackView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return inflater.inflate(R.layout.events_layout, null);
}
}
Stack_Adapter.java
public class Stack_Adapter extends BaseAdapter {
ArrayList<Stack_Items> arrayList;
LayoutInflater inflater;
ViewHolder holder = null;
public Stack_Adapter(Context context, ArrayList<Stack_Items> arrayList) {
this.arrayList = arrayList;
this.inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Stack_Items getItem(int pos) {
return arrayList.get(pos);
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.stack_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.textView1);
holder.image = (ImageView) view.findViewById(R.id.imageView1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(arrayList.get(pos).getName());
holder.image.setBackgroundResource(arrayList.get(pos).getImage());
return view;
}
public class ViewHolder {
TextView text;
ImageView image;
}
}
Stack_Items
public class Stack_Items {
String name;
Integer image;
public Stack_Items(String name, Integer image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
}
}
You are doing:
stackView = (StackView) getActivity().findViewById(R.id.stackView1);
Your stackView is null. getActivity().findViewById returns null.
Why are you using getActivity()?
Where is the stackView? You should load it from the right xml.
as #Tauqir mentioned, you need to inflate the right xml like this:
View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
return view;
Try this inside onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.events_layout, null);
-------------some codes ------------
stackView = (StackView) view.findViewById(R.id.stackView1);
-------------some codes ------------
return view;
}
Do this
View rootView = inflater.inflate(R.layout.events, container, false);
stackView = (StackView) rootview.findViewById(R.id.stackView1);
.
.
.
return rootview
You first need to inflate the layout and call findViewById() on the View it returns, thus getActivity().findViewById should be rootView.findViewByID.
There are some other issues with your code as well. Like getctivity().getApplicationContext, just getActivity() is fine.
R.id.stackView1 is not found when you try to assign stackView, so it is null
change container and false to null
Do this:
View rootView = inflater.inflate(R.layout.events, null);

Android + ListView + swipe element

i'm using this library for my project, everything is works fine(swiping elements), but my questions is - is it possible to start swiping animation by click on some ListView element?
and second question:
how can i implement timedSwipeUndo for custom object?
here is my code for ListView
final ArrayAdapter<Product> adapter = new MyListAdapter(this,R.layout.text_view,productsDef);
SimpleSwipeUndoAdapter simpleSwipeUndoAdapter = new SimpleSwipeUndoAdapter(adapter, this, new MyOnDismissCallback(adapter));
AlphaInAnimationAdapter animAdapter = new AlphaInAnimationAdapter(simpleSwipeUndoAdapter);
animAdapter.setAbsListView(listView);
assert animAdapter.getViewAnimator() != null;
animAdapter.getViewAnimator().setInitialDelayMillis(INITIAL_DELAY_MILLIS);
listView.setAdapter(animAdapter);
/* Enable drag and drop functionality */
listView.enableDragAndDrop();
listView.setDraggableManager(new TouchViewDraggableManager(R.id.list_row_draganddrop_touchview));
listView.setOnItemMovedListener(new MyOnItemMovedListener(adapter));
listView.setOnItemLongClickListener(new MyOnItemLongClickListener(listView));
/* Enable swipe to dismiss */
listView.enableSimpleSwipeUndo();
/* Add new items on item click */
//listView.setOnItemClickListener(new MyOnItemClickListener(listView));
In Doc is - for timedUndoAdapter use TimedUndoAdapter. but when i trying create adapter i have error:
Error:(249, 51) error: constructor TimedUndoAdapter in class TimedUndoAdapter cannot be applied to given types;
required: V,Context,OnDismissCallback
found: ArrayAdapter<Product>,DefaultProductsListUserEdit,DefaultProductsListUserEdit.MyOnDismissCallback
reason: inferred type does not conform to declared bound(s)
inferred: ArrayAdapter<Product>
bound(s): BaseAdapter,UndoAdapter
where V is a type-variable:
V extends BaseAdapter,UndoAdapter declared in constructor <V>TimedUndoAdapter(V,Context,OnDismissCallback)
i can't use TimedAdapter for custom object or i must override some method?
here is myAdapter:
public class MyListAdapter extends ArrayAdapter<Product> implements UndoAdapter {
private final Context mContext;
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
ArrayList<Product> products = new ArrayList<Product>();
final int INVALID_ID = -1;
LayoutInflater lInflater;
public MyListAdapter(Context context, int textViewResourceId, List<Product> prod) {
//super(context, textViewResourceId, prod);
super(prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
for (int i = 0; i < prod.size(); i++) {
//add(prod.get(i));
mIdMap.put(prod.get(i),i);
}
}
#Override
public long getItemId(final int position) {
//return getItem(position).hashCode();
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
//TextView view = (TextView) convertView;
View view = convertView;
if (view == null) {
//view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
//view.setText(getItem(position).getProductName());
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
return view;
}
#NonNull
#Override
public View getUndoView(final int position, final View convertView, #NonNull final ViewGroup parent) {
View view = convertView;
if (view == null) {
//view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false);
view = lInflater.inflate(R.layout.undo_row, parent, false);
}
return view;
}
#NonNull
#Override
public View getUndoClickView(#NonNull final View view) {
return view.findViewById(R.id.undo_row_undobutton);
}
public View getHeaderView(final int position, final View convertView, final ViewGroup parent) {
TextView view = (TextView) convertView;
//View view = convertView;
if (view == null) {
//view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_header, parent, false);
//view = lInflater.inflate(R.layout.list_header, parent, false);
}
//view.setText(mContext.getString(R.string.header, getHeaderId(position)));
return view;
}
public long getHeaderId(final int position) {
return position / 10;
}
}
and class MyOnDismissCallback(adapter):
private class MyOnDismissCallback implements OnDismissCallback {
private final ArrayAdapter<Product> mAdapter;
#Nullable
private Toast mToast;
MyOnDismissCallback(final ArrayAdapter<Product> adapter) {
mAdapter = adapter;
}
#Override
public void onDismiss(#NonNull final ViewGroup listView, #NonNull final int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mAdapter.remove(position);
}
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(
DefaultProductsListUserEdit.this,
"REMOVED ITEM",
Toast.LENGTH_LONG
);
mToast.show();
}
}
Thx in advance!

ListView items reordering and repeating using baseadapter

I am trying to implement a listview that displays a list of directories. and under each directory is a gridview with associated adapter (shown below) showing a list of image thumbnails (see below image). I have it working great except whenever the list item is off the screen then brought back on screen, the images are reloaded. I am using an asynctask to download the thumbnails and replace the placeholder image for each imageview so it is not acceptable that everytime an item is offscreen, all of its thumbnails are downloaded again. Does anyone have an example of this type of implementation (gridview adapter within a listview adapter) where the imageview (or images) are stored? What is the proper way to do this?
Thanks in advance for your help.
Gallery Adapter
public class GalleryAdapter extends BaseAdapter {
private Context mContext;
ArrayList<GalleryItem> GalleryList;
//MediaAdapter adapter;
public GalleryAdapter(Context c,ArrayList<GalleryItem> l) {
mContext = c;
GalleryList = l;
}
public int getCount() {
return GalleryList.size();
}
public Object getItem(int position) {
return GalleryList.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
GalleryViewHolder viewHolder = null;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.gallery_item, parent, false);
viewHolder = new GalleryViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
convertView.setTag(viewHolder);
}
else{
viewHolder = (GalleryViewHolder) convertView.getTag();
}
viewHolder.title.setText(GalleryList.get(position).getTitle());
//Formatting the gridView to fit the screen dim.
ImageTools mWidth = new ImageTools(mContext);
viewHolder.mediaGrid.setColumnWidth(mWidth.imageSize());
int rows = (int) Math.ceil((GalleryList.get(position).getMedia().size() / mWidth.columnNumber)+1);
LinearLayout.LayoutParams labelLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, mWidth.imageSize()*rows);
viewHolder.gridHolder.setLayoutParams(labelLayoutParams);
viewHolder.mediaGrid.setLayoutParams(labelLayoutParams);
viewHolder.mediaGrid.setMinimumHeight(mWidth.imageSize()*rows);
//Set Adapter for image views
viewHolder.mediaGrid.setAdapter(new MediaAdapter(convertView.getContext(),GalleryList.get(position).getMedia()));
viewHolder.folder_settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Dialogs.createListDialog(mContext,"Folder Actions", R.array.gallery_action_array).show();
}
});
viewHolder.mediaGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
}
});
return convertView;
}
private class GalleryViewHolder {
private TextView title;
private ArrayList<ImageView> imageList;
private GridView mediaGrid;
private ImageView folder_settings;
private LinearLayout gridHolder;
private int position;
}
}
Media Adapter
public class MediaAdapter extends BaseAdapter {
private Context mContext;
ArrayList<MediaItem> mediaitems;
public MediaAdapter(Context c,ArrayList<MediaItem> l) {
mContext = c;
mediaitems = l;
}
public int getCount() {
return mediaitems.size();
}
public Object getItem(int position) {
return mediaitems.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setImageResource(R.drawable.loading);
imageView.setTag(R.integer.path,mediaitems.get(position).getPath().toString());
imageView.setTag(R.integer.fullsize,"false");
imageView.setTag(R.integer.parentpath,mediaitems.get(position).getParentPath().toString());
imageView.setTag(R.integer.index , String.valueOf(position));
try {
new thumbDownload(mContext).execute(imageView);
} catch (DbxException e) {
e.printStackTrace();
}
ImageTools mWidth = new ImageTools(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(mWidth.imageSize(), mWidth.imageSize()));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
//imageView.setImageBitmap(mediaitems.get(position).getBitmap());
return imageView;
}
}
Try with this..may be its silly way, but its worked for me. Just add a line of code inside your method like this for gallery adapter:
public View getView(int position, View convertView, ViewGroup parent) {
GalleryViewHolder viewHolder = null;
// Add this line.
convertView = null;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.gallery_item, parent, false);
viewHolder = new GalleryViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
convertView.setTag(viewHolder);
}
else{
viewHolder = (GalleryViewHolder) convertView.getTag();
}
// rest of your code
}
You can use StickyGridHeaders to implement your UI and Android-Universal-Image-Loader for flexible asynchronous image loading.
StickyGridHeaders is an Android library that provides a GridView that shows items in sections with headers. By default the section headers stick to the top like the People app in Android 4.x but this can be turned off.
Android-Universal-Image-Loader aims to provide a reusable instrument for asynchronous image loading, caching and displaying.
I ended up Using a HashMap<String,Bitmap> to store the images once they were downloaded. I made the hashMap static in my mediaAdapter so I could add the bitmap from my asynctask when it was downloaded. Then in my media Adapter getView(), I added a if statement to check if the image had already been downloaded. If it had, I used setImageBitmap(myHash.get(key)).

Categories