过年爽爽放,该回来复习复习拉WW,大家新年快乐
今天原本是要来练习以前都没接触过的gallery 的,但是Android 4.1 的版本以後,已经不再支援。官方给出的可以用HorizontalScrollView 或者ViewPager 来代替它。
《Android Developers》
This class was deprecated in API level 16.
This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.
虽然gallery已经被标记为过时,但他的展示效果还是杠杠的
在网路上查询的结果,貌似它每次切换图片时都要新建检视造成浪费太多的资源,所以被封杀了。详细原因在最下面参考资料,为了不占版面就不放这啦。监於很多人还在使用gallery,有大神在GitHub提供了自制元件『EcoGallery』,供大家优化gallery使用。
--> 大神的github https://github.com/falnatsheh/EcoGallery <--
gallery长这样
安装libaray
因为大神没有用JitPack,安装起来比较麻烦,不能直接用dependencies(手动也不行,血泪教训QAQ)
1.下载并解压缩
2.File>>New>>Import Moudle
3.选择路径
你刚刚下载的档案,选择上面EcoGallery档案,不用EcoGallerySample
然後next-->next-->next
中间会跟你报错误讯息,没记错的话是跟你说minSDK的问题,就直接依照系统方法解决
成功後,你旁边会多一点东西
4.build.gradle(app)
最後的最後,还要在build.gradle(app)新增
implementation project(":ecoGallery")
code
1.Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<us.feras.ecogallery.EcoGallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
2.MainActivity
package com.example.ecogallary;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import us.feras.ecogallery.EcoGallery;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EcoGallery ecoGallery = findViewById(R.id.gallery);
ecoGallery.setAdapter(new ImageAdapter(this));
}
private class ImageAdapter extends BaseAdapter {
private Context context;
ImageAdapter(Context context) {
this.context = context;
}
//取得 Gallery 列表 Item 的数量
//要显示几张图片改数字
public int getCount() {
return 5;
}
//取得 Gallery 列表於 position 位置上的 Item
public Object getItem(int position) {
return position;
}
//取得 Gallery 列表於 position 位置上的 Item 的 ID
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Not using convertView for sample app simplicity.
// You should probably use it in real application to get better performance.
ImageView imageView = new ImageView(context);
int resId;
//图片切换
switch (position) {
case 0: resId = R.drawable.one;
break;
case 1: resId = R.drawable.two;
break;
case 2: resId = R.drawable.three;
break;
case 3: resId = R.drawable.two;
break;
case 4: resId = R.drawable.one;
break;
default: resId = R.drawable.one;
}
imageView.setImageResource(resId);
return imageView;
}
}
}
>>: How to fix Gmail not receiving emails on iPhone?
什麽是 Fan-Out Fan-In Pattern? 将 input 由一个 producer 分...
今天来认识一下kaggle比赛的常胜军-XGboost(extreme gradient boost...
GeoJSON 是一种基於 JSON 的地理空间数据交换格式,相关定义可参考 RFC 7946,一个...
我写的 Rails 的 R+MVC 笔记图 左上角的那个人是使用者,当使用者输入网址连上网址进入网...
在我们之前提到的Execution Context,都会执行一个被称为"Hoisting的...