GridView的滚动速度在停止时很慢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridView的滚动速度在停止时很慢相关的知识,希望对你有一定的参考价值。
在我当前应用程序的一个片段中,listview中有三个列表,每个列表显示一个GridView,在GridView中有大约8到15个imageViews作为项目。所以我创建适配器A和适配器B.适配器A充当ListView和表示ListView和每个GridView的标题的字符串之间的桥梁。适配器B连接GridView及其ImageView(8~15)。因此,在适配器A的getView方法中,我插入了创建适配器B并连接到相应gridView的代码。从适配器中调用适配器的原因(当然我个人的猜测)是数据和图像很好地连接到gridView,即使它连接到ListView,GridView的滚动速度也很慢好像滚动停止了。
我的问题是
1]我想知道为什么gridView的滚动速度在停止时很慢。
- 就个人而言,我认为这不是因为图像的大小。 (这是因为GridView的滚动速度与图像输入图像视图之前的滚动速度完全相同。)
2]解决方案是什么?
- 我尝试使用AsyncTask加载图像。但我的图像是在一个drawable文件夹(int变量),我不知道如何加载它们(例如,int mimageResource = R.drawable.image)尝试时,图像仍然有空图像项目当GridView打开时, GridView的速度在那时很相似,所以我想知道AsyncTask是否是一个解决方案。
我非常感谢你的建议。
---------------------------这些是我的代码------------------- --------------
1] SiteAdapter.java(链接gridview和item)
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(链接list_title(String),GridView到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] OfficialFragment.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] SiteHolder.java(ViewHolder包含GridView项)
public class SiteViewHolder {
public ImageView icon;
public TextView icon_name;
public int position;
}
没有任何代码,很难帮助你。但是,您必须拆分“加载”和“显示”过程。您的适配器应仅保存/显示文本/数字数据,并将图像保留为空/空。然后在“Adapter.getView()”中你必须创建一个AsyncTask,它从你的drawable文件夹中提取图像(在它的“doInBackground()”方法中添加它),然后把它放在相对的ImageView中(在它的“在ListView的右行位置的onPostExecute()“方法”。
以上是关于GridView的滚动速度在停止时很慢的主要内容,如果未能解决你的问题,请参考以下文章
SAP migo这个事物代码在做操作的时候,多人操作同一个事务代码时很慢很慢,是啥原因?