Der Versuch, eine Video-Galerie mit horizontalen scrollview Kinder in einer erweiterbaren Listview zu implementieren.Videogalerie mit erweiterbaren Listenansicht übergeordneten horizontalen scrollview Kind
Problembehandlung - Anstatt alle n Anzahl der untergeordneten Elemente der Gruppe in der horizontalen Listenansicht anzuzeigen, wird das Duplikat des ersten untergeordneten Objekts n-mal horizontal erstellt.
Hier ist mein Code
Diese Aktivität, die in einem bestimmten Ordner (Fotografie) in SD-Karte gespeicherten Videos abruft. Checks für Videos innerhalb Unterordner (hier Kran, HeliCam, Montag, multi) und setzt in verschiedenen Kategorien in expandablelistview Gruppen
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
{
for (File file : directory.listFiles())
{
if (file.isFile())
mDeviceFileListApp.add(file.getName());
}
}
if(directory1.length()!=0) // check no of files
{
for (File file : directory1.listFiles())
{
if (file.isFile())
mDeviceFileListApp.add(file.getName());
}
}
if(directory2.length()!=0) // check no of files
{
for (File file : directory2.listFiles())
{
if (file.isFile())
mDeviceFileListApp.add(file.getName());
}
}
if(directory3.length()!=0) // check no of files
{
for (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>>();
for (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>();
for (String model : laptopModels)
childList.add(model);
}
private void setGroupIndicatorToRight() {
/* Get the screen width */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
expListView.setIndicatorBounds(width - getDipsFromPixel(35), width - getDipsFromPixel(5));
}
// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
// get files by 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;
}
Der expandablelistview Adapter für Eltern
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 für den horizontalen 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 Your code here what functionality you 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 is ", 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);
}
}
}
Die xML-Datei für 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>
wurde das Problem behoben 2 d Klicken Kind - bearbeitet und die HorizontalListAdapter ebenfalls aktualisiert. Jetzt ist nur Problem 1 übrig, d. H. Replizierendes Kind. Gegenwärtig OutOfBoundException beim Einführen von childPosition ++ in getView in HorizontalListAdapter. – Shifu
Ich denke, dass ich in HorizontalListAdapter die Kinderposition erhöhen muss, bis es das letzte Element der Gruppe erreicht. Aber irgendwie zeigt es OutOfBoundException. Kann mir jemand sagen, was ich tun kann, um das zu vermeiden? – Shifu