Android中GridView配合ImageLoader实现图文展示


效果图:




开网络权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Application中ImageLoader配置

<span style="font-size:18px;"><span style="font-size:18px;">public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        initImageLoader(getApplicationContext());

    }
    /**
     *ImageLoader的设置
     */
    public static void initImageLoader(Context context) {
        // Create default options which will be used for every
        // displayImage(...) call if no options will be passed to this method
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(false)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPoolSize(3)// default
                .threadPriority(Thread.NORM_PRIORITY - 1)// default
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSizePercentage(13) // default
                .defaultDisplayImageOptions(defaultOptions)
                .writeDebugLogs() // Remove for release app
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }
}
</span></span>

MainActivity主代码

<span style="font-size:18px;"><span style="font-size:18px;">public class MainActivity extends Activity {
    private ImageView image1;
    private GridView gv_KnowledgeList;
    ImageLoader imageLoader=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
//      得到数据
        List<KnowLedgeGridEntity> data =getData();
        ProducGridAdapter adapter = new ProducGridAdapter(data,MainActivity.this);
        gv_KnowledgeList.setAdapter(adapter);

    }

//  造数据
    private List<KnowLedgeGridEntity> getData(){
        List<KnowLedgeGridEntity> gridData=new ArrayList<KnowLedgeGridEntity>();
        for(int i = 0; i <10 ; i++) {
            KnowLedgeGridEntity entity = new KnowLedgeGridEntity();
            entity.setName("我的测试" + i);
            entity.setImgurl("http://g.hiphotos.baidu.com/image/pic/item/6c224f4a20a446230761b9b79c22720e0df3d7bf.jpg");
            gridData.add(entity);
        }

        return gridData;
    }
    private void initView() {
        gv_KnowledgeList = (GridView)findViewById(R.id.gv_KnowledgeList);
    }
    /**
     * GridView的适配器
     */
    private class ProducGridAdapter extends BaseAdapter {
        private List<KnowLedgeGridEntity> gridData;
        private LayoutInflater inflater;

        public ProducGridAdapter(List<KnowLedgeGridEntity> gridData, Context context) {
            this.gridData = gridData;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return gridData.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            imageLoader=ImageLoader.getInstance();
            ViewHolderGrid holer = null;
            View view = inflater.inflate(R.layout.item_know_gridview, null);
            if (holer == null) {
                holer = new ViewHolderGrid();
                holer.name = (TextView) view.findViewById(R.id.item_know_grid_name);
                holer.item_know_grid_img = (ImageView) view.findViewById(R.id.item_know_grid_img);
                view.setTag(holer);

            } else {
                holer = (ViewHolderGrid) view.getTag();
            }

            holer.name.setText(gridData.get(position).getName());
//            //          加载图片
           imageLoader.displayImage(gridData.get(position).getImgurl(), holer.item_know_grid_img);
            return view;
        }
        //  适配器中的GridView缓存类
         class ViewHolderGrid {
            TextView name;
            ImageView item_know_grid_img;

        }
    }
}</span></span>


布局文件

activity_main.xml

<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <GridView
        android:id="@+id/gv_KnowledgeList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="3"
        android:scrollbars="none"
        >

    </GridView>
</LinearLayout></span></span>
item_know_gridview.xml

<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/item_know_grid_img"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/item_know_grid_name"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="awdadw"
            android:textSize="18dp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout></span></span>
源码下载(jar包,在源码中):

http://download.csdn.net/detail/zhaihaohao1/9489601






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值