ScrollView 上的 GridView(应用程序在其主线程上做了太多工作)
GridView on ScrollView (application doing too much work on its main thread)
我有一个位于 scrollView 中的 gridview (expandableHeightGridView)。
当 gridview 包含 4 张图片时,它工作正常。但是,如果我再添加 2 个,它就会变得反应迟钝或非常迟钝。出现一条日志消息,说我在主线程上做了太多工作。我试图创建 asyncTask 来处理一些工作,但似乎没有任何效果。
这是我的主class:
public class MainActivity extends GeneralActivity {
private ExpandableHeightGridView gv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
gv = (ExpandableHeightGridView) findViewById(R.id.MainActivity_gv);
gv.setExpanded(true);
GridAdapter adapter = new GridAdapter(this);
gv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void onClickDestaques1(View v) {
Log.v("LOG", "numero 1");
}
public void onClickDestaques2(View v){
Log.v("LOG", "numero 2");
}
public void onClickDestaques3(View v){
Log.v("LOG", "numero 3");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
Intent intent;
switch (item.getItemId()) {
case R.id.pontos:
intent = new Intent(MainActivity.this, HistoricoDePontosActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.info:
intent = new Intent(MainActivity.this, AlterarInfoActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.senha:
intent = new Intent(MainActivity.this, AlterarSenhaActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.config:
intent = new Intent(MainActivity.this, ConfiguracoesActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.ajuda:
intent = new Intent(MainActivity.this, AjudaActivity.class);
MainActivity.this.startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
主要xmlclass:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_padding" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/main_ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CPF: 947665665-45"
android:textColor="#846248"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Saldo: 150 pts"
android:textColor="#153515"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bem-vindo, GUSTAVO"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aqui esta escrito algo interessante mas totalmente irrelevante." />
</LinearLayout>
<com.br.ExpandableHeightGridView
android:id="@+id/MainActivity_gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#0266c8"
android:gravity="center"
android:text="Destaques"
android:textColor="#ffffff"
android:textSize="25sp" >
</TextView>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_padding"
android:adjustViewBounds="true"
android:onClick="onClickDestaques1"
android:scaleType="centerCrop"
android:src="@drawable/icone1land" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_padding"
android:onClick="onClickDestaques2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/icone1land" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_padding"
android:onClick="onClickDestaques3"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/icone1land" />
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="@dimen/activity_padding"
android:textSize="200sp" >
</TextView>
</LinearLayout>
</ScrollView>
这是 GridAdapter class:
class GridAdapter extends BaseAdapter {
private Context context;
private ArrayList<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public GridAdapter(Context context) {
inflater = LayoutInflater.from(context);
this.context = context;
items.add(new Item(R.drawable.icone1land, JunteActivity.class));
items.add(new Item(R.drawable.icone2land, TroqueActivity.class));
items.add(new Item(R.drawable.icone3land, DeliveryActivity.class));
items.add(new Item(R.drawable.icone4land, EventosActivity.class));
items.add(new Item(R.drawable.icone5land, ReservasActivity.class));
items.add(new Item(R.drawable.icone6land, ContatosActivity.class));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view == null) {
view = inflater.inflate(R.layout.activity_inflater_gridview, viewGroup, false);
holder = new ViewHolder();
holder.img = (ImageView) view.findViewById(R.id.picture);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final Item item = (Item) getItem(i);
holder.img = (ImageView) view.findViewById(R.id.picture);
holder.img.setImageResource(item.drawableId);
holder.img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, item.activityClass);
context.startActivity(intent);
}
});
return view;
}
static class ViewHolder {
ImageView img;
}
}
可扩展网格class:
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context) {
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
和 squareImageView class:
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
请帮助我。我在这个问题上停留了一段时间。无法用关于此事的现有问题解决我的问题,因此我需要一个更可定制的答案。谢谢!
图片太大,因此我收到了这些错误消息。我调整了图像大小,突然一切都开始正常工作了!感谢@tyczj 的评论
我有一个位于 scrollView 中的 gridview (expandableHeightGridView)。
当 gridview 包含 4 张图片时,它工作正常。但是,如果我再添加 2 个,它就会变得反应迟钝或非常迟钝。出现一条日志消息,说我在主线程上做了太多工作。我试图创建 asyncTask 来处理一些工作,但似乎没有任何效果。
这是我的主class:
public class MainActivity extends GeneralActivity {
private ExpandableHeightGridView gv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
gv = (ExpandableHeightGridView) findViewById(R.id.MainActivity_gv);
gv.setExpanded(true);
GridAdapter adapter = new GridAdapter(this);
gv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void onClickDestaques1(View v) {
Log.v("LOG", "numero 1");
}
public void onClickDestaques2(View v){
Log.v("LOG", "numero 2");
}
public void onClickDestaques3(View v){
Log.v("LOG", "numero 3");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
Intent intent;
switch (item.getItemId()) {
case R.id.pontos:
intent = new Intent(MainActivity.this, HistoricoDePontosActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.info:
intent = new Intent(MainActivity.this, AlterarInfoActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.senha:
intent = new Intent(MainActivity.this, AlterarSenhaActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.config:
intent = new Intent(MainActivity.this, ConfiguracoesActivity.class);
MainActivity.this.startActivity(intent);
return true;
case R.id.ajuda:
intent = new Intent(MainActivity.this, AjudaActivity.class);
MainActivity.this.startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
主要xmlclass:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_padding" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/main_ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="CPF: 947665665-45"
android:textColor="#846248"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Saldo: 150 pts"
android:textColor="#153515"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bem-vindo, GUSTAVO"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aqui esta escrito algo interessante mas totalmente irrelevante." />
</LinearLayout>
<com.br.ExpandableHeightGridView
android:id="@+id/MainActivity_gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#0266c8"
android:gravity="center"
android:text="Destaques"
android:textColor="#ffffff"
android:textSize="25sp" >
</TextView>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_padding"
android:adjustViewBounds="true"
android:onClick="onClickDestaques1"
android:scaleType="centerCrop"
android:src="@drawable/icone1land" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_padding"
android:onClick="onClickDestaques2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/icone1land" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_padding"
android:onClick="onClickDestaques3"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/icone1land" />
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="@dimen/activity_padding"
android:textSize="200sp" >
</TextView>
</LinearLayout>
</ScrollView>
这是 GridAdapter class:
class GridAdapter extends BaseAdapter {
private Context context;
private ArrayList<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public GridAdapter(Context context) {
inflater = LayoutInflater.from(context);
this.context = context;
items.add(new Item(R.drawable.icone1land, JunteActivity.class));
items.add(new Item(R.drawable.icone2land, TroqueActivity.class));
items.add(new Item(R.drawable.icone3land, DeliveryActivity.class));
items.add(new Item(R.drawable.icone4land, EventosActivity.class));
items.add(new Item(R.drawable.icone5land, ReservasActivity.class));
items.add(new Item(R.drawable.icone6land, ContatosActivity.class));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view == null) {
view = inflater.inflate(R.layout.activity_inflater_gridview, viewGroup, false);
holder = new ViewHolder();
holder.img = (ImageView) view.findViewById(R.id.picture);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final Item item = (Item) getItem(i);
holder.img = (ImageView) view.findViewById(R.id.picture);
holder.img.setImageResource(item.drawableId);
holder.img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, item.activityClass);
context.startActivity(intent);
}
});
return view;
}
static class ViewHolder {
ImageView img;
}
}
可扩展网格class:
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context) {
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
和 squareImageView class:
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
请帮助我。我在这个问题上停留了一段时间。无法用关于此事的现有问题解决我的问题,因此我需要一个更可定制的答案。谢谢!
图片太大,因此我收到了这些错误消息。我调整了图像大小,突然一切都开始正常工作了!感谢@tyczj 的评论