2017-04-24 32 views
0

我想要幫助覆蓋每個網格項頂部的播放圖標,如果它包含video.Here是我的GridView的佈局文件。我正在嘗試做,但我無法連接所有的東西。如何在Gridview Android上覆蓋播放圖標?

PS: - 我是新手

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="2dp" 
tools:context="com.techx.storysaver.ImageFragment"> 

<GridView 
    android:id="@+id/grid_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:horizontalSpacing="2dp" 
    android:listSelector="@drawable/griditem_selector" 
    android:numColumns="2" 
    android:drawSelectorOnTop="true" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="2dp" /> 
</FrameLayout> 

這裏是我的ImageAdapter

public class ImageAdapter extends BaseAdapter { 

private Context context; 

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures"; 
File f = new File(path); 
File file[] = f.listFiles(); 

public ImageAdapter(Context c) { 
    context = c; 
} 

@Override 
public void notifyDataSetChanged() { 
    super.notifyDataSetChanged(); 
} 

@Override 
public int getCount() { 
    return file.length; 
} 

@Override 
public Object getItem(int position) { 
    return file[position]; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Arrays.sort(file, LastModifiedFileComparator.LASTMODIFIED_REVERSE); 
    final String path = file[position].getAbsolutePath(); 
    CheckableLayout l; 
    ImageView i; 

    if (convertView == null) { 

     i = new ImageView(context); 
     i.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     int widthSize = getScreenWidth(); 
     widthSize = widthSize/2; 
     widthSize = widthSize - 8; 

     int heightSize = getScreenHeight(); 
     heightSize = heightSize/3; 
     heightSize = heightSize - 10; 

     i.setLayoutParams(new ViewGroup.LayoutParams(widthSize, heightSize)); 
     i.setPadding(8, 8, 8, 8); 

     l = new CheckableLayout(context); 
     l.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT)); 
     l.addView(i); 
    } else { 
     l = (CheckableLayout) convertView; 
     i = (ImageView) l.getChildAt(0); 
    } 
    if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")) { 
     Glide 
       .with(context) 
       .load(file[position]) 
       .into(i); 

    } 
    if (path.endsWith(".mp4")) { 

     Glide 
       .with(context) 
       .load(file[position]) 
       .into(i); 
    } 

    return l; 

} 

public class CheckableLayout extends FrameLayout implements Checkable { 
    private boolean mChecked; 

    public CheckableLayout(Context context) { 
     super(context); 
    } 

    @SuppressWarnings("deprecation") 
    public void setChecked(boolean checked) { 
     mChecked = checked; 
     setBackgroundDrawable(checked ? getResources().getDrawable(R.drawable.item_selector) : null); 
    } 

    public boolean isChecked() { 
     return mChecked; 
    } 

    public void toggle() { 
     setChecked(!mChecked); 
    } 
} 

public static int getScreenWidth() { 
    return Resources.getSystem().getDisplayMetrics().widthPixels; 
} 

public static int getScreenHeight() { 
    return Resources.getSystem().getDisplayMetrics().heightPixels; 
} 
} 
+0

爲什麼這樣做編程,只是在電網項目視圖 –

+0

圖標添加你的XML嘗試或者你不想使用XML? – FAT

+0

嗨,我無法弄清楚如何添加在網格視圖頂部的XML。你能幫我嗎? –

回答

0

爲你的GridView Item創建layout XML。

grid_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/tools" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    card_view:cardCornerRadius="4dp" 
    card_view:cardUseCompatPadding="false" 
    card_view:cardPreventCornerOverlap="false"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:layout_alignParentTop="true" 
      android:scaleType="centerCrop" 
      android:src="@drawable/sample_1" /> 

     <ImageView 
      android:id="@+id/icon_play" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:src="@drawable/icon_play"/> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/image" 
      android:padding="8dp" 
      android:background="#CCFFFFFF" 
      android:text="This is simple title" 
      android:textColor="#000000" /> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 

2.使用grid_item.xml從適配器getView()方法對每個item

@Override 
public View getView(int pos, View convertView, ViewGroup parent) 
{ 
    Arrays.sort(file, LastModifiedFileComparator.LASTMODIFIED_REVERSE); 
    final String path = file[position].getAbsolutePath(); 

    if(convertView == null) 
    { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.grid_item, null); 
    } 

    ImageView imageView = (ImageView) convertView.findViewById(R.id.image); 
    ImageView iconPlay= (ImageView) convertView.findViewById(R.id.icon_play); 
    TextView textTitle= (TextView) convertView.findViewById(R.id.title); 

    // Image 
    if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")) { 
     Glide 
      .with(context) 
      .load(file[position]) 
      .into(imageView); 

     // Hide play icon 
     iconPlay.setVisibility(View.GONE); 
    } 

    // Video 
    if (path.endsWith(".mp4")) { 
     Glide 
      .with(context) 
      .load(file[position]) 
      .into(imageView); 

     // Show play icon 
     iconPlay.setVisibility(View.VISIBLE); 
    } 

    return convertView; 
} 

OUTPUT:

enter image description here

希望這將有助於〜

+0

嗨,感謝您的幫助。但我在裏面使用了框架佈局和gridview。我沒有使用cardview –

+0

您必須爲您的網格項目使用CardView。您已經創建了一個用於在GridView上填充網格項目的適配器。閱讀我的完整答案。希望你能得到這個。 – FAT