我知道有几种方法可以使用标题和图标填充Android ListView
对象,但在尝试简化和改进代码时我感到困惑。
这是情景:
public class IconRow {
private String title;
private int icon;
public IconRow(String title, int icon) {
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public int getIcon() {
return icon;
}
}
public class ListAdapter extends BaseAdapter {
Context context;
List<IconRow> rowItem;
String description;
long option;
public ListAdapter(Context context, List<IconRow> rowItem, String description, long option)
{
this.context = context;
this.rowItem = rowItem;
this.description = description;
this.option = option;
}
@Override
public int getCount() {
return rowItem.size();
}
@Override
public Object getItem(int position) {
return rowItem.get(position);
}
@Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.lv_arrow, null);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageView1);
TextView txtTitle = (TextView) convertView.findViewById(R.id.textView1);
IconRow row_pos = rowItem.get(position);
// setting the image resource and title
imgIcon.setImageResource(row_pos.getIcon());
txtTitle.setText(row_pos.getTitle());
}
return convertView;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@android:color/white" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-light"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="24dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="right|center"
android:layout_marginLeft="20dp"
android:scaleType="fitEnd"
android:src="@drawable/ic_go"
android:layout_marginRight="0dp" />
</LinearLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_parameters_settings, container, false);
lv_settings_1 = (ListView) rootView.findViewById(R.id.lv_settings_1);
menutitles = getResources().getStringArray(R.array.array_lv_settings_1);
menuIcons = getResources().obtainTypedArray(R.array.arrow_icons);
menu_iconRow = new ArrayList<>();
for (int i = 0; i < menutitles.length; i++) {
IconRow items = new IconRow(menutitles[i], menuIcons.getResourceId(
i, -1));
menu_iconRow.add(items);
}
adapter_settings_1 = new ListAdapter(getActivity(), menu_iconRow, "No Description", 0);
lv_settings_1.setAdapter(adapter_settings_1);
return rootView;
}
我做错了吗?无论如何我可以简化这段代码吗?
答案 0 :(得分:3)
我通常用你的方式来管理ListView。可能你知道这一点,但请看一下这页http://developer.android.com/training/improving-layouts/smooth-scrolling.html
在滚动ListView期间,您的代码可能经常调用findViewById(),这会降低性能。即使适配器返回一个膨胀的视图以进行回收,您仍然需要查找元素并更新它们。围绕重复使用findViewById()的方法是使用&#34;视图持有者&#34;设计模式。
static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}