Android Gridview不滚动

时间:2014-03-08 02:44:06

标签: android gridview android-fragments scroll custom-adapter

我正在尝试使用gridview来显示我的图像。我让我的代码工作以显示我的图像,但问题是gridview没有滚动显示我的其他图像。我有15张图片,但它只显示6张图片(2列)。我在这里错过了什么?以下是我的代码。

来自我的片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_ates, container, false);
    templateGridContainer = (GridView)rootView.findViewById(R.id.gridview_templates);

    tAdapter = new TemplatesAdapter(getActivity(), imageIDs);
    templateGridContainer.setAdapter(tAdapter);

    templateGridContainer.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
               Toast.makeText(getActivity(), "clicked grid", Toast.LENGTH_SHORT).show();
            }
        });

    return rootView;
}

我的自定义适配器:

public class TemplatesAdapter extends BaseAdapter {
private Context context;
private ImageView imageView;
private TextView imageTitle;
private Integer[] imageIDs;

public TemplatesAdapter(Context c, Integer[] imageSet){
    context = c;
    imageIDs = new Integer[imageSet.length];
    imageIDs = imageSet;
}

//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;
        if (convertView == null){
            gridView = new View(context);
            gridView = inflater.inflate(R.layout.row_template_layout, null);
            imageView = (ImageView)gridView.findViewById(R.id.imageView_temp_img);
            imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(5, 5, 5, 5);

            imageTitle = (TextView)gridView.findViewById(R.id.textView_temp_title);
        }else{
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imageIDs[position]);

        return imageView;
}

//---returns the number of images---
public int getCount(){
    return imageIDs.length;
}
//---returns the ID of an item---
public Object getItem(int position){
    return position;
}
//---returns the ID of an item---
public long getItemId(int position){
    return position;
}
}

我的片段布局:

<?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:background="@color/mx_gray"
android:orientation="vertical" >

<GridView
    android:id="@+id/gridview_templates"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="82"
    android:numColumns="2" >
</GridView>

</LinearLayout>

自定义适配器的行布局:

<?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:background="@color/mx_gray"
android:orientation="vertical"
android:weightSum="100" >

<ImageView
    android:id="@+id/imageView_temp_img"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="90"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView_temp_title"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:gravity="center|left"
    android:paddingLeft="10dp"
    android:text="TextView"
    android:textColor="@color/mx_white" />

</LinearLayout>

设置gridview返回时

public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View  gridView = new View(context);
    gridView = inflater.inflate(R.layout.row_template_layout, null);
    imageView = (ImageView)gridView.findViewById(R.id.imageView_temp_img);
    imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setPadding(5, 5, 5, 5);

    imageTitle = (TextView)gridView.findViewById(R.id.textView_temp_title);

    imageView.setImageResource(imageIDs[position]);
    //            imageTitle.setText(position);
    return gridView;
}

以下是产生的错误:

03-08 19:29:58.613: E/AndroidRuntime(15784): FATAL EXCEPTION: main
03-08 19:29:58.613: E/AndroidRuntime(15784): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:655)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.GridView.onMeasure(GridView.java:1044)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.View.measure(View.java:15504)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.Choreographer.doFrame(Choreographer.java:525)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.os.Handler.handleCallback(Handler.java:615)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.os.Looper.loop(Looper.java:137)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at android.app.ActivityThread.main(ActivityThread.java:4921)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at java.lang.reflect.Method.invokeNative(Native Method)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at java.lang.reflect.Method.invoke(Method.java:511)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-08 19:29:58.613: E/AndroidRuntime(15784):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

出现的可能问题是,您没有更新您的convertView,因为它应该是getView中返回的。 (或您创建的gridView)

当您在MainLayout中已经有一个GridView本身时,不知道为什么要创建一个GridView。

执行以下更改,它应该有效:

   convertView = inflater.inflate(R.layout.row_template_layout, null); // your convert view should be the one that is assigned the inflate layout

   imageView = (ImageView)convertView.findViewById(R.id.imageView_temp_img);
   imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
   imageView.setScaleType(ImageView.ScaleType.FIT_XY);
   imageView.setPadding(5, 5, 5, 5);

   imageTitle = (TextView)gridView.findViewById(R.id.textView_temp_title);        

   imageView.setImageResource(imageIDs[position]);

然后是return语句,

    return convertView;// should return your converView