gridview setOnIemSelectedListener不起作用

时间:2015-03-19 05:58:22

标签: android android-gridview

我有一个带有imageview和textview的网格视图。 Gridview itemSelectedListener不工作。我已经尝试了这里提供的每个解决方案,即使焦点和可点击的真或假。如果我在适配器中应用单击侦听器,那么它的工作正常。我不明白问题是什么。

我的代码是 -

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>

3 个答案:

答案 0 :(得分:0)

setOnItemSelectedListener在选择模式下工作(用于多项选择)。所以使用setOnItemClickListener对GridView项目点击进行操作。

答案 1 :(得分:0)

如果您想在用户点击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);
         }
});

答案 2 :(得分:0)

  1. 查看您的getView有问题,请尝试修改后的代码:

    @覆盖 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;
    

    }

  2. onItemSelectedListener用于查看项目选择但不点击,点击时无法获得回调onItemSelected(...)。只有:

  3.   

    仅当新选择的位置与先前选择的位置不同或者没有选定的项目时,才会调用此回调。

    希望这有帮助!