【自定义控件】 ——- GridView

    <cn.com.karl.view.MyGridView
        android:id="@+id/bookShelf"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/head"
        android:cacheColorHint="#00000000"
        android:columnWidth="90.0dip"
        android:fadingEdge="none"
        android:horizontalSpacing="5dp"
        android:listSelector="#00000000"
        android:numColumns="3"
        android:scrollbars="none"
        android:verticalSpacing="20dp" />

 

public class MyGridView extends GridView {

    private Bitmap background;

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        background = BitmapFactory.decodeResource(getResources(),
                R.drawable.bookshelf_layer_center);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int count = getChildCount();
        int top = count > 0 ? getChildAt(0).getTop() : 0;
        int backgroundWidth = background.getWidth();
        int backgroundHeight = background.getHeight() + 2;
        int width = getWidth();
        int height = getHeight();

        for (int y = top; y < height; y += backgroundHeight) {
            for (int x = 0; x < width; x += backgroundWidth) {
                canvas.drawBitmap(background, x, y, null);
            }
        }
        super.dispatchDraw(canvas);
    }

}

 

public class CopyOfBookShelfActivity extends BaseActivity {
    private GridView bookShelf;
    private int[] data = { R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt, R.drawable.cover_txt,
            R.drawable.cover_txt, R.drawable.cover_txt

    };
    private String[] name = { "天龙八部", "搜神记", "水浒传", "黑道悲情" };
    private GridView gv;
    private Button iv;
    private List<ResolveInfo> apps;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        bookShelf = (GridView) findViewById(R.id.bookShelf);
        ShlefAdapter adapter = new ShlefAdapter();
        bookShelf.setAdapter(adapter);
        bookShelf.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                if (arg2 >= data.length) {

                } else {
                    Toast.makeText(getApplicationContext(), "" + arg2,
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        loadApps();
        gv = (GridView) findViewById(R.id.allApps);
        iv = (Button) findViewById(R.id.imageViewIcon);
        gv.setAdapter(new GridAdapter());
    }

    class ShlefAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.length + 5;
        }
        @Override
        public Object getItem(int arg0) {
            return arg0;
        }
        @Override
        public long getItemId(int arg0) {
            return arg0;
        }
        @Override
        public View getView(int position, View contentView, ViewGroup arg2) {
            contentView = LayoutInflater.from(getApplicationContext()).inflate(
                    R.layout.item1, null);
            TextView view = (TextView) contentView
                    .findViewById(R.id.imageView1);
            if (data.length > position) {
                if (position < name.length) {
                    view.setText(name[position]);
                }
                view.setBackgroundResource(data[position]);
            } else {
                view.setBackgroundResource(data[0]);
                view.setClickable(false);
                view.setVisibility(View.INVISIBLE);
            }
            return contentView;
        }

    }

    private void loadApps() {
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        apps = getPackageManager().queryIntentActivities(intent, 0);
    }

    public class GridAdapter extends BaseAdapter {
        public GridAdapter() {
        }

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

        public Object getItem(int position) {
            return apps.get(position);
        }

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

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

            ImageView imageView = null;
            if (convertView == null) {
                imageView = new ImageView(CopyOfBookShelfActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
            } else {
                imageView = (ImageView) convertView;
            }
            ResolveInfo ri = apps.get(position);
            imageView.setImageDrawable(ri.activityInfo
                    .loadIcon(getPackageManager()));
            return imageView;
        }
    }

}

 

完整代码下载地址:http://download.csdn.net/detail/androidsj/5336085

 

 

 

 

 

 

 

 

 

 

 

 

« »

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *