首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有适配器回收电池的android gridview header解决方案

具有适配器回收电池的android gridview header解决方案
EN

Stack Overflow用户
提问于 2013-05-31 23:31:05
回答 4查看 5.1K关注 0票数 3

我已经看到了许多关于如何实现与网格的其余部分一起滚动的网格视图标题的解决方案。

它们中的大多数都是在滚动视图中创建一个列表布局或相对布局,其中包含一个标题视图和一个网格视图。这个解决方案有一个问题,滚动视图不知道网格的大小,所以为了克服这个问题,你需要像这样扩展网格视图:https://stackoverflow.com/a/4536955/751180

但问题是,这样做会迫使网格视图在不回收单元的情况下一次呈现所有项目。这可能会导致应用程序崩溃,因为大量的内存使用,特别是当视图包含图像时。

其他人正在使用Listviews,并根据屏幕大小计算可以放置多少列。我个人希望继续使用网格视图。

有没有人用不同的方法实现过网格视图标题?

EN

回答 4

Stack Overflow用户

发布于 2013-08-22 20:43:28

我花了很多时间尝试将正确的头设置为GridView。没有成功。似乎,实现自定义GridView (继承自ListView)是唯一合理的方法。下面是一个包含页眉和页脚的GridView示例:https://github.com/SergeyBurish/HFGridView

票数 4
EN

Stack Overflow用户

发布于 2013-06-24 18:38:51

你可以尝试使用下面的库https://github.com/maurycyw/HeaderGridView,它可能会有帮助。

票数 3
EN

Stack Overflow用户

发布于 2015-03-06 15:15:26

我做了自定义网格视图与头图像从sdcard位置源。我的整个项目,我放在下面。如果这对任何人都有帮助,请投票给我,以提高声誉。

代码语言:javascript
复制
public class MainActivity extends Activity {
    private File file;

    LinearLayout linear;
    ArrayList<Bitmap> listbitmap;
    String[] folderlist ;
    ExpandableHeightGridView gridview;
    LinearLayout linearhere;
    String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Hi";

    ArrayList<String> mylist = new ArrayList<String>();


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linear = (LinearLayout) findViewById(R.id.linear);
        File imagesSource = new File(imageInSD);
        File[] dictoryFiles = imagesSource.listFiles();

        for(int i =0; i<dictoryFiles.length;i++){
            mylist.add(dictoryFiles[i].getName());
        }

        folderlist = mylist.toArray(new String[mylist.size()]);

        linearhere = new LinearLayout(getApplicationContext());
        linearhere.setOrientation(LinearLayout.VERTICAL);
        for (int k = 0; k < folderlist.length; k++) {



            TextView textview = new TextView(getApplicationContext());
            textview.setTextColor(Color.BLACK);
            textview.setTextSize(20);
            textview.setText(folderlist[k]);

            gridview = new ExpandableHeightGridView(getApplicationContext());
            gridview.setId(Calendar.SECOND);

            gridview.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
            gridview.setBackgroundColor(Color.WHITE);
            gridview.setNumColumns(3);
            gridview.setColumnWidth(GridView.AUTO_FIT);
            gridview.setVerticalSpacing(5);
            gridview.setHorizontalSpacing(5);
            gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);


            listbitmap = new ArrayList<Bitmap>();
            file = new File(imageInSD + "/" + folderlist[k]);
            File list[] = file.listFiles();

            for (int i = 0; i < list.length; i++) {

                String sp=list[i].getName();
                //if(sp.startsWith("123")){
                    //textview.setText(folderlist[k]);
                File image = new File(imageInSD + "/"+folderlist[k]+"/" + list[i].getName());

                InputStream fis = null;
                try {
                    fis = new FileInputStream(image.getAbsolutePath());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                Bitmap bm = BitmapFactory.decodeStream(fis, null, options);
                listbitmap.add(bm);
                //}
            }
            gridview.setAdapter(new CustomGridAdapter(getApplicationContext(),
                    listbitmap));
            gridview.setExpanded(true);

            linearhere.addView(textview);
            linearhere.addView(gridview);

        }

        linear.addView(linearhere);

    }

}

======================
class2
======================

public class ExpandableHeightGridView extends GridView
{

    boolean expanded = false;

    public ExpandableHeightGridView(Context context)
    {
        super(context);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

======================
class 3:
======================
public class CustomGridAdapter extends BaseAdapter {

    private Context context;
    private final ArrayList<Bitmap> gridValues;

    // Constructor to initialize values
    public CustomGridAdapter(Context context, ArrayList<Bitmap> gridValues) {

        this.context = context;
        this.gridValues = gridValues;
    }

    @Override
    public int getCount() {

        // Number of times getView method call depends upon gridValues.length
        return gridValues.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int id) {

        return id;
    }

    // Number of times getView method call depends upon gridValues.length

    public View getView(int position, View convertView, ViewGroup parent) {

        // LayoutInflator to call external grid_item.xml file

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from grid_item.xml ( Defined Below )

            gridView = inflater.inflate(R.layout.grid_item, null);

            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            imageView.setImageBitmap(gridValues.get(position));

        } else {

            gridView = (View) convertView;
        }

        return gridView;
    }
}
=========================
activity_main.xml Code:
=========================

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#D8D8D8"

                android:orientation="vertical" >

            </LinearLayout>


</ScrollView>

==========================
grid_item.xml code:
==========================

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="180dp"
        android:layout_margin="5dp"
        android:layout_height="150dp"
        >
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp" >

    </LinearLayout>

</LinearLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16860872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档