如何让android的GridView中显示的元素只选择一行中的某一个而改变资源文件。
这是GridView的Adapter代码packagecom.lightsoft.lottery;importandroid.app.Activity;publiccla...
这是GridView的Adapter代码
package com.lightsoft.lottery;
import android.app.Activity;
public class LotteryConstantlyPageFiveStarsNumbersListAdapter extends BaseAdapter {
Activity FiveStarsNumbers;
int nNumFiveStars;
int nNum;
int nFiveStars[];
// int nLineNum;//行数
LotteryConstantlyPageFiveStarsNumbersListAdapter(Activity ListAdapter, int NumFiveStars){
FiveStarsNumbers = ListAdapter;
nNumFiveStars = NumFiveStars;
}
public int getCount() {
// TODO Auto-generated method stub
return nNumFiveStars;
}
public Object getItem(int postion) {
// TODO Auto-generated method stub
return postion;
}
public long getItemId(int postion) {
// TODO Auto-generated method stub
return postion;
}
/**
*
* @param
* @par 加载子图层lottery_constantly_page_fivestart_sublayout并设置关闭事件
* @param position
* @param convertView
* @param parent
* @return
* @return
* @retval
* @note 备注
*
* @par 示例:
* @code
*
* @endcode
* @see -
* @deprecate -
* @Exception
* @since Ver 1.0
*/
public View getView(int postion, View arg1, ViewGroup arg2 ) {
// TODO Auto-generated method stub
ImageView imgFiveStarsNumbers = new ImageView(FiveStarsNumbers);
int nResId;
nNum = postion % 10;
// nLineNum = postion / 10;
String strImgFileName;
strImgFileName = "lottery_constantly_page_green" + nNum;
nResId = FiveStarsNumbers.getResources().getIdentifier(strImgFileName, "drawable", "com.lightsoft.lottery");
nResId = FiveStarsNumbers.getResources().getIdentifier(strImgFileName, "drawable", "com.lightsoft.lottery");
imgFiveStarsNumbers.setImageResource(nResId);
imgFiveStarsNumbers.setTag("GREEN," + nNum);
imgFiveStarsNumbers.setOnClickListener(m_OnClickListener);
return imgFiveStarsNumbers;
}
private ImageView.OnClickListener m_OnClickListener = new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
ImageView imgFiveStarsNumbersView = (ImageView)arg0;
String strFiveStarsNumbersTag = (String)imgFiveStarsNumbersView.getTag();
String strNumber;
int nResId;
String strImgFileName = "";
/**
* 获取按钮位置
*/
strNumber = strFiveStarsNumbersTag.split(",")[1];
/**
* 获取按钮颜色
*/
strFiveStarsNumbersTag = strFiveStarsNumbersTag.split(",")[0];
/**
* 交换按钮图片
*/
if(true == strFiveStarsNumbersTag.equals("GREEN"))
{
strImgFileName = "lottery_constantly_page_red" + strNumber;
nResId = FiveStarsNumbers.getResources().getIdentifier(strImgFileName, "drawable",
"com.lightsoft.lottery");
imgFiveStarsNumbersView.setImageResource(nResId);
imgFiveStarsNumbersView.setTag("RED,"+strNumber);
}
}
};
} 展开
package com.lightsoft.lottery;
import android.app.Activity;
public class LotteryConstantlyPageFiveStarsNumbersListAdapter extends BaseAdapter {
Activity FiveStarsNumbers;
int nNumFiveStars;
int nNum;
int nFiveStars[];
// int nLineNum;//行数
LotteryConstantlyPageFiveStarsNumbersListAdapter(Activity ListAdapter, int NumFiveStars){
FiveStarsNumbers = ListAdapter;
nNumFiveStars = NumFiveStars;
}
public int getCount() {
// TODO Auto-generated method stub
return nNumFiveStars;
}
public Object getItem(int postion) {
// TODO Auto-generated method stub
return postion;
}
public long getItemId(int postion) {
// TODO Auto-generated method stub
return postion;
}
/**
*
* @param
* @par 加载子图层lottery_constantly_page_fivestart_sublayout并设置关闭事件
* @param position
* @param convertView
* @param parent
* @return
* @return
* @retval
* @note 备注
*
* @par 示例:
* @code
*
* @endcode
* @see -
* @deprecate -
* @Exception
* @since Ver 1.0
*/
public View getView(int postion, View arg1, ViewGroup arg2 ) {
// TODO Auto-generated method stub
ImageView imgFiveStarsNumbers = new ImageView(FiveStarsNumbers);
int nResId;
nNum = postion % 10;
// nLineNum = postion / 10;
String strImgFileName;
strImgFileName = "lottery_constantly_page_green" + nNum;
nResId = FiveStarsNumbers.getResources().getIdentifier(strImgFileName, "drawable", "com.lightsoft.lottery");
nResId = FiveStarsNumbers.getResources().getIdentifier(strImgFileName, "drawable", "com.lightsoft.lottery");
imgFiveStarsNumbers.setImageResource(nResId);
imgFiveStarsNumbers.setTag("GREEN," + nNum);
imgFiveStarsNumbers.setOnClickListener(m_OnClickListener);
return imgFiveStarsNumbers;
}
private ImageView.OnClickListener m_OnClickListener = new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
ImageView imgFiveStarsNumbersView = (ImageView)arg0;
String strFiveStarsNumbersTag = (String)imgFiveStarsNumbersView.getTag();
String strNumber;
int nResId;
String strImgFileName = "";
/**
* 获取按钮位置
*/
strNumber = strFiveStarsNumbersTag.split(",")[1];
/**
* 获取按钮颜色
*/
strFiveStarsNumbersTag = strFiveStarsNumbersTag.split(",")[0];
/**
* 交换按钮图片
*/
if(true == strFiveStarsNumbersTag.equals("GREEN"))
{
strImgFileName = "lottery_constantly_page_red" + strNumber;
nResId = FiveStarsNumbers.getResources().getIdentifier(strImgFileName, "drawable",
"com.lightsoft.lottery");
imgFiveStarsNumbersView.setImageResource(nResId);
imgFiveStarsNumbersView.setTag("RED,"+strNumber);
}
}
};
} 展开
1个回答
展开全部
GridView有一个setOnItemClickLintener()方法,可以获取当前点击的item,把这个item的位置传给adapter,在adapter的getView()中,修改这个item的背景什么的
追问
能给一个例子吗?
追答
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
//在自定义的adapter中写一个方法setSelected(int);把当前点击的item的position用这个方法传入adapter
gridAdapter.setSelected(position);
}
});
在adapter中这样写:
public class GridAdapter extends BaseAdapter{
private int selected;
public void setSelected(int position){
selected = position;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
……
if( selected == position )
textView.setText("已选");//点击的text显示已选
else
textView.setText("没选");//所有没有点击的都显示没选
……
}
}
大概是这样的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询