在我当前应用程序的一个片段中,列表视图中有三个列表,每个列表显示一个 GridView,其中大约 8 到 15 个 imageViews 作为 GridView 中的项目。因此,我制作了适配器 A 和适配器 B.适配器 A 充当 ListView 与表示列表视图标题和每个 GridView 的字符串之间的桥梁。适配器 B 将 GridView 与其 ImageView(8~15( 连接起来。因此,在适配器 A 的 getView 方法中,我插入了创建适配器 B 的代码并连接到相应的 gridView。从适配器内部调用适配器的原因是(当然还有我个人的猜测(是数据和图像很好地连接到 gridView,即使它很好地连接到 ListView,GridView 的滚动速度也很慢,就像滚动停止一样。
我的问题是
1] 我想知道为什么 gridView 的滚动速度在停止时很慢。
- 就个人而言,我不认为这是因为图像的大小。(这是因为 GridView 的滚动速度与将图像输入图像视图之前完全相同。
2] 解决方案是什么?
- 我尝试使用AsyncTask加载图像。但是我的图像位于可绘制文件夹(int 变量(中,我不知道如何加载它们(例如,int mimageResource = R.drawable.image( 尝试时,图像仍将具有空图像项 当 GridView 打开时,GridView 的速度当时相似,所以我想知道 AsyncTask 是否是一种解决方案。
我非常感谢您的建议。
---------------------------这些是我的代码---------------------------------
1] 站点适配器.java(链接网格视图和项目(
public class SiteAdapter extends ArrayAdapter<Site> {
private Site currentSite;
private final int REQUEST_WIDTH = 256;
private final int REQUEST_HEIGHT = 256;
public Context context;
public SiteAdapter(Context context, ArrayList<Site> sites) {
super(context, 0, sites);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
SiteViewHolder viewHolder;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,false);
viewHolder = new SiteViewHolder();
viewHolder.icon = (ImageView) listItemView.findViewById(R.id.image);
viewHolder.icon_name = (TextView) listItemView.findViewById(R.id.name);
listItemView.setTag(viewHolder);
}
else
viewHolder = (SiteViewHolder) listItemView.getTag();
currentSite = getItem(position);
viewHolder.position = position;
//new ThumbnailTask(position, viewHolder).execute((currentSite.getMsite_imagesource()));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getContext().getResources(),currentSite.getMsite_imagesource(),options);
options.inSampleSize = setSimpleSize(options, REQUEST_WIDTH, REQUEST_HEIGHT);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),currentSite.getMsite_imagesource(),options);
viewHolder.icon.setBackground(new ShapeDrawable(new OvalShape()));
viewHolder.icon.setClipToOutline(true);
viewHolder.icon.setImageBitmap(bitmap);
viewHolder.icon_name.setText(currentSite.getMsite_name());
return listItemView;
}
private int setSimpleSize(BitmapFactory.Options options, int requestWidth, int requestHeight){
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
int size = 1;
while(requestWidth < originalWidth || requestHeight < originalHeight){
originalWidth = originalWidth / 2;
originalHeight = originalHeight / 2;
size = size * 2;
}
return size;
}
2] ListAdapter (link list_title(String(, GridView to ListView(
public class ListAdapter extends BaseAdapter {
public Context mcontext;
public Hashtable<String, ArrayList<Site>> mdata;
public Map.Entry<String,ArrayList<Site>> copyData;
public ArrayList<String> keySet = new ArrayList<String>();
private String key;
private int num=0;
public String currentName;
private ArrayList<Site> currentSite;
private int [] count = {0,0,0};
public ListAdapter(Context context,Hashtable<String, ArrayList<Site>> data){
mcontext = context;
mdata = data;
Iterator it = data.entrySet().iterator();
while(it.hasNext()) {
copyData = (Map.Entry<String, ArrayList<Site>>) it.next();
keySet.add(copyData.getKey());
}
}
public String getKey(int position){
//Iterator it = mdata.entrySet().iterator();
//copyData = (Map.Entry<String, ArrayList<Site>>) it.next();
return keySet.get(position);
}
@Override
public int getCount() {
/*Iterator it = mdata.entrySet().iterator();
while(it.hasNext())
{
num++;
}
return num;*/
return keySet.size();
}
@Override
public ArrayList<Site> getItem(int position) {
return mdata.get(keySet.get(position));
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
final ArrayList<Site> currentSite;
ListViewHolder holder;
if(view == null) {
view = LayoutInflater.from(mcontext).inflate(R.layout.site_list, parent, false);
holder = new ListViewHolder();
holder.list_title = (TextView) view.findViewById(R.id.title_site_list);
holder.lists = (GridView) view.findViewById(R.id.gridView);
view.setTag(holder);
}
else
holder = (ListViewHolder) view.getTag();
currentName = keySet.get(position);
holder.list_title.setText(currentName);
currentSite = getItem(position);
final SiteAdapter siteAdapter = new SiteAdapter(mcontext,currentSite);
holder.lists.setAdapter(siteAdapter);
final ViewSwitcher viewSwitcher = (ViewSwitcher) view.findViewById(R.id.visibility);
holder.list_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(count[position]%2==0){
viewSwitcher.setVisibility(View.VISIBLE);
viewSwitcher.showNext();
count[position]++;
}
else if(count[position]%2==1){
viewSwitcher.setVisibility(View.GONE);
viewSwitcher.showNext();
count[position]++;
}
}
});
holder.lists.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
final Site site = currentSite.get(position);
if (site.getMsite_name() == "Gist home") {
PopupMenu popupIFWB = new PopupMenu(mcontext.getApplicationContext(), view);
popupIFWB.getMenuInflater().inflate(R.menu.insta_facebook_web_blog_popup_menu, popupIFWB.getMenu());
popupIFWB.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.webIFWB:
openWebPage(site.getMsite_urlW());
break;
case R.id.blogIFWB:
openWebPage(site.getMsite_urlB());
break;
case R.id.facebookIFWB:
openWebPage(site.getMsite_urlF());
break;
case R.id.instaIFWB:
openWebPage(site.getMsite_urlI());
break;
}
return false;
}
});
popupIFWB.show();
} else
openWebPage(site.getMsite_url());
}
});
return view;
}
private static class ListViewHolder{
public TextView list_title;
public GridView lists;
}
public void openWebPage(String url){
Uri webpage = Uri.parse(url);
Intent siteMove = new Intent(Intent.ACTION_VIEW, webpage);
if(siteMove.resolveActivity(mcontext.getPackageManager()) != null)
mcontext.startActivity(siteMove);
}
}
3] 官方片段.java(与首页相同(
public class OfficialFragment extends Fragment {
private int count = 0;
private ArrayList<String> arrayGroup = new ArrayList<String>();
private Hashtable<String, ArrayList<Site>> arrayChild = new Hashtable<String, ArrayList<Site>>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View viewMain = inflater.inflate(R.layout.listview, null);
super.onCreate(savedInstanceState);
ListView listView = (ListView) viewMain.findViewById(R.id.listView);
//GridView gridView = (GridView) viewMain.findViewById(R.id.gridView);
setArrayData();
ListAdapter listAdapter = new ListAdapter(getActivity(),arrayChild);
listView.setAdapter(listAdapter);
return viewMain;
}
public void openWebPage(String url){
Uri webpage = Uri.parse(url);
Intent siteMove = new Intent(Intent.ACTION_VIEW, webpage);
if(siteMove.resolveActivity(getActivity().getPackageManager()) != null)
startActivity(siteMove);
}
public void setArrayData(){
arrayGroup.add("공식 사이트 모음");
arrayGroup.add("조직 사이트 모음");
arrayGroup.add("동아리 사이트 모음");
ArrayList<Site> arrayOfficial = new ArrayList<Site>();
arrayOfficial.add(new Site("Gist home", "https://www.gist.ac.kr/kr/","https://blog.naver.com/gist1993","https://www.facebook.com/GIST.ac.kr/","https://www.instagram.com/gist1993/", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("Zeus system", "https://zeus.gist.ac.kr", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("Portal system", "https://portal.gist.ac.kr/intro.jsp", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("Email system", "https://mail.gist.ac.kr/loginl?locale=ko_KR", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("Gist college", "https://college.gist.ac.kr/", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("Gist library", "https://library.gist.ac.kr/", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("학내공지", "https://college.gist.ac.kr/main/Sub040203", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("GIST 창업진흥센터", "https://www.facebook.com/gistbi/?ref=py_c", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("전기전자컴퓨터공학부", "http://eecs.gist.ac.kr/", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("신소재공학부", "http://mse.gist.ac.kr", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("기계공학부", "http://me.gist.ac.kr", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("지구·환경공학부", "http://env1.gist.ac.kr", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("생명과학부", "http://life1.gist.ac.kr", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("물리·광과학과", "http://phys.gist.ac.kr/", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("화학과", "http://chem.gist.ac.kr", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("융합기술원", "http://iit.gist.ac.kr", R.drawable.ic_dashboard_black_24dp));
arrayOfficial.add(new Site("나노바이오재료전자공학과", "http://wcu.gist.ac.kr/", R.drawable.ic_dashboard_black_24dp));
ArrayList<Site> arrayOrganization = new ArrayList<Site>();
arrayOrganization.add(new Site("Gist 총학생회", "https://www.facebook.com/gistunion/", R.drawable.gistunion));
arrayOrganization.add(new Site("Gist 동아리연합회", "https://www.facebook.com/gistclubunite/", R.drawable.clubnight));
arrayOrganization.add(new Site("Gist 하우스", "https://www.facebook.com/GISTcollegeHOUSE/", R.drawable.gisthouse));
arrayOrganization.add(new Site("Gist 문화행사위원회", "https://www.facebook.com/Moonhangwe/", R.drawable.moonhangwe));
arrayOrganization.add(new Site("Gist 신문","http://gistnews.co.kr/", "https://www.facebook.com/pg/GistSinmoon/posts/", R.drawable.gistnews));
arrayOrganization.add(new Site("Gist 홍보대사", "http://blog.naver.com/PostList.nhn?blogId=gist1993&from=postList&categoryNo=28", R.drawable.gionnare));
ArrayList<Site> arrayCircle = new ArrayList<Site>();
arrayCircle.add(new Site("영화 동아리 Cinergy", "https://www.facebook.com/gistcinergy/", R.drawable.cinergy));
arrayCircle.add(new Site("영상편집 동아리 The Gist", "https://www.facebook.com/Gentletist/","https://www.youtube.com/channel/UCMUDHS0SZvQilFe5h6eI9rA/videos", R.drawable.thegist));
arrayCircle.add(new Site("문예창작 동아리 사각사각", "https://www.facebook.com/GIST-%EC%82%AC%EA%B0%81%EC%82%AC%EA%B0%81-238788459851229/", R.drawable.sagaksagak));
arrayCircle.add(new Site("gist 고양이 지냥이", "https://www.facebook.com/giscats/", R.drawable.giscat));
arrayCircle.add(new Site("요리동아리 이쑤시개", "https://www.facebook.com/%EC%9D%B4%EC%91%A4%EC%8B%9C%EA%B0%9C-%EC%9A%94%EB%A6%AC%ED%95%98%EB%8A%94-GIST%EC%83%9D-272551203239747/?ref=py_c", R.drawable.essosigae));
arrayCircle.add(new Site("칵테일 동아리 MixoloGIST", "https://www.facebook.com/Mixologist-1584231725157207", R.drawable.mixologist));
arrayCircle.add(new Site("보드게임 동아리 BGM", "https://www.facebook.com/GISTBGM", R.drawable.bgm));
arrayCircle.add(new Site("성소수자 모임 speQtrum", "https://www.facebook.com/gistspeqtrum/", R.drawable.speqtrum));
arrayCircle.add(new Site("만화 동아리 erutlucbus", "https://www.facebook.com/Erutlucbus/", R.drawable.eru));
arrayCircle.add(new Site("천체관측 동아리 SpaceBar", "https://www.facebook.com/GISTspacebar/", R.drawable.spacebar));
arrayCircle.add(new Site("전산 동아리 WING", "https://www.facebook.com/GISTWING/", R.drawable.wing));
arrayCircle.add(new Site("환경 동아리 온새미로", "https://www.facebook.com/onsaemiro123/", R.drawable.onsaemiro));
arrayCircle.add(new Site("축구 동아리 Kickass", "https://www.facebook.com/gistkickass/?ref=br_rs", R.drawable.kickass));
arrayChild.put(arrayGroup.get(0),arrayOfficial);
arrayChild.put(arrayGroup.get(1),arrayOrganization);
arrayChild.put(arrayGroup.get(2),arrayCircle);
}
}
4] 站点持有人.java(视图持有人包含网格视图的项目(
public class SiteViewHolder {
public ImageView icon;
public TextView icon_name;
public int position;
}
没有任何代码,很难帮助你。 但是,您必须拆分加载和显示过程。您的适配器应仅保存/显示文本/数字数据,并将图像保留为空/空。然后在"Adapter.getView(("中,您必须创建一个AsyncTask,该任务从可绘制文件夹中提取图像(在其"doInBackground(("方法中将其删除(,然后将其放入相对ImageView(在其"onPostExecute(("方法中执行此操作(在ListView的右行位置。