0

我要创建的是一个由 GridView 组成的片段,该 GridView 充满了可点击的图片。

我已经搜索了很多地方.. Normal Grid GridView Frag

并以某种方式试图将它们一起工作..但似乎并没有完成这项工作

///// 活动类 /////

public static class ChampionFragment extends Fragment {

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

        System.out.println("Inside onCreateView");

        String[] champList = getResources().getStringArray(R.array.championList);
        System.out.println("Got champList");

        View champView = inflater.inflate(R.layout.champion_menu, container, false);
        System.out.println("inflater");

        /*
        ArrayAdapter<String> arrayAptr = new ArrayAdapter<String>(this.getActivity().getApplicationContext(), R.layout.champion_menu,champList);
        System.out.println("Created array Adapter");

        //GridView grid = (GridView)champView.findViewById(R.id.champView);
        System.out.println("Grided");

        grid.setAdapter(arrayAptr);
        System.out.println("Set Adapter");
        */

        return  champView;
        //ArrayList champList = getChampionList();
    }
}

///// 布局 ////

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/champView"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</GridView>

所以是的,我已经尝试了很多方法......我不知道如何让它发挥作用,非常感谢!

4

1 回答 1

0

您应该使用不同的方法设置数据和适配器

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

然后你需要设置一个onItemClickLister

champView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), SomeActivity.class);
            startActivity(intent);
        }
    });
于 2012-08-05T21:16:55.403 回答