2015-03-19 34 views
0

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

我的代码是 -

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> 
+0

为什么不使用itemclicklistener? – Raghunandan 2015-03-19 06:00:33

+0

@拉古南丹 - 是的,它解决了我的问题。 – Anushka 2015-03-19 06:21:53

回答

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); 
     } 
}); 
0

setOnItemSelectedListener在选择模式(多选使用)。所以使用setOnItemClickListener上做的GridView项目的点击操作的工作。

+0

是的,现在工作。 – Anushka 2015-03-19 06:26:49

0
  1. 看起来你的getView有问题,请尝试修改代码:

    @覆盖 公共查看getView(最终诠释的立场,观点convertView,ViewGroup以及母公司){

    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(...)。而且,只有:

此回调,只有当新选择的位置从先前选择的位置是不同的调用,或者如果没有选择的项目。

希望得到这个帮助!