简体   繁体   English

如何从Android GridView onItemClick添加新活动?

[英]How to add New Activity from Android GridView onItemClick?

Check this image sample i have done it Material Design Gridview Image I have done the same Material Design for grid view Activity on my app, i like to add the onItemClick to new Activity , 检查此图像示例我是否已完成Material Design Gridview图像我已经对我的应用程序的网格视图Activity了相同的Material Design ,我想将onItemClick添加到新Activity

I don't know how to do this , please give the brief explanation for the solution 我不知道该怎么做,请简要说明解决方案

res/layout/activity_main.xml RES /布局/ 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>

res/layout/gridview_custom_layout.xml RES /布局/ 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>

src/MainActivity.java SRC / MainActivity.java

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 = {
            "Android",
            "Java",
            "GridView",
            "ListView",
            "Adapter",
            "Custom GridView",
            "Material",
            "XML",
            "Code",

    };
    public static int[] gridViewImages = {
            R.drawable.android_ic,
            R.drawable.android_ic,
            R.drawable.android_ic,
            R.drawable.android_ic,
            R.drawable.android_ic,
            R.drawable.android_ic,
            R.drawable.android_ic,
            R.drawable.android_ic,
            R.drawable.android_ic
    };

    @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("Material Grid");
    }

}

src/CustomAndroidGridViewAdapter.java SRC / CustomAndroidGridViewAdapter.java

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

PLease give me the solution for call another Actvivty when mouse click on Image (Image 1, Image 2, etc) in Gridview this 请给我解决方案,当在Gridview中单击图像(图像1,图像2等)时,调用另一个Actvivty

Replace your method with mine 用我的方法代替

@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]);

 imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

     Intent intent = new Intent(context,YourActivity.class);
     context.startActivity(intent);


        }
    });


    } else {
        grid = (View) convertView;
    }

    return grid;
}

After 10 seconds in Google... Starting Another Activity 在Google中停留 10秒钟后... 开始另一项活动

Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);

And Click Listener/Event 并单击监听器/事件

gridView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Toast.makeText(getApplicationContext(),
                "Item Clicked: " + position, Toast.LENGTH_SHORT).show();

    }
});

here is the code to go another activity after click on every image. 这是单击每个图像后执行另一活动的代码。

public class MainActivity extends AppCompatActivity {

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

public static String[] gridViewStrings = {
        "Android",
        "Java",
        "GridView",
        "ListView",
        "Adapter",
        "Custom GridView",
        "Material",
        "XML",
        "Code",

};
public static int[] gridViewImages = {
        R.drawable.android_ic,
        R.drawable.android_ic,
        R.drawable.android_ic,
        R.drawable.android_ic,
        R.drawable.android_ic,
        R.drawable.android_ic,
        R.drawable.android_ic,
        R.drawable.android_ic,
        R.drawable.android_ic
};

@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.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {          
          if(gridViewStrings[position].equalsIgnoreCase(String.valueOf(1))){
            Intent intent=new Intent(MainActivity.this,SecoundActivity.class);
              intent.putExtra("image",gridViewImages[position]);
              startActivity(intent);
         }
           else if(gridViewStrings[position].equalsIgnoreCase(String.valueOf(2))){
            Intent intent2=new Intent(MainActivity.this,ThirdActvity.class);
              intent2.putExtra("image2",gridViewImages[position]);
              startActivity(intent2);
         }
       //Like this create activites how many images are there in    gridViewImages[]array.you have 9 images so create nine activities..you have to send the url in every activity from one activity to another activity according to your requirement
        }
    });
    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("Material Grid");
}
}

create SecoundActivity.class 创建SecoundActivity.class

  public class SecoundActivity extends AppCompactActivity{
    private ImageView image;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       Intent intent=getIntent();
       int position = intent.getExtras().getInt("image");
        setContentView(R.layout.secound_view);
        image=(ImageView) findViewById(R.id.image);
      image.setImageResource(position);
}
}

then Finally create the secound_view.xml 然后最后创建secound_view.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_horizontal_margin">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image"
       />
</LinearLayout>

In your MainActivity onCreate method, add an OnItemClick listener for the GridView: 在您的MainActivity onCreate方法中,为GridView添加一个OnItemClick侦听器:

gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
               // Your Intent goes here

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 onitemclick 获取数据并将其显示在另一个活动(Android Dev)上? - How to get data from onitemclick and display it on another activity (Android Dev)? 如何使用 onItemClick 打开新活动 - How to open new activity using onItemClick 使用Android AdapterView.OnItemClick侦听器重定向到新的空白活动 - Using Android AdapterView.OnItemClick Listener to redirect to a new blank activity 为什么来自 gridView 的 onItemClick 无法正常工作? - Why is onItemClick from gridView not working correctly? 在新活动中从gridview打开完整图像 - Open full Image from gridview in new Activity 如何启动一个包含 GridView 的新活动,其中填充了来自另一个活动的适配器? - How to start a new activity that contains GridView filled with an adapter from another activity? 在Eclipse Android中添加新活动 - Add new activity in eclipse android 如何在 android 工作室的列表视图中添加新变量(来自以前的活动)到列表中? - How to add a new varible (from previous activity) to a list to display on listview in android studio? 如何在Android中覆盖onItemClick的位置 - how to override the position of an onItemClick in Android 如何将多个按钮从一个活动添加到Android中的不同活动 - How to add multple buttons from one activity to an a different to activity in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM