简体   繁体   English

如何改善填充ListView?

[英]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. 我知道几种用标题和图标填充Android ListView对象的方法,但是在尝试简化和改进代码时感到卡住了。

This is the scenario: 这是方案:

1 - Creating two arrays 1-创建两个数组

在此处输入图片说明

2 - Creating a RowItem class 2-创建RowItem类

  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 3-创建一个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 4-创建行布局

<?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 5-在片段内创建列表

@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. 我通常使用您的方式来管理ListView。 Probably you know this but give a look at this page http://developer.android.com/training/improving-layouts/smooth-scrolling.html 也许您知道这一点,但是请看一下此页面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. 您的代码可能在滚动ListView期间频繁调用findViewById(),这可能会降低性能。 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. 重复使用findViewById()的一种方法是使用“视图持有者”设计模式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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