2016-05-27 51 views
0

我有一個應用程序在列表視圖中顯示一些本地圖像。當我運行應用logcat說在列表視圖中顯示圖像:應用程序可能在其主線程上做了太多工作

跳過43幀!應用程序可能在其主線程上做了太多工作。

任何建議將有幫助,謝謝!

下面是代碼

MainActivity.java

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    ListView list; 
    String[] web = { 
      "Google Plus", 
      "Twitter", 
      "Tall", 
    }; 

    Integer[] imageId = { 
      R.drawable.gggggggggg, 
      R.drawable.hhhhhhhhhh, 
      R.drawable.ttttttttttt 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     CustomList adapter = new 
       CustomList(MainActivity.this, web, imageId); 
     list=(ListView)findViewById(R.id.list); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show(); 

      } 
     }); 

    } 
} 

這裏是CustomList.java

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class CustomList extends ArrayAdapter<String>{ 

    private final Activity context; 
    private final Integer[] imageId; 
    public CustomList(Activity context, 
         String[] web, Integer[] imageId) { 
     super(context, R.layout.list_single, web); 
     this.context = context; 
     this.imageId = imageId; 

    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 

     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView= inflater.inflate(R.layout.list_single, null, true); 


     ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
     imageView.setImageResource(imageId[position]); 

     return rowView; 

    } 
} 

回答

1

1)使用viewHolder保存所有.findViewById - 堂妹這是爲Android繁重的操作現在它總是調用這個,而你正在滾動。

2)對於加載圖像更好使用ImageLoaderVolley庫它有緩存的圖像,你會節省很多ram。

3)點擊項目在適配器中組織界面,然後在你的活動中實現它,所以點擊你可以在你的活動中調用你的界面和動作。

相關問題