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

时间:2016-09-02 11:24:42

标签: java android xml android-layout gridview

检查此图片示例我已完成Material Design Gridview Image 我在我的应用上为网格视图$(function(){ $('.btn').click(function(){ var divClicked = $(this).parent(); divClicked.find('i.open').attr('class', 'complete').html('Complete'); }) }) 做了同样的Material Design,我想将Activity添加到新的onItemClick

我不知道该怎么做,请给出解决方案的简要说明

RES /布局/ activity_main.xml中

Activity

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

的src / MainActivity.java

<?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 / CustomAndroidGridViewAdapter.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");
    }

}

当我在Gridview中点击图像(图像1,图像2等)时,PLease给我解决另一个Actvivty的问题

4 个答案:

答案 0 :(得分:2)

用我的方法替换你的方法

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

答案 1 :(得分:0)

在谷歌待了10秒...... Starting Another Activity

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

点击听众/活动

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();

    }
});

答案 2 :(得分:0)

这是在点击每张图片后进行另一项活动的代码。

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

创建 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);
}
}

然后最后创建 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>

答案 3 :(得分:0)

在MainActivity onCreate方法中,为GridView添加一个OnItemClick侦听器:

gridView.setOnItemClickListener(new OnItemClickListener() {

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

        }
    });