Android:setOnItemClickListener不起作用

时间:2016-05-28 07:31:36

标签: java android onclicklistener

我正在尝试实现一个`setOnItemClickListener但是它似乎从来没有出现过某种原因。对此最常见的解决方法似乎是添加:

 android:focusable="false"
        android:focusableInTouchMode="false"

`

......但是我似乎仍然无法到达setOnItemClickListener或在其中点燃吐司。

任何建议都表示赞赏。

Java来源:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.preinstall_activity);
    mContext = PreinstallActivity.this;
    //fetchLinks(final UserData userData);

    fetchLinks();

    gridView = (GridView) findViewById(R.id.gridView2);
    gridView.setAdapter(mAdapter);

    // Implement On Item click listener
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            Toast.makeText(PreinstallActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
        }
    });


}

XML来源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".PreinstallActivity">


    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/primaryColor"
        android:layout_width="match_parent"
        android:paddingRight="10dp"
        android:layout_height="wrap_content"

        app:theme="@style/FreeMo.ToolBarStyle"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

        <ImageView
            android:id="@+id/action_bar"
            android:layout_width="30dp"
            android:layout_height="30dp"

            android:layout_gravity="center|start"
            android:background="@drawable/ic_toggle"
            />
        <TextView
            android:id="@+id/headline_text"
            android:paddingLeft="20dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/rec_apps_title"
            android:textColor="@color/edit_text"
            android:textSize="@dimen/headline_text_size" />
        <ImageView
            android:id="@+id/user_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"

            android:layout_gravity="center|end"
            android:background="@drawable/ic_menu"></ImageView>
    </android.support.v7.widget.Toolbar>


    <TextView
        android:id="@+id/rec_apps_txt"
        android:padding="12dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/rec_apps_txt"
        android:textColor="@color/text_view"
        android:textSize="@dimen/default_text_size" />
    <GridView
        android:layout_height="wrap_content"
        android:id="@+id/gridView2"
        android:layout_width="match_parent"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="0dp">
    </GridView>
</LinearLayout>

XML Source 2:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:padding="5dp">

    <ImageView
        android:layout_height="64dp"
        android:id="@+id/imageView1"
        android:layout_width="64dp"
        android:src="@drawable/ic_lock_handle"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
    </ImageView>

    <TextView
        android:text="TextView"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="0dp"
        android:layout_centerHorizontal="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:textSize="10sp"
        android:ellipsize="marquee"></TextView>


</RelativeLayout>

Java Source 2:

public class PreinstallAdapter extends BaseAdapter
{
    private ArrayList<String> listCountry;
    private ArrayList<String> listFlag;
    private Activity activity;

    public PreinstallAdapter(Activity activity, ArrayList<String> listCountry, ArrayList<String> listFlag) {
        super();
        this.listCountry = listCountry;
        this.listFlag = listFlag;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listCountry.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.preinstall_grid, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listCountry.get(position));
        Picasso.with(activity).load(listFlag.get(position)).into(view.imgViewFlag);
//        view.imgViewFlag.setImageResource(listFlag.get(position));

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

1.确保您的Gridview具有宽度和高度。 2.加

android:focusable="false"
android:focusableInTouchMode="false"

到LinearLayout。像这样

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:focusable="false"
android:focusableInTouchMode="false"
```````````````````````````````````>