简体   繁体   中英

How can I improve populating my ListView?

I'm aware of a few ways to populate an Android ListView object with a title and a icon but I feel stuck when trying to simplify and improve my code.

This is the scenario:

1 - Creating two arrays

在此处输入图片说明

2 - Creating a RowItem class

  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;
  }

}

3 - Creating a ListAdapter

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;
}

4 - Creating a Row Layout

<?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>

5 - Creating the List inside Fragment

@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;
    }

Am I doing it the wrong way? Is there anyway I can simplify this code?

I usually use your way for manage ListView. Probably you know this but give a look at this page http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.

static class ViewHolder {
  TextView text;
  TextView timestamp;
  ImageView icon;
  ProgressBar progress;
  int position;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM