首页 > 代码库 > Android:日常学习笔记(8)———探究UI开发(5)

Android:日常学习笔记(8)———探究UI开发(5)

Android:日常学习笔记(8)———探究UI开发(5)

ListView控件的使用

ListView的简单用法

public class MainActivity extends AppCompatActivity {
    private String[] data=http://www.mamicode.com/{"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry",
"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry",
"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> adapter = new ArrayAdapter<String>
(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); } }

说明:

  ArrayAdapter通过泛型来指定要适配的数据类型,然后在构造函数中把当前上下文、ListView子项布局的ID、要适配的数据传入。

  我们在这里使用了安卓的内置布局文件。

  ListView设置适配器,setAdapter(),将构建好的适配器对象传进去,以此建立数据与ListView之间的关系。

 

定制ListView界面

1.定义一个实体类

public class Fruit {
    private String name;
    private int imageID;

    public Fruit(String name, int imageID) {
        this.name = name;
        this.imageID = imageID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageID() {
        return imageID;
    }

    public void setImageID(int imageID) {
        this.imageID = imageID;
    }
}

2.自定义子项布局文件

<?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">
    <ImageView
        android:id="@+id/fruit_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"/>
</LinearLayout>

3.编写适配类

public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourceID;

    public FruitAdapter(@NonNull Context context, @LayoutRes int resource, @IdRes int textViewResourceId, @NonNull List<Fruit> objects) {
        super(context, resource, textViewResourceId, objects);
        resourceID = textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Fruit fruit = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceID,parent,false);
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_img);
        TextView fruitText = (TextView) view.findViewById(R.id.fruit_text);
        fruitImage.setImageResource(fruit.getImageID());
        fruitText.setText(fruit.getName());
        return view;
    }
}

4.应用

public class MainActivity extends AppCompatActivity {
    private List<Fruit> fruitList=new ArrayList<>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.activity_main,R.layout.entry_fruit_item,fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }

    public void initFruits()
    {
        for(int i=0;i<100;i++)
        {
            fruitList.add(new Fruit("XXX",R.drawable.next_24px));
        }
    }
}

 

 

 

Android:日常学习笔记(8)———探究UI开发(5)