【问题标题】:How do i make my Gridview clickable如何使我的 Gridview 可点击
【发布时间】:2014-09-26 12:21:33
【问题描述】:

我有这个 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>

剧院列表布局

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

【问题讨论】:

    标签: android xml json gridview layout


    【解决方案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();
            }
    
        }
    
    }
    

    【讨论】:

    • 我应该在“passtheaterinfodatahere”中输入什么?
    • 我刚刚提供了演示,您能告诉我如何详细显示剧院信息吗?
    【解决方案2】:
     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);
                } 
    
            });
    

    【讨论】:

    • gridView = (GridView) view.findViewById(R.id.gridView1);
    • 这是什么?意图 i = new Intent(TheaterFragment .this.getActivity(),OtherActivity.class);您是否将 OtherActivity.class 替换为您的预期活动,并在清单中声明该活动
    • 我确实在清单中声明了。我在你的回答中包含了我的 logcat。你能看看吗
    • 我把它改成了 MovieDetailFragment.class
    • 抱歉,先学点基础的,别担心不能用那个代替,因为那不是ACTIVTY,是FRAGMENT,不能调用fragment,每个fragment必须通过activity关联。并且在意图中,您必须调用活动而不是片段。搜索并学习这些!!!
    猜你喜欢
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多