java - 我想在单击时为 gridview 打开新 Activity

标签 java android xml android-studio gridview

我是安卓新手。我创建了 Material 设计 GridView 。现在我想做的是,当我单击每个 GridView 时,它应该对我进行每个新 Activity 。当我点击 ALPHABETS 时,它将转到 ListAlphabet.java。感谢您的帮助。

enter image description here

我的 activity_main.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/android_coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:id="@+id/appbar_layout"
        android:layout_height="@dimen/app_bar_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_android_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="@dimen/expanded_toolbar_title_margin_start"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerInside"
                android:src="@drawable/code"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:id="@+id/nestedscrollview"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <GridView
                android:id="@+id/grid"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:columnWidth="100dp"
                android:gravity="center"
                android:listSelector="#00000000"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

gridview_custom_layout.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/android_gridview_custom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp">

    <com.andexert.library.RippleView
        android:id="@+id/more"
        rv_centered="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:rv_color="#fff"
        app:rv_rippleDuration="200">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/gridview_image"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/gridview_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/grid_image"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Grid View Item"
                android:textColor="#444"
                android:textSize="12sp"
                android:textStyle="bold" />
        </LinearLayout>
    </com.andexert.library.RippleView>
</LinearLayout>

MainActivity.java 是:

package com.affinityapp.sj.alphabet;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.GridView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
    CoordinatorLayout rootLayoutAndroid;
    GridView gridView;
    Context context;
    ArrayList arrayList;

    public static String[] gridViewStrings = {
            "ALPHABETS",
            "NUMBERS",
            "MONTH",
            "DAYS",
            "ANIMALS",
            "CALL US",

    };
    public static int[] gridViewImages = {
            R.drawable.icon_alphabet,
            R.drawable.icon_number,
            R.drawable.icon_calendar,
            R.drawable.icon_days,
            R.drawable.icon_animal,
            R.drawable.icon_call
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        gridView = (GridView) findViewById(R.id.grid);
        gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));

        initInstances();
    }

    private void initInstances() {
        rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
        collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
        collapsingToolbarLayoutAndroid.setTitle("e-Learning");
    }

}

CustomAndroidGridViewAdapter.java 是:

package com.affinityapp.sj.alphabet;

/**
 * Created by SJ on 23-01-2017.
 */
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by HP on 5/11/2016.
 */

public class CustomAndroidGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private final String[] string;
    private final int[] Imageid;

    public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
        mContext = c;
        this.Imageid = Imageid;
        this.string = string;
    }

    @Override
    public int getCount() {
        return string.length;
    }

    @Override
    public Object getItem(int p) {
        return null;
    }

    @Override
    public long getItemId(int p) {
        return 0;
    }

    @Override
    public View getView(int p, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.gridview_custom_layout, null);
            TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
            ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
            textView.setText(string[p]);
            imageView.setImageResource(Imageid[p]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

最佳答案

因此,您可以使用这种方式来点击 GridView :

public class MainActivity extends AppCompatActivity {

Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;

public static String[] gridViewStrings = {
        "ALPHABETS",
        "NUMBERS",
        "MONTH",
        "DAYS",
        "ANIMALS",
        "CALL US",

};
public static int[] gridViewImages = {
        R.drawable.icon_alphabet,
        R.drawable.icon_number,
        R.drawable.icon_calendar,
        R.drawable.icon_days,
        R.drawable.icon_animal,
        R.drawable.icon_call
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    gridView = (GridView) findViewById(R.id.grid);
    gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));

    initInstances();

 gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do whatever you want, like start new Activity or display new view or display dialog box or display just message
        Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});
}

private void initInstances() {
    rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
    collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
    collapsingToolbarLayoutAndroid.setTitle("e-Learning");
    }

     }

希望这对您有所帮助。

关于java - 我想在单击时为 gridview 打开新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41810979/

相关文章:

java - 无法使用 selenium 脚本在 mozilla firefox 中写入 URL。即使eclipse也没有显示任何错误

java - 时间轴动画未按预期工作

java - 在运行时修改Android应用程序

android - 获取 UI 层次结构时出错 Error while obtaining UI hierarchy XML file : com. android.ddmlib.SyncException: Remote object doesn't exist

c# - 如何找出 WCF SOAP XML 响应的反序列化中错误的确切位置?

php - Magento 管理菜单 block 未显示在配置选项卡中?

java - 符号曾经从何而来?

java - 如何从另一个方法访问方法的返回类型

android - Android 和 iOS 接受哪种 X509 格式? (需要安装PKCS#7)

android - 如何在 Robotium 中使用 clickLongOnScreen(x, y)?