gpt4 book ai didi

android - listView.setAdapter 这么卡?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:40 25 4
gpt4 key购买 nike

我有三个可滑动的标签。在第一个选项卡中,我有一个包含 1000 多个项目的 ListView 。

我尝试使用 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;
}
};

这是我的适配器:

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;

}

最佳答案

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

关于android - listView.setAdapter 这么卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com