Android-如何在 ListView 的页脚添加 GridView

标签 android gridview scrollview

enter image description here

我有一个 GridView ,我想将其添加到 ListView 的页脚中,因为我希望此页面应该具有完整的页面滚动功能。

这是我的代码,我在其中使用我的 GridView 适配器设置我的 GridView ,但它在页脚中显示为空白。我有两个 xml,一个用于页眉,另一个仅用于 GridView 。

画廊 GridView 适配器

public class GalleryMainActivityGridViewAdapter extends BaseAdapter {

    /**
     * Description:Declare the UI components.
     */
    private Context mContextGridView;
    private GallerySmartLazyLoader sl;
    ArrayList<HashMap<String, String>> galleryArray;

    /**
     * This method is use to set object that will control the listview
     * 
     * @param c
     * @param resultArray
     * 
     */

 public GalleryMainActivityGridViewAdapter(Context c, ArrayList<HashMap<String,String>>              
        resultArray) { 
            mContextGridView = c;
        galleryArray = new ArrayList<HashMap<String, String>>();
        galleryArray = resultArray;
        sl = new GallerySmartLazyLoader(mContextGridView);
    }

    public int getCount() {

        return galleryArray.size();
    }

    public Object getItem(int position) {

        return null;
    }

    public long getItemId(int position) {

        return 0;
    }

    /**
     * Description:This method returns a bitmap related to drawable.
     * 
     * @param bitmapUrl
     *            to set the background on detail page
     * 
     */
public Bitmap getBitmap(String bitmapUrl) {

        URL url;
        try {
            System.out.println("bitmapUrl :" + " bitmapUrl : " + bitmapUrl);
            url = new URL(bitmapUrl);
            return        BitmapFactory.decodeStream(url.openConnection().getInputStream());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    // create View for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        View MyView = convertView;

        if (convertView == null) {
            /* create a new view of our layout and inflate it in the row */

            // Inflate the layout
            LayoutInflater inflater = (LayoutInflater) mContextGridView.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            MyView = inflater.inflate(R.layout.gallery_main_page_grid_item, null);

            // Initialize the UI components
            ImageView imgView_Grid_PlayVideo = (ImageView) MyView.findViewById(R.id.imgView_Grid_PlayVideo);
            ImageView imgView_Grid_Thumbnail_Gallery = (ImageView) MyView.findViewById(R.id.imgView_Grid_Thumbnail_Gallery);
            TextView txtView_Grid_Views_Gallery = (TextView) MyView.findViewById(R.id.txtView_Grid_Views_Gallery);
            Typeface txtViewForViews = Typeface.createFromAsset(MyView.getContext().getAssets(), "fonts/arial.ttf");
            txtView_Grid_Views_Gallery.setTypeface(txtViewForViews);

            TextView txtView_Grid_Name_Gallery = (TextView) MyView.findViewById(R.id.txtView_Grid_Name_Gallery);
            Typeface txtViewForName = Typeface.createFromAsset(MyView.getContext().getAssets(), "fonts/arial_bold.ttf");
            txtView_Grid_Views_Gallery.setTypeface(txtViewForName);

            ImageView imgView_GridItem_Gallery = (ImageView) MyView.findViewById(R.id.imgView_GridItem_Gallery);

            // This will set the item coming from API
            imgView_Grid_Thumbnail_Gallery.setImageBitmap(getBitmap(galleryArray.get(0).get("publicUrl") + "/11"));
            txtView_Grid_Views_Gallery.setText(galleryArray.get(position).get("hits") + " views");
            txtView_Grid_Name_Gallery.setText(galleryArray.get(position).get("user_name"));
            imgView_GridItem_Gallery.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView_GridItem_Gallery.setPadding(0, 0, 0, 0);
            String url = galleryArray.get(position).get("thumbUrl") + "/13";
            imgView_GridItem_Gallery.setTag(url);
            sl.DisplayImage(url, (Activity) mContextGridView, imgView_GridItem_Gallery);

            if (galleryArray.get(position).get("filetype").toString().equals("1")) {
                imgView_Grid_PlayVideo.setVisibility(View.GONE);
            } else {
                imgView_Grid_PlayVideo.setVisibility(View.VISIBLE);
            }

        }

        return MyView;
    }

}

GallaryLoginMainActivity

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallary_login_main_page_list);
    data = new ArrayList<ArrayList<HashMap<String, String>>>();
    lstGallaryMain = (ListView) findViewById(R.id.lstGallaryMain);
    inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    init(this, R.id.main, getIntent());

    if (!Connection()) {
        Context context = getApplicationContext();
        CharSequence text = "Sorry you need an Internet connection!  Please try again when the network is available.";
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        toast.show();
        finish();

    } else {

        /*
         * 
         * This method is used to Show The loading dialog till the data
         * loads for main page.
         */

        new AsyncTask<Void, Void, Void>() {
            protected void onPreExecute() {
                loadingDialog = ProgressDialog.show(GallaryLoginMainActivity.this, getResources().getString(R.string.app_name), "Loading...");
            }

            @Override
            protected Void doInBackground(Void... params) {
                loadDataFromServer();
                return null;
            };

            protected void onPostExecute(Void result) {

                View footerView = inflater.inflate(R.layout.test_media, null);


                GalleryMainActivityGridViewAdapter galleryMainActivityGridViewAdapter = new GalleryMainActivityGridViewAdapter(GallaryLoginMainActivity.this,userDataActivity);

                GridView gridView = (GridView)footerView.findViewById(R.id.gridview_Gallery);
                gridView.setAdapter(galleryMainActivityGridViewAdapter);
                lstGallaryMain.addFooterView(gridView);

                ((GalleryMainActivityGridViewAdapter)(galleryMainActivityGridViewAdapter)).notifyDataSetChanged();
                lstGallaryMain.setAdapter(new GalleryCustomAdapterForMainPage<T>(GallaryLoginMainActivity.this, data));
                ((BaseAdapter)(lstGallaryMain.getAdapter())).notifyDataSetChanged();
                lstGallaryMain.addFooterView(gridView);
                GallaryLoginMainActivity.this.onContentChanged();


                if (loadingDialog != null && loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                }
            };
        }.execute();
    }


}

最佳答案

最后我得到了我自己的问题的答案我已经使用 android 查询库创建了新的适配器并在我的屏幕上实现了新的 GridView 适配器并发现 ScrollView 中的平滑度更高......

这是我的代码

public class GalleryMainActivityGridViewAdapter extends BaseAdapter {

    AQuery listAQ;
    Context mContext;
    int layoutResourceId;
    ArrayList<HashMap<String, String>> dataArray;

    public GalleryMainActivityGridViewAdapter(Context context, ArrayList<HashMap<String, String>> resultArray) {
        this.mContext = context;
        // this.layoutResourceId = layoutId;
        this.dataArray = resultArray;
        listAQ = new AQuery(mContext);
        // aa = new ArrayAdapter<Photo>(mContext, layoutResourceId);
    }

    public int getCount() {
        return dataArray.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return 0;
    }

    @SuppressWarnings("deprecation")
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.gallery_main_page_grid_item, parent, false);
        }
        Holder holder = new Holder();

        AQuery aq = listAQ.recycle(convertView);

        holder.imgView_Grid_Thumbnail_Gallery = (ImageView) convertView.findViewById(R.id.imgView_Grid_Thumbnail_Gallery);

        if (holder.imgView_Grid_Thumbnail_Gallery != null) {
            aq.id(holder.imgView_Grid_Thumbnail_Gallery.getId()).image(dataArray.get(position).get("thumbUrl") + "/11");
        }

        holder.txtView_Grid_Name_Gallery = (TextView) convertView.findViewById(R.id.txtView_Grid_Name_Gallery);
        if (holder.txtView_Grid_Name_Gallery != null) {
            aq.id(holder.txtView_Grid_Name_Gallery.getId()).text(dataArray.get(position).get("user_name"));
            Typeface txtViewForName = Typeface.createFromAsset(convertView.getContext().getAssets(), "fonts/arial_bold.ttf");
            holder.txtView_Grid_Name_Gallery.setTypeface(txtViewForName);

        }

        holder.imgView_GridItem_Gallery = (ImageView) convertView.findViewById(R.id.imgView_GridItem_Gallery);

        if (holder.imgView_GridItem_Gallery != null) {

            Bitmap cachedImage = aq.getCachedImage(R.drawable.loading);
            String publicUrl = dataArray.get(position).get("publicUrl") + "/13";
            if (aq.shouldDelay(convertView, parent, publicUrl, 0)) {
                aq.id(holder.imgView_GridItem_Gallery.getId()).image(cachedImage, 0.75f);
            } else {
                aq.id(holder.imgView_GridItem_Gallery.getId()).image(publicUrl, true, true, 0, 0, cachedImage, 0, 0.75f);
            }
        }

        // if (holder.txtView_Grid_Views_Gallery != null) {
        //
        // }
        holder.imgView_Grid_PlayVideo = (ImageView) convertView.findViewById(R.id.imgView_Grid_PlayVideo);
        if (holder.imgView_Grid_PlayVideo != null) {

            if (dataArray.get(position).get("filetype").toString().equals("1")) {
                holder.imgView_Grid_PlayVideo.setVisibility(View.GONE);
            } else {
                holder.imgView_Grid_PlayVideo.setVisibility(View.VISIBLE);
            }
        }

        return convertView;

    }

    class Holder {
        ImageView imgView_Grid_PlayVideo;
        ImageView imgView_Grid_Thumbnail_Gallery;
        TextView txtView_Grid_Views_Gallery;
        TextView txtView_Grid_Name_Gallery;
        ImageView imgView_GridItem_Gallery;
    }

}

关于Android-如何在 ListView 的页脚添加 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11896964/

相关文章:

android - 非拉伸(stretch) ScrollView

ios - 如何为不占用整个父 View 的 ScrollView 设置约束

android - 为什么在 Activity 开始时软键盘显示或不显示?

java - 就内存和时间而言,分配给字符串变量是否成本高昂?

android - Xcode 与 Android 的 React Native 模块冲突

php - 如何在Gridview Yii2中的条形码生成器中传递 "elementId"?

ASP.NET Gridview 列没有 ID?

asp.net gridview 分页

安卓图像按钮

Android gradle 构建失败,出现异常