android - Video gallery with expandablelistview parent horizontal scrollview child -
trying implement video gallery horizontal scrollview children inside expandablelistview.
problem facing - instead of showing n number of children of group in horizontal list view, duplicate of first child created n times horizontally.
here code
this activity fetches videos saved in particular folder (photography) in sd-card. checks videos inside subfolders(here crane,helicam,montage,multi) , put in different categories in expandablelistview groups
public class expandablelistactivity extends activity { list<string> grouplist; list<string> childlist; map<string, list<string>> collection; expandablelistview explistview; expandablelistadapters explistadapter; public static final string doc_folder_name = "/photography/"; //folder public static final string doc_folder_name_one = "/crane/"; public static final string doc_folder_name_two = "/helicam/"; public static final string doc_folder_name_three = "/montage/"; public static final string doc_folder_name_foure = "/multi camera output/"; private static final string tag="om"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.new_layout); this.setrequestedorientation(activityinfo.screen_orientation_landscape); final string sdcard = environment.getexternalstoragedirectory().tostring() +doc_folder_name; if(getdevicefiles().size() > 0) { createcollection(); explistview = (expandablelistview) findviewbyid(r.id.laptop_list); final expandablelistadapters explistadapter = new expandablelistadapters(this, collection, grouplist); explistview.setadapter(explistadapter); // setgroupindicatortoright(); explistview.setongroupexpandlistener(new expandablelistview.ongroupexpandlistener() { int previousitem = 0; @override public void ongroupexpand(int groupposition) { if (groupposition != previousitem) explistview.collapsegroup(previousitem); previousitem = groupposition; } }); } else { toast.maketext(getapplicationcontext(), "videos not found", toast.length_short).show(); intent intent = new intent(this, mainactivity.class); startactivity(intent); } settitle("videos"); } public arraylist<string> getdevicefiles() { arraylist<string> mdevicefilelistapp = new arraylist<string>(); file directory = new file(environment.getexternalstoragedirectory()+doc_folder_name+doc_folder_name_one); file directory1 = new file(environment.getexternalstoragedirectory()+doc_folder_name+doc_folder_name_two); file directory2= new file(environment.getexternalstoragedirectory()+doc_folder_name+doc_folder_name_three); file directory3 = new file(environment.getexternalstoragedirectory() + doc_folder_name+doc_folder_name_foure); if(directory.length()!=0) // check no of files { (file file : directory.listfiles()) { if (file.isfile()) mdevicefilelistapp.add(file.getname()); } } if(directory1.length()!=0) // check no of files { (file file : directory1.listfiles()) { if (file.isfile()) mdevicefilelistapp.add(file.getname()); } } if(directory2.length()!=0) // check no of files { (file file : directory2.listfiles()) { if (file.isfile()) mdevicefilelistapp.add(file.getname()); } } if(directory3.length()!=0) // check no of files { (file file : directory3.listfiles()) { if (file.isfile()) mdevicefilelistapp.add(file.getname()); } } return mdevicefilelistapp; } public void createcollection() { grouplist = new arraylist<string>(); grouplist.add("crane"); grouplist.add("helicam"); grouplist.add("montage"); grouplist.add("multi output"); collection = new linkedhashmap<string, list<string>>(); (string laptop : grouplist) { arraylist<string> child = new arraylist<string>(); if (laptop.equals("crane")) { arraylist<string> filelist = getfiles(environment.getexternalstoragedirectory()+"/"+doc_folder_name+"/"+doc_folder_name_one,"mp4"); for(string filename : filelist) child.add(filename); loadchild(child); } else if (laptop.equals("helicam")){ arraylist<string> filelist = getfiles(environment.getexternalstoragedirectory()+"/"+doc_folder_name+"/"+doc_folder_name_two,"mp4"); for(string filename : filelist) child.add(filename); loadchild(child); } else if (laptop.equals("montage")){ arraylist<string> filelist = getfiles(environment.getexternalstoragedirectory()+"/"+doc_folder_name+"/"+doc_folder_name_three,"mp4"); for(string filename : filelist) child.add(filename); loadchild(child); } else if (laptop.equals("multi output")){ arraylist<string> filelist = getfiles(environment.getexternalstoragedirectory()+"/"+doc_folder_name+"/"+doc_folder_name_foure,"mp4"); for(string filename : filelist) child.add(filename); loadchild(child); } collection.put(laptop, childlist); } } private void loadchild(arraylist<string> laptopmodels) { childlist = new arraylist<string>(); (string model : laptopmodels) childlist.add(model); } private void setgroupindicatortoright() { /* screen width */ displaymetrics dm = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(dm); int width = dm.widthpixels; explistview.setindicatorbounds(width - getdipsfrompixel(35), width - getdipsfrompixel(5)); } // convert pixel dip public int getdipsfrompixel(float pixels) { // screen's density scale final float scale = getresources().getdisplaymetrics().density; // convert dps pixels, based on density scale return (int) (pixels * scale + 0.5f); } // files type private arraylist<string> getfiles(string path,string filetype) { arraylist<string> tempfilelist=new arraylist<string>(); file file=new file(path); file[] allfiles=file.listfiles(); if(allfiles.length==0) { return null; } else { for(int i=0;i<allfiles.length;i++) { if(filetype.equals("mp4") && allfiles[i].getname().endswith(".mp4")) tempfilelist.add(allfiles[i].getname()); } } return tempfilelist; }
the expandablelistview adapter parent
public class expandablelistadapters extends baseexpandablelistadapter { private activity context; private map<string, list<string>> laptopcollections; private list<string> laptops; public expandablelistadapters(activity context, map<string, list<string>> laptopcollections, list<string> laptops) { this.context = context; this.laptopcollections = laptopcollections; this.laptops = laptops; } public object getchild(int groupposition, int childposition) { return laptopcollections.get(laptops.get(groupposition)).get(childposition); } public long getchildid(int groupposition, int childposition) { return childposition; } public view getchildview(final int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { final string filename = (string) getchild(groupposition, childposition); childholder childholder = null; if (convertview == null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.group_child_item, null, false); childholder = new childholder(); convertview.settag(childholder); } else { childholder = (childholder) convertview.gettag(); } childholder.horizontallistview = (hlistview) convertview.findviewbyid(r.id.horizontallistview); horizontallistadapter horizontallistadapter = new horizontallistadapter(context, laptopcollections, laptops , groupposition ,childposition); childholder.horizontallistview.setadapter(horizontallistadapter); return convertview; } public int getchildrencount(int groupposition) { return 1; } public object getgroup(int groupposition) { return laptops.get(groupposition); } public int getgroupcount() { return laptops.size(); } public long getgroupid(int groupposition) { return groupposition; } public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { string laptopname = (string) getgroup(groupposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.item_parent,null); } textview item = (textview) convertview.findviewbyid(r.id.laptop); item.settypeface(null, typeface.bold); item.settext(laptopname); imageview indicator = (imageview) convertview.findviewbyid(r.id.image_indicator); if(isexpanded) { indicator.setimageresource(r.drawable.axtivetabicon1); item.settextcolor(context.getresources().getcolor(r.color.codegreen)); } else { indicator.setimageresource(r.drawable.tabicon1); item.settextcolor(context.getresources().getcolor(r.color.codeblack)); } return convertview; } public boolean hasstableids() { return true; } public boolean ischildselectable(int groupposition, int childposition) { return true; } private static class childholder { hlistview horizontallistview; }
adapter horizontal scroll childview
public class horizontallistadapter extends baseadapter{ public static final string doc_folder_name = "/photography/"; // folder public static final string doc_folder_name_one = "/crane/"; public static final string doc_folder_name_two = "/helicam/"; public static final string doc_folder_name_three = "/montage/"; public static final string doc_folder_name_foure = "/multi camera output/"; private context mcontext; private map<string, list<string>> laptopcollections; private list<string> laptops; private int groupposition; private int childposition; package com.evolved.ayshir.picdrift; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import com.bumptech.glide.glide; import java.io.file; import java.util.list; import java.util.map; public class horizontallistadapter extends baseadapter { public static final string doc_folder_name = "/photography/"; //folder public static final string doc_folder_name_one = "/crane/"; public static final string doc_folder_name_two = "/helicam/"; public static final string doc_folder_name_three = "/montage/"; public static final string doc_folder_name_foure = "/multi camera output/"; private context mcontext; private map<string, list<string>> laptopcollections; private list<string> laptops; private int groupposition; private int childposition; public horizontallistadapter(context context, map<string, list<string>> laptopcollections, list<string> laptops, int groupposition, int childposition) { this.mcontext = context; this.laptopcollections = laptopcollections; this.laptops = laptops; this.groupposition = groupposition; this.childposition = childposition; } @override public object getitem(int childposition) { return laptopcollections.get(laptops.get(groupposition)).get(childposition); } @override public int getcount() { return laptopcollections.get(laptops.get(groupposition)).size(); } public object getchild(int groupposition, int childposition) { return laptopcollections.get(laptops.get(groupposition)).get(childposition); } @override public long getitemid(int childposition) { return childposition++; } @override public view getview( int position, view convertview, viewgroup parent) { viewholder viewholder; if (convertview == null) { layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.child_item, null, false); viewholder = new viewholder(convertview); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } // implement code here functionality need. textview item = (textview) convertview.findviewbyid(r.id.laptop); { final string filename = (string) getchild(groupposition, childposition); item.settext(filename); imageview imagethumbnail = (imageview) convertview.findviewbyid(r.id.childimage); if (groupposition == 0) { string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_one + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); } else if (groupposition == 1) { string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_two + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); } else if (groupposition == 2) { string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_three + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); } else if (groupposition == 3) { string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_foure + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); } } convertview.setonclicklistener(new onclicklistener() { public void onclick(view convertview) { final string filename = (string) getchild(groupposition, childposition); string filenamearray[] = filename.split("\\."); string fileextension = filenamearray[filenamearray.length - 1]; string filetype = ""; if (fileextension.equals("mp4")) filetype = "video/mpeg"; if (filetype != "") { if (groupposition == 0) { string sd = environment.getexternalstoragedirectory() + doc_folder_name + doc_folder_name_one + filename; file productfilename = new file(environment.getexternalstoragedirectory() + doc_folder_name + doc_folder_name_one + filename); log.i("file ", filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 1) { file productfilename0 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_two + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename0), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 2) { file productfilename2 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_three + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename2), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 3) { file productfilename3 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_foure + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename3), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } } } }); return convertview; } private static class viewholder { imageview thumbnail; textview modelname; public viewholder(view itemview) { thumbnail = (imageview) itemview.findviewbyid(r.id.childimage); modelname = (textview) itemview.findviewbyid(r.id.laptop); } }
}
the xml file item_parent.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#99ffffff" android:gravity="center_vertical" android:padding="5dp"> <textview android:id="@+id/laptop" android:textsize="18dp" android:text="text" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textstyle="bold" /> <imageview android:id="@+id/image_indicator" android:src="@drawable/tabicon1" android:layout_width="15dp" android:layout_height="15dp" android:scaletype="fitcenter" android:layout_margin="5dp" /> </linearlayout>
child_item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:padding="10dp" android:background="#66ffffff" android:gravity="center" > <framelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <imageview android:id="@+id/childimage" android:layout_width="180dp" android:layout_height="120dp" android:layout_gravity="center" android:padding="1dp" android:scaletype="fitcenter" android:croptopadding="true" android:background="@drawable/border_image" /> <imageview android:id="@+id/play" android:src="@drawable/playiconnew" android:layout_width="35dp" android:layout_height="wrap_content" android:layout_gravity="center" android:scaletype="fitcenter" /> </framelayout> <textview android:id="@+id/laptop" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/childimage" android:layout_centervertical="true" android:paddingleft="15dp" android:text="demo text" android:layout_gravity="center" android:gravity="center" /> </linearlayout>
group_item_child.xml
<it.sephiroth.android.library.widget.hlistview android:id="@+id/horizontallistview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/child" android:padding="20dp" xmlns:android="http://schemas.android.com/apk/res/android">
new_layout.xml`
<linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight=".1" > <expandablelistview android:id="@+id/laptop_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:divider="@drawable/divider" android:groupindicator="@null" /> </linearlayout>
new getview horizontallistadapter. takes care both outofboundexception , displays children of group correctly.i found working correctly.
@override public view getview( int position, view convertview, viewgroup parent) { viewholder viewholder; if (convertview == null) { layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.child_item, null, false); viewholder = new viewholder(convertview); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } // implement code here functionality need. textview item = (textview) convertview.findviewbyid(r.id.laptop); if(childposition<getcount()){ log.i(tag, "child position " + childposition); log.i(tag, "size " + getcount()); final string filename = (string) getchild(groupposition, childposition); item.settext(filename); imageview imagethumbnail = (imageview)convertview.findviewbyid(r.id.childimage); if(groupposition == 0 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name +"/"+ doc_folder_name_one + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); }else if(groupposition == 1 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_two + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); }else if(groupposition == 2 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_three + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); }else if(groupposition == 3 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_foure + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); } convertview.setonclicklistener(new view.onclicklistener() { public void onclick(view convertview) { string filenamearray[] = filename.split("\\."); string fileextension = filenamearray[filenamearray.length - 1]; string filetype = ""; if (fileextension.equals("mp4")) filetype = "video/mpeg"; if (filetype != "") { if (groupposition == 0) { string sd = environment.getexternalstoragedirectory() + doc_folder_name + doc_folder_name_one + filename; file productfilename = new file(environment.getexternalstoragedirectory() + doc_folder_name + doc_folder_name_one + filename); log.i("file ", filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 1) { file productfilename0 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_two + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename0), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 2) { file productfilename2 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_three + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename2), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 3) { file productfilename3 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_foure + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename3), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } } } }); } else if(childposition==getcount()) try{ final string filename = (string) getchild(groupposition, 0); item.settext(filename); imageview imagethumbnail = (imageview)convertview.findviewbyid(r.id.childimage); if(groupposition == 0 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name +"/"+ doc_folder_name_one + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); }else if(groupposition == 1 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_two + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); }else if(groupposition == 2 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_three + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); }else if(groupposition == 3 ){ string filepath = environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_foure + "/" + filename; glide.with(convertview.getcontext()) .load(uri.fromfile(new file(filepath))) // or uri/path .into(imagethumbnail); } convertview.setonclicklistener(new view.onclicklistener() { public void onclick(view convertview) { string filenamearray[] = filename.split("\\."); string fileextension = filenamearray[filenamearray.length - 1]; string filetype = ""; if (fileextension.equals("mp4")) filetype = "video/mpeg"; if (filetype != "") { if (groupposition == 0) { string sd = environment.getexternalstoragedirectory() + doc_folder_name + doc_folder_name_one + filename; file productfilename = new file(environment.getexternalstoragedirectory() + doc_folder_name + doc_folder_name_one + filename); log.i("file ", filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 1) { file productfilename0 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_two + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename0), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 2) { file productfilename2 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_three + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename2), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } else if (groupposition == 3) { file productfilename3 = new file(environment.getexternalstoragedirectory() + "/" + doc_folder_name + "/" + doc_folder_name_foure + "/" + filename); intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(productfilename3), filetype); intent.setflags(intent.flag_activity_no_history); mcontext.startactivity(intent); } } } }); } catch( indexoutofboundsexception e){ log.d(tag, "caught");} childposition++; return convertview; } private static class viewholder { imageview thumbnail; textview modelname; public viewholder(view itemview) { thumbnail = (imageview) itemview.findviewbyid(r.id.childimage); modelname = (textview) itemview.findviewbyid(r.id.laptop); } }
}
Comments
Post a Comment