【问题标题】:gridview setOnIemSelectedListener not working网格视图 setOnIemSelectedListener 不起作用
【发布时间】:2015-03-19 14:29:06
【问题描述】:

我有一个带有 imageview 和 textview 的网格视图。 Gridview itemSelectedListener 不起作用。我已经尝试了这里提供的所有解决方案,即使可聚焦和可点击的 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 文件-

ma​​in.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>

【问题讨论】:

  • 为什么不使用 itemclicklistener?
  • @Raghunandan-yeah 它解决了我的问题。

标签: android android-gridview


【解决方案1】:

如果你想在用户点击 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】:
    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(...)。而且只有:

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

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 2016-06-22
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多