bucket-list-adapter

Android BucketListAdapter

This is an implementation of a bucket list adapter, similar to what you see in the Google Play application. It features a grid-like layout of list elements, with the advantage that you can still use list headers and footers - something that's not possible with the standard GridView.

To use the adapter, you need to override the "protected View getBucketElement(int position, T currentElement)" method. In this method, you should return the View of a bucket-element of the specified position. For convenience, you also get the current element for that position as a second parameter.

Two constructors exist for the adapter. The first, "BucketListAdapter(Activity ctx, List elements)" takes the Activity as and the elements be presented, and assumes that the elements should be presented in 1 column. The other, "BucketListAdapter(Activity ctx, List elements, Integer bucketSize)" takes the exact column number as a third parameter.

The "void enableAutoMeasure(float minBucketElementWidthDip)" method may be used to automatically calculate the required column number based on the minimum element width given as a parameter.

Here's some example code:

List<String> myListContent = new ArrayList<String>();
    myListContent.add("First");
    myListContent.add("Second");
    myListContent.add("Thrid");
    
    BucketListAdapter<String> myAdapter = new BucketListAdapter<String>(this, myListContent, 2) {
        @Override
        protected View getBucketElement(final int position, String currentElement) {
            View bucketElement = View.inflate(ctx, R.layout.bucket_element, null);
            
            TextView text = (TextView)bucketElement.findViewById(R.id.text);
            text.setText(String.valueOf(currentElement));
            
            bucketElement.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(ctx, "Clicked on: " + position, Toast.LENGTH_SHORT).show();
                }
            });
            
            return bucketElement;
        }
    };
    
    bucketList.setAdapter(myAdapter);

An demo application is also provided with the project, check it out.



bucket-list-adapter

Android BucketListAdapter

这是一个桶列表适配器的实现,类似于您在其中看到的 Google Play应用程式它具有列表元素的网格状布局 您仍然可以使用列表头文件和页脚的优点 - 这不是 可以使用标准的GridView。

要使用适配器,您需要覆盖protected View getBucketElement(int position,T currentElement) 方法。在此方法中,您应该返回指定位置的bucket-element的View。为了方便, 您还可以获得该位置的当前元素作为第二个参数。

适配器存在两个构造函数。第一个BucketListAdapter(Activity ctx,List elements)接受Activity 作为和元素被呈现,并假设元素应该在1列中呈现。 另一个,BucketListAdapter(Activity ctx,List elements,Integer bucketSize)获取准确的列号 作为第三个参数。

可以使用void enableAutoMeasure(float minBucketElementWidthDip)方法自动计算 基于作为参数给出的最小元素宽度所需的列号。

以下是一些示例代码:

List<String> myListContent = new ArrayList<String>();
    myListContent.add("First");
    myListContent.add("Second");
    myListContent.add("Thrid");

BucketListAdapter&lt;String&gt; myAdapter = new BucketListAdapter&lt;String&gt;(this, myListContent, 2) {
    @Override
    protected View getBucketElement(final int position, String currentElement) {
        View bucketElement = View.inflate(ctx, R.layout.bucket_element, null);

        TextView text = (TextView)bucketElement.findViewById(R.id.text);
        text.setText(String.valueOf(currentElement));

        bucketElement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ctx, &#34;Clicked on: &#34; + position, Toast.LENGTH_SHORT).show();
            }
        });

        return bucketElement;
    }
};

bucketList.setAdapter(myAdapter);

项目中还提供演示应用程序,请查看。




相关问题推荐