2017-04-15 173 views
-1

我正在開發應用程序,其中Imageview嵌套在用於創建Gridview的Linearlayout中。GridView - Onclick監聽器

當用戶單擊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; 
} 


} 
+0

我已經編輯了問題的代碼 – Murali

回答

1

@我想獲取圖像視圖,如果一個imageview(內Gridview)是 點擊。下面在setOnClickListener方法中,對象v 指向線性佈局。如何獲取嵌入 內的ImageView,當它被點擊時?

試試下面的代碼來設置clickListenerImageView

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方法解釋一下嗎?

每當您的GridView創建一行時,都會調用getView()方法。 Here你會發現一個關於這個問題的精美解釋的答案。

希望這會有所幫助。

+1

線 - 持有人=(持有人)rowView。 getTag();彈出 錯誤值不能分配給最終變量「持有人」 – Murali

+0

感謝tahsinRupam,我越來越頭暈,不理解此代碼。 – Murali

+0

這裏我想單擊一個圖像視圖,然後單擊第二個imageview,如果條件沒問題,那麼我用imageview1s的圖像替換imageview2的圖像。清除imageview1的圖像,你可以請求幫助 – Murali

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" />

+0

好的我知道了...... – Murali

+0

當我嘗試僅在square.xml中放置imageview時,它顯示爲紅色 – Murali