如何使用Myadapter

时间:2016-09-16 08:15:55

标签: android android-custom-view

当我使用Myadapter点击动态创建的imageview时,我希望使用我的函数showImage(最终的Uri imageUri)将图像显示为全屏,有人会提供解决方案。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mnt);
    GridView gridView = (GridView)findViewById(R.id.gridview);
    gridView.setAdapter(new MyAdapter(this));
}

全屏显示图像的功能

 public void showImage(final Uri imageUri) {
    Dialog builder = new Dialog(Mnt.this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    ImageView imageView = new ImageView(Mnt.this);
    imageView.setImageURI(imageUri);
    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    builder.show();
}

MyAdapter.java

public final class MyAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;

public MyAdapter(Context context) {
    mInflater = LayoutInflater.from(context);

    mItems.add(new Item("1",       R.drawable.m1));
    mItems.add(new Item("2",   R.drawable.m2));
    mItems.add(new Item("3", R.drawable.m3));
    mItems.add(new Item("4",      R.drawable.m4));
    mItems.add(new Item("5",     R.drawable.m1));
    mItems.add(new Item("6",      R.drawable.m2));
}

@Override
public int getCount() {
    return mItems.size();
}

@Override
public Item getItem(int i) {
    return mItems.get(i);
}

@Override
public long getItemId(int i) {
    return mItems.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    TextView name;

    if (v == null) {
        v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        v.setTag(R.id.text, v.findViewById(R.id.text));
    }

    picture = (ImageView) v.getTag(R.id.picture);
    name = (TextView) v.getTag(R.id.text);

    Item item = getItem(i);

    picture.setImageResource(item.drawableId);
    name.setText(item.name);

    return v;
}

private static class Item {
    public final String name;
    public final int drawableId;

    Item(String name, int drawableId) {
        this.name = name;
        this.drawableId = drawableId;
    }
}

activity_mnt.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:stretchMode="columnWidth"
        android:numColumns="2"/>

</FrameLayout>

SquareImageView.java

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}}

4 个答案:

答案 0 :(得分:0)

ViewGroup.LayoutParams.MATCH_PARENT 而不是 ViewGroup.LayoutParams.WRAP_CONTENT 或者你没有给imageview提供布局参数,这就是为什么它只占用屏幕中图像的大小。 试试这个,让我知道。

答案 1 :(得分:0)

为图像视图设置LayoutParam。

AVFullScreenViewController

问题很模糊。但我认为你的意思是这个。尝试一下,告诉我是否有事情发生了。

首先,保存上下文

LayoutParams param = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

imageView.setLayoutParams(param);

然后在你的图片上设置setTag(drawableId)并点击一下。

public MyAdapter(Context context) {
    this.context = context;
    ...
} 

希望有所帮助。

答案 2 :(得分:0)

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //call your showImage(final Uri imageUri) function with position/id
        }
    });

答案 3 :(得分:0)

修改showImage方法以传递整数资源ID。

试试这个,

public void showImage(final int imageId) {
    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    ImageView imageView = new ImageView(this);
    imageView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), imageId, getTheme()));
    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    builder.show();
}

不是在适配器内创建数据集,而是在Activity中创建它。在onCreate初始化数据集,如下所示。

private final List<Item> mItems = new ArrayList<Item>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mnt);

    mItems.add(new Item("1", R.drawable.m1));
    mItems.add(new Item("2", R.drawable.m2));
    mItems.add(new Item("3", R.drawable.m3));
    mItems.add(new Item("4", R.drawable.m4));
    mItems.add(new Item("5", R.drawable.m1));
    mItems.add(new Item("6", R.drawable.m2));

    GridView gridView = (GridView)findViewById(R.id.gridview);
    gridView.setAdapter(new MyAdapter(this, mItems));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Get your image id from your Item object
            int imageId = mItems.get(position).getDrawable();
            showImage(imageId);
        }

    });
}

还要修改适配器的构造函数,如下所示

public MyAdapter(Context context, List<Item> dataset) {
    mInflater = LayoutInflater.from(context);
    this.mItems = dataset;
}