gridview setOnIemSelectedListener 不工作
gridview setOnIemSelectedListener not working
我有一个带有图像视图和文本视图的网格视图。 Gridview itemSelectedListener 不是 working.I 已经尝试了此处提供的所有解决方案,即使可聚焦和可点击为 true 或 false。如果我在适配器中应用点击监听器,那么它工作正常。我不明白问题是什么。
我的代码是-
MainActivity.java
public class HomeScreen extends Activity {
TextView txtUserName;
public CustomSharedPreferences preferences;
// Array of strings storing options
String[] options = new String[] {
"Create Group",
"Add People",
"Personal",
"Sharing",
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] images = new int[]{
R.drawable.user_group,
R.drawable.add_user,
R.drawable.personal,
R.drawable.sharing,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.option);
txtUserName=(TextView)findViewById(R.id.textViewUserName);
preferences = CustomSharedPreferences.getInstance(this);
String username= preferences.getStringValue(CustomSharedPreferences.USERNAME,"");
txtUserName.setText(username);
GridView gridView = (GridView) findViewById(R.id.gridView1);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this,options,images));
gridView.setTextFilterEnabled(true);
gridView.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
Log.e("inisde","click grid");
Log.e("pos",""+position);
switch(position)
{
case 0:
Intent createGrp=new Intent(HomeScreen.this,CreateGroup.class);
startActivity(createGrp);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e("on","nothingselected");
// TODO Auto-generated method stub
}
});
}
}
Adapter.java
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
String [] option;
int [] imageId;
//private static LayoutInflater inflater=null;
// Keep all Images in array
/* public Integer[] mThumbIds = {
R.drawable.user_group
R.drawable.pic_2,
R.drawable.pic_3
};
*/
// Constructor
public ImageAdapter(Context c,String[] options,int[] img)
{
mContext = c;
option=options;
imageId=img;
// inflater = ( LayoutInflater )mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return option.length;
}
/*@Override
public int getCount() {
return mThumbIds.length;
}*/
@Override
public Object getItem(int position) {
return option[position];
}
@Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder
{
TextView tv;
ImageView img;
}
@SuppressLint("InflateParams")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// First let's verify the convertView is not null
if (convertView == null)
{
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_layout, parent,false);
}
holder = new ViewHolder();
holder.tv=(TextView) convertView.findViewById(R.id.txt);
holder.img=(ImageView) convertView.findViewById(R.id.imageView1);
holder.img.setClickable(false);
holder.tv.setText(option[position]);
holder.img.setImageResource(imageId[position]);
/* convertView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(position==0)
{
Intent createGrp=new Intent(mContext,GroupList.class);
mContext.startActivity(createGrp);
}
else if(position==1)
{
Intent addPeople=new Intent(mContext,AddMembers.class);
mContext.startActivity(addPeople);
}
else if(position==3)
{
Intent sharing=new Intent(mContext,AddMembers.class);
mContext.startActivity(sharing);
}
// TODO Auto-generated method stub
Toast.makeText(mContext, "You Clicked "+option[position], Toast.LENGTH_LONG).show();
}
});*/
return convertView;
}
}
XML 个文件-
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/welcome_logout"/>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:layout_marginTop="@dimen/editTextMarginTop"
android:gravity="center"
android:stretchMode="columnWidth"
android:clickable="true">
</GridView>
</LinearLayout>
gridlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:gravity="center_horizontal"/>
</LinearLayout>
如果你想在用户单击 GridView 中的每个项目时执行某些操作,你应该使用 GridView 的 setOnItemClickListener。
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent createGrp = new Intent(HomeScreen.this,CreateGroup.class);
startActivity(createGrp);
}
});
看起来你的 getView
有问题,尝试修改代码:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// First let's verify the convertView is not null
if (convertView == null)
{
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_layout, parent,false);
holder = new ViewHolder();
holder.tv=(TextView) convertView.findViewById(R.id.txt);
holder.img=(ImageView) convertView.findViewById(R.id.imageView1);
holder.img.setClickable(false);
convertView.setTag(holder);
}
else
holder = (ViewHolder)convertView.getTag();
holder.tv.setText(option[position]);
holder.img.setImageResource(imageId[position]);
return convertView;
}
onItemSelectedListener
是view item selection but not click,点击时无法回调onItemSelected(...)
。并且只有:
This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.
希望对您有所帮助!
我有一个带有图像视图和文本视图的网格视图。 Gridview itemSelectedListener 不是 working.I 已经尝试了此处提供的所有解决方案,即使可聚焦和可点击为 true 或 false。如果我在适配器中应用点击监听器,那么它工作正常。我不明白问题是什么。
我的代码是-
MainActivity.java
public class HomeScreen extends Activity {
TextView txtUserName;
public CustomSharedPreferences preferences;
// Array of strings storing options
String[] options = new String[] {
"Create Group",
"Add People",
"Personal",
"Sharing",
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] images = new int[]{
R.drawable.user_group,
R.drawable.add_user,
R.drawable.personal,
R.drawable.sharing,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.option);
txtUserName=(TextView)findViewById(R.id.textViewUserName);
preferences = CustomSharedPreferences.getInstance(this);
String username= preferences.getStringValue(CustomSharedPreferences.USERNAME,"");
txtUserName.setText(username);
GridView gridView = (GridView) findViewById(R.id.gridView1);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this,options,images));
gridView.setTextFilterEnabled(true);
gridView.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
Log.e("inisde","click grid");
Log.e("pos",""+position);
switch(position)
{
case 0:
Intent createGrp=new Intent(HomeScreen.this,CreateGroup.class);
startActivity(createGrp);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e("on","nothingselected");
// TODO Auto-generated method stub
}
});
}
}
Adapter.java
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
String [] option;
int [] imageId;
//private static LayoutInflater inflater=null;
// Keep all Images in array
/* public Integer[] mThumbIds = {
R.drawable.user_group
R.drawable.pic_2,
R.drawable.pic_3
};
*/
// Constructor
public ImageAdapter(Context c,String[] options,int[] img)
{
mContext = c;
option=options;
imageId=img;
// inflater = ( LayoutInflater )mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return option.length;
}
/*@Override
public int getCount() {
return mThumbIds.length;
}*/
@Override
public Object getItem(int position) {
return option[position];
}
@Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder
{
TextView tv;
ImageView img;
}
@SuppressLint("InflateParams")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// First let's verify the convertView is not null
if (convertView == null)
{
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_layout, parent,false);
}
holder = new ViewHolder();
holder.tv=(TextView) convertView.findViewById(R.id.txt);
holder.img=(ImageView) convertView.findViewById(R.id.imageView1);
holder.img.setClickable(false);
holder.tv.setText(option[position]);
holder.img.setImageResource(imageId[position]);
/* convertView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(position==0)
{
Intent createGrp=new Intent(mContext,GroupList.class);
mContext.startActivity(createGrp);
}
else if(position==1)
{
Intent addPeople=new Intent(mContext,AddMembers.class);
mContext.startActivity(addPeople);
}
else if(position==3)
{
Intent sharing=new Intent(mContext,AddMembers.class);
mContext.startActivity(sharing);
}
// TODO Auto-generated method stub
Toast.makeText(mContext, "You Clicked "+option[position], Toast.LENGTH_LONG).show();
}
});*/
return convertView;
}
}
XML 个文件-
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/welcome_logout"/>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:layout_marginTop="@dimen/editTextMarginTop"
android:gravity="center"
android:stretchMode="columnWidth"
android:clickable="true">
</GridView>
</LinearLayout>
gridlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:gravity="center_horizontal"/>
</LinearLayout>
如果你想在用户单击 GridView 中的每个项目时执行某些操作,你应该使用 GridView 的 setOnItemClickListener。
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent createGrp = new Intent(HomeScreen.this,CreateGroup.class);
startActivity(createGrp);
}
});
看起来你的
getView
有问题,尝试修改代码:@Override public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null; // First let's verify the convertView is not null if (convertView == null) { // This a new view we inflate the new layout LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.gridview_layout, parent,false); holder = new ViewHolder(); holder.tv=(TextView) convertView.findViewById(R.id.txt); holder.img=(ImageView) convertView.findViewById(R.id.imageView1); holder.img.setClickable(false); convertView.setTag(holder); } else holder = (ViewHolder)convertView.getTag(); holder.tv.setText(option[position]); holder.img.setImageResource(imageId[position]); return convertView;
}
onItemSelectedListener
是view item selection but not click,点击时无法回调onItemSelected(...)
。并且只有:
This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.
希望对您有所帮助!