如何使我的Gridview可点击

时间:2014-08-04 04:19:52

标签: android xml json gridview layout

我有这个gridview,我想让它在点击后开始单独的活动。 例如,我的GridView有剧院的名称和图像。因此,一旦我点击gridview中显示的特定影院,它应该重定向并显示该影院的细节。我怎样才能做到这一点?

TheaterFragment类

package com.fortuna.cinemalk;

import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;

public class TheaterFragment extends Fragment {

    private GridView gridView;

    private ArrayList<BaseElement> filmTheater;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.theater_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        gridView = (GridView) view.findViewById(R.id.gridView1);

        new BackGround().execute();

        return view;
    }

    public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            filmTheater = JSONServices.getTheater();
            return null;
        }

        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setTheater(filmTheater);

            adapter = new LazyAdapter(filmTheater, activity,
                    Element.THEATER_LIST.getType());

            gridView.setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }

}

theater_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth" 
        android:layout_margin="5dp"
        android:columnWidth="100dp"/>

</LinearLayout>

theater_list_layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >



    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"

        android:background="@color/tabDark"
           android:gravity="center"




         />

    <TextView
        android:id="@+id/theaters"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image"
        android:textColor="#ffffff"
        android:background="@color/tabDark"
        android:textStyle="bold" />



</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

public class TheaterFragment extends Fragment {

    private GridView gridView;

    private ArrayList<BaseElement> filmTheater;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.theater_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        gridView = (GridView) view.findViewById(R.id.gridView1);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(activity,theaterDetailsActivity.class);
                intent.putExtra("theaterInfo",passtheaterinfodatahere);
                startActivity(intent);
            }
        });

        new BackGround().execute();

        return view;
    }

    public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            filmTheater = JSONServices.getTheater();
            return null;
        }

        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setTheater(filmTheater);

            adapter = new LazyAdapter(filmTheater, activity,
                    Element.THEATER_LIST.getType());

            gridView.setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }

}

答案 1 :(得分:0)

 gridView.setOnItemClickListener(new  AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent i = new Intent(TheaterFragment .this.getActivity(),OtherActivity.class);
                startActivity(i);
            } 

        });