listView.setAdapter是如此迟钝?

时间:2014-01-23 07:18:00

标签: android listview

我有三个可跳转的标签。在第一个标签中,我有一个包含1000多个项目的列表视图。

我尝试使用Asynctask和Runnable填充列表,我对此没有任何问题。

问题是当我尝试将适配器分配给listview时。我注意到无论适配器中有多少项,无论它是1还是1000,当它尝试执行时:listView.setAdapter(适配器),它会跳过大约30帧......

由于listView.setAdapter(适配器)用于第一个选项卡,因此每当用户尝试进入该选项卡时,UI会冻结几毫秒。虽然这不是一个严重的问题,但我不喜欢它。

问题是:这是正常的,当我们调用listView.setAdapter(适配器)时,UI会冻结几毫秒并跳过一些帧吗?无论如何都没有解决这个问题?

我清楚吗?

    Carpet_handler ch; // my db handler
ListView listview;

public ArrayList<CarpetGen> carpetGens;
CarpetAdapter adapter;
boolean loadingMore=false;
int offset;
Context ctx;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_first_page_customers, container, false);
    ch=new Carpet_handler(getActivity()); // db handler
    listview = (ListView) rootView.findViewById(R.id.listView2);
    ctx=this.getActivity();
    carpetGens = new ArrayList<CarpetGen>();
    offset=0;
    adapter = new CarpetAdapter(getActivity(), R.layout.row, carpetGens);



    listview.setAdapter(adapter); // adapter is empty first time, runnable fills it
    listview.setOnItemClickListener(myClickListener);



    //Here is where the magic happens
    listview.setOnScrollListener(new OnScrollListener(){
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {}
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
            //what is the bottom iten that is visible
            int lastInScreen = firstVisibleItem + visibleItemCount;
            //is the bottom item visible & not loading more already ? Load more !
            if((lastInScreen == totalItemCount) && !(loadingMore)){
                Thread thread =  new Thread(null, loadMoreListItems);
                thread.start();
            }
        }
    });


    return rootView;
}



   //Runnable to load the items
   private Runnable loadMoreListItems = new Runnable() {
    @Override
    public void run() {
        //Set flag so we cant load new items 2 at the same time
        loadingMore = true;
        //Reset the array that holds the new items
        carpetGens= new ArrayList<CarpetGen>();
        carpetGens.addAll(ch.getAllCustomerCarpets1(getActivity().getIntent().getIntExtra("recordid", -1), offset));
        offset+=15;
        //Done! now continue on the UI thread
        if(getActivity()==ctx)
            getActivity().runOnUiThread(returnRes);

    }
};

//Since we cant update our UI from a thread this Runnable takes care of that!
private Runnable returnRes = new Runnable() {
    @Override
    public void run() {
        //Loop thru the new items and add them to the adapter
        if(carpetGens != null && carpetGens.size() > 0){
                    for(int i=0;i < carpetGens.size();i++)
                    adapter.add(carpetGens.get(i));
                 }
                adapter.notifyDataSetChanged();
                loadingMore = false;
        }
   };

这是我的适配器:

public class CarpetAdapter extends ArrayAdapter<CarpetGen> {

private final Context context;
private final int rowResourceId;
//private final String[] Ids;
private final ArrayList<CarpetGen> Objects;

public CarpetAdapter(Context context, int textViewResourceId, ArrayList<CarpetGen> objects){//Arr[] objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.rowResourceId = textViewResourceId;
    this.Objects = objects;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View rowView = convertView;
    CarpetGen i = Objects.get(position);
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rowView = inflater.inflate(R.layout.row, parent, false);
        //TextView textView = (TextView) rowView.findViewById(R.id.textView);

        //textView.setText(i.Result_String);

        holder = new ViewHolder();
        holder.title = (TextView) rowView
                .findViewById(R.id.textView);

        rowView.setTag(holder);            
    } else {        
        holder = (ViewHolder) rowView.getTag();
    }

    holder.title.setText(i.Result_String);



    return rowView;

}
static class ViewHolder {
TextView title;

} }

2 个答案:

答案 0 :(得分:1)

我认为这是完全正常的。请记住,它必须刷新布局,即使它没有元素,当你调用setAdapter()方法时,你正在做一些取决于你的实现的操作,但基本上它不是0秒 - 奇迹。正如@laalto建议的那样,自定义适配器实现中也可能存在瓶颈,因此您可能需要考虑在Thread(或基本上是AsyncThread)中进行一些工作。但正如我所说,我不担心它(总的来说,如果在虚拟设备中发生这种跳帧)。

答案 1 :(得分:0)

我认为您应该尝试执行以下操作:

1st - 使用AsyncTask加载doInBakground方法中的项目。

2nd - doPostExecute方法中AsyncTask中的listView.setAdapter。

希望有所帮助