Gridview滚动图像在android中运行不顺畅

时间:2015-10-28 07:31:28

标签: android gridview scrollview

您好,在下面将所有图像显示为gridview.gridview图像正确显示。默认网格视图将提供滚动选项,现在滚动图像,它不能顺利滚动。

任何人都可以帮助我。如何平滑滚动所有图像,如正常滚动。这是我的下面的代码,任何我错误的地方,请帮助我

的java

public class GridFragment2 extends Fragment {

    private static final String TAG = GridFragment.class.getSimpleName();
public static boolean isSelectedGrid2;
    private GridView mGridView;
    private ProgressBar mProgressBar;

    private GridViewAdapter2 mGridAdapter;
    private ArrayList<GridItem> mGridData;
    private String FEED_URL = "http://javatechig.com/?json=get_recent_posts&count=45", searchKey;
    String vv;
    LinearLayout top;
    View hr;

    String catid;



    SharedPreferences imgSh;
    SharedPreferences p, got;



    public GridFragment2() {
    }

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

        View rootView = inflater.inflate(R.layout.activity_gridview2, container, false);

        mGridView = (GridView)rootView.findViewById(R.id.gridView);
        mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
        top = (LinearLayout)rootView.findViewById(R.id.top);
        hr = rootView.findViewById(R.id.hr);
        top.setVisibility(View.GONE);
        hr.setVisibility(View.GONE);



        imgSh = this.getActivity().getSharedPreferences("data", Context.MODE_APPEND);
        p = this.getActivity().getSharedPreferences("gridData", Context.MODE_APPEND);
        got = this.getActivity().getSharedPreferences("gotData", Context.MODE_APPEND);

        //Initialize with empty data
        mGridData = new ArrayList<>();
        mGridAdapter = new GridViewAdapter2(rootView.getContext(), R.layout.grid_item_layout2, mGridData);
        mGridView.setAdapter(mGridAdapter);

        //Grid view click event
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Get item at position
                isSelectedGrid2=true;
                String catalogueID = p.getString("SelectedCatalogueIdFromGrid1", "");
                GridItem item = (GridItem) parent.getItemAtPosition(position);
                Intent intent = new Intent(getActivity(), ImageGallery.class);
                ImageView imageView = (ImageView) v.findViewById(R.id.grid_item_image);
                int[] screenLocation = new int[2];
                imageView.getLocationOnScreen(screenLocation);
                SharedPreferences prefPosition=getActivity().getSharedPreferences("positionPref", Context.MODE_PRIVATE);
                SharedPreferences.Editor positionEditor=prefPosition.edit();
                positionEditor.putInt("position", position);
                positionEditor.commit();
                //Pass the image title and url to DetailsActivity
                intent.putExtra("left", screenLocation[0]).
                        putExtra("top", screenLocation[1]).
                        putExtra("width", imageView.getWidth()).
                        putExtra("height", imageView.getHeight()).
                        putExtra("title", item.getTitle()).
                        putExtra("image", item.getImage()).
                        putExtra("catid", catalogueID).putExtra("position",position);

                //   Log.d("image", (item.getImage()).toString());

                //Start details activity
                getActivity().finish();
                startActivity(intent);
            }
        });

        //Start download
        new AsyncHttpTask().execute(FEED_URL);
        mProgressBar.setVisibility(View.VISIBLE);

        return rootView;
    }



    //Downloading data asynchronously
    public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {

        @Override
        protected Integer doInBackground(String... params) {
            Integer result = 1;

            parseResult();

            return result;

        }
        @Override
        protected void onPostExecute(Integer result) {
            // Download complete. Lets update UI

            if (result == 1) {
                mGridAdapter.setGridData(mGridData);
            } else {
                Toast.makeText(getActivity().getBaseContext(), "Failed to fetch data!", Toast.LENGTH_SHORT).show();
            }

            //Hide progressbar
            mProgressBar.setVisibility(View.GONE);
        }
    }


    String streamToString(InputStream stream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
        String line;
        String result = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }

        // Close stream
        if (null != stream) {
            stream.close();
        }
        return result;
    }


    private void parseResult() {


        catid = p.getString("SelectedCatalogueIdFromGrid1", "");
        int clickedPosn = p.getInt("clickedPosition", 0);
        int count = p.getInt("numOfImagesInside"+catid+clickedPosn, 0);

        Log.d("counterman-id", String.valueOf(count));

        GridItem item;
        for (int i = 0; i < count; i++) {

            item = new GridItem();

            item.setTitle("");
            // item.setImage("file://" + Environment.getExternalStorageDirectory() + "/" + "thumbImage" + catid + i + ".jpg");
            item.setImage("/data/data/com.example.adaptiz.tanishq/"+"thumbImage" + catid + i+".jpg");
            // item.setImage(imgSh.getString("thumb" + i, ""));
            mGridData.add(item);
        }

    }

适配器

public class GridViewAdapter2 extends ArrayAdapter<GridItem> {

    //private final ColorMatrixColorFilter grayscaleFilter;
    private Context mContext;
    private int layoutResourceId;
    private ArrayList<GridItem> mGridData = new ArrayList<GridItem>();

    public GridViewAdapter2(Context mContext, int layoutResourceId, ArrayList<GridItem> mGridData) {
        super(mContext, layoutResourceId, mGridData);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.mGridData = mGridData;
    }


    /**
     * Updates grid data and refresh grid items.
     *
     * @param mGridData
     */
    public void setGridData(ArrayList<GridItem> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;



        if (row == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            Typeface type_normal = Typeface.createFromAsset(getContext().getAssets(), "HelveticaNeue-Light.otf");

            holder = new ViewHolder();
            holder.titleTextView = (TextView) row.findViewById(R.id.grid_item_title);
            holder.titleTextView.setTypeface(type_normal);

            holder.download=(ImageView)row.findViewById(R.id.img);

            holder.imageView = (ImageView) row.findViewById(R.id.grid_item_image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        GridItem item = mGridData.get(position);

        holder.titleTextView.setText(Html.fromHtml(item.getTitle()));


        File image = new File(item.getImage(), "");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];
        options.inPurgeable = true;
        Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), options);
        // bitmap = Bitmap.createScaledBitmap(bitmap, 230, 230, true);
        Log.d("the bitmap size", String.valueOf(bitmap.getWidth() + " " + bitmap.getHeight()));
        holder.imageView.setImageBitmap(bitmap);
        //bitmap=null;

        // Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
        return row;
    }

    static class ViewHolder {
        TextView titleTextView;
        ImageView imageView;
        ImageView download;
    }

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/toprel"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:id="@+id/top"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/searchTitle"
            android:layout_centerVertical="true"
            android:text="Search"
            android:textSize="15sp"
            android:gravity="left"
            android:layout_toRightOf="@+id/leftTitle"
            android:layout_toEndOf="@+id/leftTitle" />

        <EditText
            android:inputType="text"
            android:layout_centerVertical="true"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:id="@+id/searchBox"
            android:background="@drawable/searchbg2x"
            android:layout_toRightOf="@+id/searchTitle"
            android:layout_toEndOf="@+id/searchTitle"
            android:layout_marginLeft="15dp"
            android:singleLine="true"
            android:imeOptions="actionSearch"



            />


    </LinearLayout>

    <View
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:id="@+id/hr"
        android:background="#000000"
        android:layout_below="@id/top"
        />



    <GridView
        android:layout_below="@+id/hr"
        android:id="@+id/gridView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:columnWidth="200dp"
        android:gravity="center"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp"
        android:listSelector="@android:color/transparent"
        android:clickable="true"
        android:fastScrollEnabled="true"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerInParent="true"
        android:visibility="gone"/>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:orientation="horizontal"
        android:id="@+id/userlayout"
        android:layout_alignParentTop="true"
        android:visibility="gone"
        android:layout_alignRight="@+id/gridView"
        android:layout_alignEnd="@+id/gridView"

        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="18sp"
            android:id="@+id/userView"
            android:text="username"

            />

        <ImageView
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_width="20dp"
            android:layout_height="10dp"
            android:background="@drawable/icon"
            />

    </LinearLayout>



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="15sp"
        android:layout_gravity="bottom"
        android:layout_alignBottom="@+id/gridView"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

为此你必须使用Image Loader来加载图像。

请参阅以下链接。

http://www.androidhive.info/2012/07/android-loading-image-from-url-http/