GridView onClickListener / onItemClickListener无法正常工作

时间:2017-01-02 12:58:14

标签: android android-gridview

我正在尝试在网格视图中实现卡片视图。网格视图显示完美;即使我触摸网格视图项目,它也没有显示任何内容。但是,当我点击其工作下方显示的文字时。我提到This但没有工作......

感谢。

我的代码如下,

// Contact_Backup.java

public class Contact_Backup extends AppCompatActivity {

        @Bind(R.id.gridview)
        GridView gridview;
        String[] contact_screen_array;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contact_backup);
            ButterKnife.bind(this);
            contact_screen_array = getResources().getStringArray(R.array.TextScreen);
            String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
            String concat = "\nContacts";
            gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
        }
    }

// Custom_Adapter

public class Custom_Adapter extends BaseAdapter {

    Context contaxt;
    String[] Array_text;
    String[] Array_icon;
    String concat;
    LayoutInflater inflater;

    public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
    {
        this.contaxt = context;
        this.Array_text = Array_text;
        this.Array_icon = Array_icon;
        this.concat=concat;
        inflater = (LayoutInflater)context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return Array_text.length;
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }


    @Bind(R.id.Screen_icon)
    Button Screen_icon;
    @Bind(R.id.Screen_text)
    TextView Screen_text;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) contaxt
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            grid = new View(contaxt);
            grid = inflater.inflate(R.layout.new_app_card_layout, null);
            ButterKnife.bind(this, grid);
            Screen_text.setText(Array_text[position]+concat);
            Screen_icon.setText(Array_icon[position]);
            Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
            Screen_icon.setTypeface(font);
        } else {
            grid = (View) convertView;
        }

        grid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG::",""+position);
            }
        });
        return grid;
    }
}

// contact_backup.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_backgorund"
    >
    <RelativeLayout
        android:background="@color/transparent"
        android:padding="5.0dip"
        android:id="@+id/top_bar_screen"
        android:layout_weight="0.2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:textColor="@color/newclr"
            android:id="@+id/tvContacts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contacts"
            android:shadowColor="#000000"
            android:shadowDx="1.0"
            android:shadowDy="1.0"
            android:shadowRadius="2.0"
            android:layout_centerVertical="true" />
        <TextView
            android:textColor="@color/newclr"
            android:id="@+id/tvLastBackup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/last_backup"
            android:shadowColor="#000000"
            android:shadowDx="1.0"
            android:shadowDy="1.0"
            android:shadowRadius="2.0"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:id="@+id/v1"
        android:layout_below="@+id/top_bar_screen"
        android:background="@color/newclr"/>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipe"
        android:layout_below="@+id/v1"
        android:paddingTop="20sp"
        android:paddingLeft="10sp"
        android:paddingRight="10sp"
        android:layout_weight="0.6"
        >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:layout_height="wrap_content" />
    </GridLayout>

    <!-- com.startapp.android.publish.banner.Banner
        android:id="@+id/startAppBanner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_weight="0.2"
        android:layout_centerHorizontal="true" /-->


</RelativeLayout>

// new_app_card_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fssr_card"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    card_view:cardBackgroundColor="@color/transparent"
    >

    <LinearLayout
        android:gravity="center"
        android:orientation="vertical"
        android:id="@+id/Contacts_Button_Layout"
        android:background="@drawable/linearlayout_normal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="-1.0dip"
        android:layout_weight="1.0"
        >

        <Button
            android:textSize="35.0sp"
            android:textColor="@color/newclr"
            android:layout_gravity="center"
            android:id="@+id/Screen_icon"
            android:background="@null"
            android:padding="20sp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/fa_icon_contact"
            android:drawablePadding="5.0dip"
            android:textAllCaps="false" />

        <TextView
            android:textSize="15.0sp"
            android:textColor="@color/white"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:id="@+id/Screen_text"
            android:background="@color/newclr"
            android:padding="5sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/contactsbackup"
            android:textAllCaps="false"/>

    </LinearLayout>
</android.support.v7.widget.CardView>

5 个答案:

答案 0 :(得分:0)

在实际gridview对象上设置侦听器。 gridView.setOnItemClickListener(new OnItemClickListener()...);

答案 1 :(得分:0)

试试这个,

 public class Contact_Backup extends AppCompatActivity {

        @Bind(R.id.gridview)
        GridView gridview;
        String[] contact_screen_array;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contact_backup);
            ButterKnife.bind(this);
            contact_screen_array = getResources().getStringArray(R.array.TextScreen);
            String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
            String concat = "\nContacts";
            gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
            gridview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int i=0;i<contact_screen_array.length;i++) {
                        Log.d("TAG::", "Position"+contact_screen_array[i]);
                    }
                }
            });
        }
    }

答案 2 :(得分:0)

使用此方法点击gridview项目:

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // your action when gridView item is clicked   

        }
    });

答案 3 :(得分:0)

最后,我成功了。这是代码。

只需在// new_app_card_layout.xml中使用Textview而不是Button。

只需更新它,

<TextView
        android:textSize="35.0sp"
        android:textColor="@color/newclr"
        android:layout_gravity="center"
        android:id="@+id/Screen_icon"
        android:background="@null"
        android:padding="20sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/fa_icon_contact"
        android:drawablePadding="5.0dip"
        android:textAllCaps="false"
        android:textAlignment="center" />

非常感谢你们所有人的帮助...... :)

答案 4 :(得分:-1)

试试这个我更新了自定义适配器类。请使用此更新代码再次检查。

public class Custom_Adapter extends BaseAdapter {

    Context contaxt;
    String[] Array_text;
    String[] Array_icon;
    String concat;
    LayoutInflater inflater;

    public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
    {
        this.contaxt = context;
        this.Array_text = Array_text;
        this.Array_icon = Array_icon;
        this.concat=concat;
        inflater = (LayoutInflater)context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return Array_text.length;
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }


    @Bind(R.id.Screen_icon)
    Button Screen_icon;
    @Bind(R.id.Screen_text)
    TextView Screen_text;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) contaxt
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            grid = new View(contaxt);
            grid = inflater.inflate(R.layout.new_app_card_layout, null);
            ButterKnife.bind(this, grid);
            Screen_text.setText(Array_text[position]+concat);
            Screen_icon.setText(Array_icon[position]);
            Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
            Screen_icon.setTypeface(font);

            grid.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("TAG::",""+position);
                }
            });

        } else {
            grid = (View) convertView;

            grid.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("TAG::",""+position);
                }
            });
        }
        return grid;
    }
}

希望这会帮助你!