如何在Gridview Android上叠加播放图标?

时间:2017-04-24 13:25:54

标签: java android gridview

我希望帮助在每个网格项的顶部覆盖一个播放图标,如果它包含视频。这是我的GridView布局文件。我试图做,但我无法连接所有东西。

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;
}
}

1 个答案:

答案 0 :(得分:0)

1。为您的GridView layout创建Item 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;
}

<强>输出:

enter image description here

希望这会有所帮助〜