Fragment中无法选择GridView项

时间:2014-11-03 17:07:09

标签: android gridview fragment

我在Fragment中有Activity,我正在尝试从自定义适配器显示GridView`。我能够显示GridView但无法选择gridview的任何项目

任何人都可以帮我识别问题。

请找到我使用过的代码:

//MainActivity

package com.example.frag;

import java.util.ArrayList;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.os.Build;

public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        GridView optionsGridView;
        ArrayList<InfoItems> items = new ArrayList<InfoItems>();
        OptionsAdapter adapter;
        String items2[] = {"1","2","3","4"};

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
//          implentation of display of options in GridView

            optionsGridView = (GridView) rootView.findViewById(R.id.optionsGrid);

            /*items.add(new InfoItems("Passport"));
            items.add(new InfoItems("Aadhar"));
            items.add(new InfoItems("Pan"));
            items.add(new InfoItems("DL"));*/




            adapter = new OptionsAdapter(this.getActivity(), items2);

            optionsGridView.setAdapter(adapter);
            return rootView;
        }
    }
}


Custom Adapter

package com.example.frag;

import java.util.ArrayList;
import dalvik.bytecode.OpcodeInfo;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OptionsAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<InfoItems> infoItems;
    String items[];

    public OptionsAdapter(Context context,ArrayList<InfoItems> items) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.infoItems = items;
    }

    public OptionsAdapter(Context contsxt,String txt[]) {
        // TODO Auto-generated constructor stub
        this.context = contsxt;
        this.items = txt;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return infoItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if(convertView ==  null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.info_options, null);
        }

        TextView option = (TextView) convertView.findViewById(R.id.optionsItem);
        option.setText(items[position]);

        return convertView;
    }

}


    fragment xml -- layout which will be displayed in the fragment

<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="com.example.frag.MainActivity$PlaceholderFragment" >

    <GridView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/optionsGrid"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:numColumns="2"
        android:gravity="center"
        android:layout_marginTop="10dp"/>


</RelativeLayout>

item xml -- list item of the custom adapter

<?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="vertical" 
    android:background="#000"
    android:clickable="true">

    <TextView android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textColor="#f000ff"
        android:textSize="20sp"
        android:textStyle="bold"
        android:id="@+id/optionsItem"
        android:text="passport"
        android:clickable="true"
        android:layout_gravity="center"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

Squonk是正确的,你需要调用optionsGridView.setAdapter(new OnItemClickListener ....)。您还可以在片段中实现OnItemClickListener并调用optionsGridView.setAdapter(this)。

答案 1 :(得分:0)

PlaceholderFragment 中,如下所示:

full_data.tar