listView.setAdapter这么迟钝吗?

SMa*_*diS 5 android listview

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

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

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

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

问题是:当我们调用listView.setAdapter(adapter)时,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;
        }
   };
Run Code Online (Sandbox Code Playgroud)

这是我的适配器:

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;
Run Code Online (Sandbox Code Playgroud)

}}

nKn*_*nKn 1

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