我正在開發應用程序,其中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;
}
}
我已經編輯了問題的代碼 – Murali