GridView - Onclick监听器

时间:2017-04-15 08:01:53

标签: java android android-imageview android-adapter

我在app上工作,其中Imageview嵌套在Linearlayout中,用于创建Gridview。

当用户单击imageview 1然后再单击imageview 2时,如果所有条件都正常,我将imageview 1的图像更改为imageview 2。怎么做?

以下代码供您参考

activity_main.xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"    >
<GridView
    android:id="@+id/gridView1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_alignParentLeft="true"
    android:numColumns="8"
    android:padding="2dp"
    android:background="@drawable/chessboard"
    >

</GridView>

square.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
   <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/imageView1"
        android:layout_gravity="center"
       />

   </LinearLayout>

CustomAdapter.java

  package com.example.android.chess;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.Toast;

  public class CustomAdapter extends BaseAdapter{

  int [] result;
  Context context;
  int [] imageId;
  private static LayoutInflater inflater=null;
  public CustomAdapter(MainActivity mainActivity,int[] prgmImages) {
    // TODO Auto-generated constructor stub
    result=prgmImages;
    context=mainActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    //TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;

   rowView = inflater.inflate(R.layout.square, null);
   //holder.img=(ImageView) rowView.findViewById(R.id.ImagesId);
   holder.img=(ImageView) rowView.findViewById(R.id.imageView1);


   // holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Toast.makeText(context, "You Clicked "+ v.getBackground(), Toast.LENGTH_LONG).show();

            Toast.makeText(context, "You Clicked "+v.getLayoutParams()., Toast.LENGTH_LONG).show();
            // TODO Auto-generated method stub
            //Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });

    return rowView;
}


}

2 个答案:

答案 0 :(得分:1)

  

@I想要获得Imageview,如果是一个imageview(在Gridview内)   点击。在下面的setOnClickListener方法中,对象为v   指向线性布局。如何将ImageView嵌套在里面   什么时候点击它?

尝试使用以下代码将clickListener设置为ImageView

private ViewHolder holder;

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    holder = new Holder();
    View rowView;

    if(rowView==null)
    {
      rowView = inflater.inflate(R.layout.square, null);
      //holder.img=(ImageView) rowView.findViewById(R.id.ImagesId);
      holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
      rowView.setTag(holder);
     }
    else {
      holder = (Holder)rowView.getTag();
    }
   // holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    holder.img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(context, "You Clicked the imageView", Toast.LENGTH_LONG).show();
        }
    });
}
  

@Also,你能解释一下getView方法吗?

每次为getView()创建一行时,都会调用方法GridViewHere你会找到关于这个问题的精美解释。

希望这有帮助。

答案 1 :(得分:1)

在只有一个元素的地方你不需要LinearLayout。只是ImageView会做。如果您使用的是ListView,请使用setOnItemSelectedListener

更新#2
这就是你的square.xml看起来像的样子     <?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" />