GridView不能在Android中平滑滚动

时间:2014-07-09 09:55:10

标签: android

我有一个gridview with imageview和title,由自定义适配器填充。一切正常。 但是当我滚动网格视图时,它会轻弹并花时间滚动。所以任何人都可以帮助我顺利滚动..我尝试了其他解决方案但是没有工作。

以下是我的grid_layout 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="vertical"
              android:id="@+id/LinearLayoutUserHome"

         >
    <!--  android:background="@drawable/ic_main_icon"-->
    <Spinner
            android:id="@+id/spinnerCityAuto"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:prompt="@string/spinner_title"
            style="@style/TestSpinnerStyle"
            />

        <GridView
                android:id="@+id/gridView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:numColumns="3"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:stretchMode="columnWidth"
                android:smoothScrollbar="true"
                android:fastScrollEnabled="true">
        </GridView>
</LinearLayout>

自定义适配器getView方法

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    CustomClassGrid custom=arrayList.get(position);
    int height = metrics.heightPixels;
    Bitmap bitmap=null;
    URL imageURL = null;
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.row_grid, null);
        holder = new ViewHolder();

        holder.thumbImage =  (ImageView) convertView.findViewById(R.id.grid_image);
        holder.text =  (TextView) convertView.findViewById(R.id.grid_text);
        holder.thumbImage.getLayoutParams().height = 100;
        holder.thumbImage.getLayoutParams().width = width / 3;
        convertView.setTag(holder);

    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    try {
        imageURL = new URL(custom.getImage());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    holder.thumbImage.setImageBitmap(bitmap);
    holder.text.setText(custom.getText());

    return convertView;

}
public class ViewHolder {
    ImageView thumbImage;
    TextView text;
}

先谢谢..

2 个答案:

答案 0 :(得分:0)

设置适配器后,将此代码添加到网格视图中。

 yourgridview.setOnTouchListener(new GridView.OnTouchListener() {
                            @Override
                            public boolean onTouch(View v, MotionEvent event) {
                                int action = event.getAction();
                                switch (action) {
                                case MotionEvent.ACTION_DOWN:
                                    // Disallow ScrollView to intercept
                                    // touch
                                    // events.
                                      v.getParent().requestDisallowInterceptTouchEvent(true);
                                    break;

                                case MotionEvent.ACTION_UP:
                                    // Allow ScrollView to intercept touch
                                    // events.
                                    v.getParent().requestDisallowInterceptTouchEvent(false);
                                    break;

                                 case MotionEvent.ACTION_MOVE:
                                         // Allow ScrollView to intercept touch
                                         // events.

                                         //return true;
                                         v.getParent()
                                           .requestDisallowInterceptTouchEvent(
                                             false);
                                         return true;



                                }



                                // Handle ListView touch events.
                                v.onTouchEvent(event);
                                return false;
                            }
                        });

答案 1 :(得分:0)

我的问题现在已解决..由于gridview中加载了大量图像,因此发生了这种情况。因此无论何时滚动,网格都会重新加载并轻弹一段时间。因此我在加载时缓存了所有图像..我使用AndroidQuery进行图像加载。