Android_GridView
gridview与listview大同小异,gridview呈现的是九宫格的布局,listview呈现的是列表布局
gridview的一些特殊属性:
1.android:numColumns=”auto_fit” //GridView的列数设置为自动
2.android:columnWidth=”90dp " //每列的宽度,也就是Item的宽度
3.android:stretchMode=”columnWidth"//缩放与列宽大小同步
4.android:verticalSpacing=”10dp” //两行之间的边距
5.android:horizontalSpacing=”10dp” //两列之间的边距
6.android:cacheColorHint="#00000000" //去除拖动时默认的黑色背景
7.android:listSelector="#00000000" //去除选中时的黄色底色
8.android:scrollbars="none" //隐藏GridView的滚动条
9.android:fadeScrollbars="true" //设置为true就可以实现滚动条的自动隐藏和显示
10.android:fastScrollEnabled="true" //GridView出现快速滚动的按钮(至少滚动4页才会显示)
11.android:fadingEdge="none" //GridView衰落(褪去)边缘颜色为空,缺省值是vertical。(可以理解为上下边缘的提示色)
12.android:fadingEdgeLength="10dip" //定义的衰落(褪去)边缘的长度
13.android:stackFromBottom="true" //设置为true时,你做好的列表就会显示你列表的最下面
14.android:transcriptMode="alwaysScroll" //当你动态添加数据时,列表将自动往下滚动最新的条目可以自动滚动到可视范围内
15.android:drawSelectorOnTop="false" //点击某条记录不放,颜色会在记录的后面成为背景色,内容的文字可见(缺省为false)
Demo环节:
一般步骤:gridview布局 --> item布局 -->数据源--> 适配器(将数据与item绑定) --> 为gridview添加适配器
使用gridview加载网络图片:
主布局xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android:id="@+id/one_gradView" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="70dp" android:numColumns="auto_fit" android:verticalSpacing="20dp" android:horizontalSpacing="20dp" android:stretchMode="columnWidth" android:gravity="center"/> </RelativeLayout>
item布局
<?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="vertical" > <ImageView android:id="@+id/three_img" android:layout_width="150dp" android:layout_height="150dp" /> </LinearLayout>
数据源:通过异步下载图片获取数据
//网络图片地址 private void initData() { imgList = new ArrayList<>(); imgList.add("http://img5.duitang.com/uploads/item/201406/26/20140626164837_dzKds.jpeg"); imgList.add("http://img2.imgtn.bdimg.com/it/u=3980629563,3881837630&fm=21&gp=0.jpg"); imgList.add("http://img5q.duitang.com/uploads/item/201505/08/20150508155052_XJaNW.jpeg"); imgList.add("http://img4.duitang.com/uploads/item/201407/02/20140702105736_FdN5P.jpeg"); imgList.add("http://img2.imgtn.bdimg.com/it/u=2866652161,3841912673&fm=21&gp=0.jpg"); imgList.add("http://img4.imgtn.bdimg.com/it/u=883757693,2063816225&fm=21&gp=0.jpg"); imgList.add("http://cdn.duitang.com/uploads/item/201309/26/20130926110955_QtUdX.jpeg"); imgList.add("http://zjimg.5054399.com/allimg/160815/14_160815161625_9.jpg"); imgList.add("http://i-7.vcimg.com/trim/09ce7067d2467c54cf05bbd271ee3ec8430415/trim.jpg"); } //下载网络图片 public Bitmap downImg(String urlPath){ try { URL url = new URL(urlPath); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(6000); urlConnection.setDoInput(true); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); return bitmap; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //创建AsycnTask加载图片 public class MyAsycnTask extends AsyncTask<String,String,String>{ ThreeAdapter threeAdapter; public MyAsycnTask(ThreeAdapter threeAdapter) { this.threeAdapter = threeAdapter; } @Override protected String doInBackground(String... strings) { for (int i = 0; i <imgList.size() ; i++) { bitmapList.add(downImg(imgList.get(i))); publishProgress(); } return null; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); //通知适配器更新数据 threeAdapter.notifyDataSetChanged(); } }
适配器:
public class ThreeAdapter extends BaseAdapter { private Context context; private List<Bitmap> bitmapList; public ThreeAdapter(Context context,List<Bitmap> bitmapList1) { this.context = context; this.bitmapList = bitmapList1; } @Override public int getCount() { return bitmapList.size(); } @Override public Object getItem(int i) { return bitmapList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { viewHodler viewHodler = null; if (view == null){ view = View.inflate(context, R.layout.three_item,null); viewHodler = new viewHodler(); viewHodler.imageView = view.findViewById(R.id.three_img); view.setTag(viewHodler); }else { viewHodler = (ThreeAdapter.viewHodler) view.getTag(); } //如果图片无法加载则显示默认图片 if (bitmapList.get(i) == null) { viewHodler.imageView.setImageResource(R.drawable.ic_launcher_background); }else { viewHodler.imageView.setImageBitmap(bitmapList.get(i)); } return view; } //辅助类,优化缓存 public class viewHodler{ ImageView imageView; } }
主要实现
public class ThreeActivity extends AppCompatActivity { private List<Bitmap> bitmapList;//存放图片 protected List<String> imgList;//存放图片网络地址 protected GridView gridView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_layout); gridView = findViewById(R.id.scond_graidView); bitmapList = new ArrayList<>(10); initData(); ThreeAdapter threeAdapter = new ThreeAdapter(this, bitmapList); MyAsycnTask myAsycnTask = new MyAsycnTask(threeAdapter); myAsycnTask.execute(); gridView.setAdapter(threeAdapter); } }
获取系统应用信息:
自定义应用信息类AppInfo:
/** * 获取应用信息类 */ public class AppInfo { //应用名称 private String appName; //应用包名 private String packgeName; //版本名称 private String versionName; //版本号 private int versionCode; //应用图标 private Drawable appIcon; public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getPackgeName() { return packgeName; } public void setPackgeName(String packgeName) { this.packgeName = packgeName; } public String getVersionName() { return versionName; } public void setVersionName(String versionName) { this.versionName = versionName; } public int getVersionCode() { return versionCode; } public void setVersionCode(int versionCode) { this.versionCode = versionCode; } public Drawable getAppIcon() { return appIcon; } public void setAppIcon(Drawable appIcon) { this.appIcon = appIcon; } }
获取应用信息方法:
/** * 获取app信息方法 */ public List<AppInfo> getAppList(){ List<AppInfo> list = new ArrayList<>(); //获取包管理器 PackageManager packageManager = getPackageManager(); //获取包信息集合 List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0); for (int i = 0; i <installedPackages.size() ; i++) { PackageInfo packageInfo = installedPackages.get(i); //实例化AppInfo AppInfo appInfo = new AppInfo(); //获取应用名称 appInfo.setAppName(packageInfo.applicationInfo.loadLabel(packageManager).toString()); //获取应用图标 appInfo.setAppIcon(packageInfo.applicationInfo.loadIcon(packageManager)); //获取包名 appInfo.setPackgeName(packageInfo.packageName); //获取版本号 appInfo.setVersionCode(packageInfo.versionCode); //获取版本名称 appInfo.setVersionName(packageInfo.versionName); //判断是否为用户自定义安装程序 if ((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0){ list.add(appInfo); } } return list; }
总体来说gridview的用法与listview差不多,创建布局,获取数据源,通过适配器将数据源与布局绑定在显示在页面上