我已经创建了一个网格视图,其中包含一系列图像,现在它有9个图像,但计划进行更大的图像。我试图设置它,所以当我点击一个图像时,它将覆盖该图像与单个图像(红色x)我发现了几个在gridview顶部覆盖图像的例子,但他们似乎都使用另一个阵列。我尝试将我的单个图像设置为数组,但这似乎不起作用。我有onitemclicklistner也工作,我可以让图像显示为第一个图像。正在考虑尝试用x填充整个网格并将其隐藏(不确定这是否是最佳方式)
现在它无法正常工作,因为我正在尝试添加第二张图片。
我的网格适配器
public class EasyGridAdapter extends BaseAdapter {
private int[] easyPics;
private int[] overlayPics; //used for overlay ...this is just created here
Context myContext;
public EasyGridAdapter(Context myContext, int easyPics[], int overlayPics[]){
//sets gridadapter to use pics , if had text under pic would need to add after int[] pics
// name EasyGridAdapter is name ot the java class
this.myContext = myContext;
this.easyPics = easyPics;
this.overlayPics = overlayPics;
}
@Override
public int getCount() {
return easyPics.length;
}
@Override
public Object getItem(int position) {
return easyPics[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid = convertView;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //not sure what for
grid = inflater.inflate(R.layout.custom_easy, null);
}
//main view
ImageView iv = (ImageView) grid.findViewById(R.id.ivPics); //ivPics is android id name of imageviewer in custom easy xml
ImageView ol = (ImageView) grid.findViewById(R.id.overlayImage);
iv.setImageResource(easyPics[position]); //easypics is the interger from above
ol.setImageResource(overlayPics[position]);
return grid; //returns grid which is name of grid on activity_main.xml (with all stuff in it)
}
**my custom xml file**
<ImageView
android:id="@+id/ivPics"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@drawable/e1"
/>
<!--added to overlay image when item clicked.-->
<ImageView
android:id="@+id/overlayImage"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@drawable/x"
android:visibility="gone"
/>
我的主要活动的一部分让我知道您是否需要了解更多
public void onClick(View v) {
switch (v.getId()){
case R.id.btnEasy:
creatArray();
btnEasy.setVisibility(View.INVISIBLE); //hides button on the second activity
btnMed.setVisibility(View.INVISIBLE);
btnHard.setVisibility(View.INVISIBLE);
grid=(GridView)findViewById(R.id.grid);
// EasyGridAdapter adapter = new EasyGridAdapter(MainActivity.this, easyArray);
EasyGridAdapter adapter = new EasyGridAdapter(MainActivity.this, randomPics, overlay); //have to add all images (arrays here)
grid.setAdapter(adapter);
//listens for click on grid
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//sets what to do if each grid is pressed
if(position == 0){
Toast.makeText(MainActivity.this, "clicked 1!", Toast.LENGTH_SHORT).show();
// shows that was clicked. shows x
// ImageView overlay = (ImageView)findViewById(R.id.overlay);
// overlay.setVisibility(View.VISIBLE);
}
if(position == 1){
ImageView overlayImage = (ImageView)findViewById(R.id.overlayImage);
overlayImage.setVisibility(View.VISIBLE);
}
if(position == 2){
ImageView overlay = (ImageView)findViewById(R.id.overlayImage);
overlay.setVisibility(View.VISIBLE);
编辑自定义xml
public class EasyGridAdapter extends BaseAdapter {
private int[] easyPics;
private int[] overlayPic; //used for overlay ...this is just created here
Context myContext;
public EasyGridAdapter(Context myContext, int easyPics[], int overlayPic []){
//sets gridadapter to use pics , if had text under pic would need to add after int[] pics
// name EasyGridAdapter is name ot the java class
this.myContext = myContext;
this.easyPics = easyPics;
this.overlayPic = overlayPic;
}
@Override
public int getCount() {
return easyPics.length;
// return overlayPic.length;
}
@Override
public Object getItem(int position) {
// return easyPics[position]; //wored with just easypics
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid = convertView;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //not sure what for
grid = inflater.inflate(R.layout.custom_easy, null);
}
//main view
ImageView iv = (ImageView) grid.findViewById(R.id.easyPics); //EasyPics is android id name of imageviewer in custom easy xml
ImageView ol = (ImageView) grid.findViewById(R.id.overlayPic);
iv.setImageResource(easyPics[position]); //easypics is the interger from above
ol.setImageResource(overlayPic[position]);
return grid; //returns grid which is name of grid on activity_main.xml (with all stuff in it)
}
答案 0 :(得分:1)
1)从onItemClickListener
删除/评论 GridView
:
2)我假设您使用RelativeLayout
作为custom_easy.xml
的父级。 修改 getView()
方法如下:
public class Holder
{
RelativeLayout rlParent;
ImageView iv;
ImageView ivOverlay;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid = convertView;
final Holder holder=new Holder();
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //not sure what for
grid = inflater.inflate(R.layout.custom_easy, null);
}
//main view
holder.rlParent = (RelativeLayout) grid.findViewById(R.id.yourParentLayout);
holder.iv = (ImageView) grid.findViewById(R.id.ivPics); //ivPics is android id name of imageviewer in custom easy xml
holder.ol = (ImageView) grid.findViewById(R.id.overlayImage);
holder.iv.setImageResource(easyPics[position]); //easypics is the interger from above
holder.relParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.ol.setVisibility(View.VISIBLE);
}
});
return grid; //returns grid which is name of grid on activity_main.xml (with all stuff in it)
}
希望这有帮助。