CẬP NHẬT 2011-08-29 Nếu tôi xóa hình ảnh trong NodePickup, độ trễ sẽ biến mất.ListView rất chậm khi cuộn (sử dụng ViewHolder/tái chế)
Câu hỏi đặt ra là - tại sao?
----
Tôi lại cố gắng ra một số Android Dev lần nữa. Tôi có một điện thoại HTC Hero "cũ" nằm xung quanh, vì vậy tôi đã khởi động cái đó lên, đã thực hiện một số cập nhật và hiện đang chạy n lần nữa với Eclipse và phần còn lại.
Tôi có Android 2.1 đang chạy trên thiết bị.
Tôi đã thực hiện một ứng dụng thử nghiệm rất đơn giản mà không làm gì cả, ngoại trừ việc hiển thị một số Hoạt động và như vậy. Mặc dù không có kết nối cơ sở dữ liệu, không có dữ liệu nào được tìm nạp từ bất kỳ mạng nào ứng dụng rất chậm. Rất chậm.
Ví dụ: tôi có một ListView với một số mục bố cục tùy chỉnh. Khi chỉ thêm 6-7 mục (để tôi có được cuộn) nó là cực kỳ chậm khi di chuyển. Ngoài ra, tôi có một số nút mà thay đổi hoạt động và cũng có nghĩa là rất rất chậm:
mButtonListenerUPP = new OnClickListener() {
@Override
public void onClick(View v)
{
Intent myIntent = new Intent(BaseActivity.this, ActivityMain.class);
BaseActivity.this.startActivity(myIntent);
}
};
tôi không thể tìm ra lý do tại sao, vì vậy Im chỉ đăng mã ở đây và hy vọng rằng ai đó có một số tip cho tôi =)
Thx!
Các Adapter, NodeRowAdapter
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class NodeRowAdapter extends ArrayAdapter
{
private Activity context;
private ArrayList<Node> mList;
private LayoutInflater inflater;
NodeRowAdapter(Activity context, ArrayList<Node> objects)
{
super(context, R.layout.nodepickup, objects);
this.context=context;
mList = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
class ViewHolder
{
TextView name;
TextView time;
TextView road;
Node node;
}
public Node getNode(int position)
{
if (mList != null && mList.size() > position)
return mList.get(position);
else
return null;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
ViewHolder holder;
if (view == null)
{
view = inflater.inflate(R.layout.nodepickup, parent, false);
holder = new ViewHolder();
holder.name =(TextView)view.findViewById(R.id.name);
holder.time =(TextView)view.findViewById(R.id.time);
holder.road =(TextView)view.findViewById(R.id.road);
view.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Node node = mList.get(position);
holder.name.setText(node.name);
holder.time.setText(node.time);
holder.road.setText(node.road);
return(view);
}
}
Các hoạt động chính, ActivityMain
public class ActivityMain extends BaseActivity
{
private NodeRowAdapter _nodeRowAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SICApplication._myContext = this;
SICApplication._myContext = this;
_nodeRowAdapter = new NodeRowAdapter(this, SICApplication.dataHolder_activityMain._nodes);
ListView listView1 = (ListView) findViewById(R.id.ListViewNodes);
listView1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Node node = _nodeRowAdapter.getNode(position);
Log.v("MyApp", "Node=" + node.name);
}
});
listView1.setAdapter(_nodeRowAdapter);
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.add_item:
addNodeItem();
return true;
}
return false;
}
private void addNodeItem()
{
_nodeRowAdapter.add(new Node("Test", "asd asd ", "14:00", 1));
}
}
Các danh sách tùy chỉnh mục, NodePickup
public class NodePickup extends LinearLayout
{
public NodePickup(Context context, AttributeSet attributeSet)
{
super(context, attributeSet);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.nodepickup, this);
this.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("Ajabaja!")
.setCancelable(true)
.setPositiveButton("JA!", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
builder.show();
}
});
}
}
Và cuối cùng, các NodePickup layout XML
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="64dip"
android:orientation="horizontal"
android:background="@drawable/stateful_background"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/ImageView01"
android:layout_width="40dip"
android:layout_height="40dip"
android:src="@drawable/arrow_up_green"
android:background="@android:color/transparent">
</ImageView>
<LinearLayout
android:id="@+id/LinearLayout02"
android:background="@android:color/transparent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="14:46 (15 min)"
android:id="@+id/time"
android:textSize="12dip"
android:textColor = "#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
</TextView>
<TextView
android:text="test"
android:id="@+id/road"
android:textSize="12dip"
android:textColor = "#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
</TextView>
<TextView
android:text="test test"
android:id="@+id/name"
android:textSize="12dip"
android:textColor = "#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
</TextView>
</LinearLayout>
</LinearLayout>
Sử dụng Traceview để xác định thời gian của bạn đang được thực hiện. – CommonsWare
Thx, tôi đã thử điều đó. Didnt nói với tôi bất cứ điều gì cả. Có lẽ tôi đang thiếu somethuing. Tuy nhiên, có vẻ như đó là ImageView thực hiện điều đó. Nếu tôi gỡ bỏ nó từ NodePickup, nó nhanh chóng một lần nữa – Ted