ListView的用法
1、在布局文件添加
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view">
</ListView>
</android.support.constraint.ConstraintLayout>
2、MainActivity中
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.myapplication.bean.MyAdapter;
import com.example.myapplication.bean.MyBean;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String data[] = {"aa", "bb", "cc", "dd", "ee"}; //构造数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, data);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
case 1:
Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
case 2:
Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
case 3:
Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
case 4:
Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
最简单用法,运行即可
3、复杂一点用法
1)自定义item布局layout_list_item.xml
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/llayout"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/text_view"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/image_view"/>
</LinearLayout>
</LinearLayout>
2)自定义数据MyBean.java
package com.example.myapplication.bean;
public class MyBean {
private String text;
private int imageId;
public MyBean(String text, int imageId) {
this.text = text;
this.imageId = imageId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
@Override
public String toString() {
return "MyBean{" +
"text='" + text + '\'' +
", imageId=" + imageId +
'}';
}
}
3)自定义MyAdapter.java 继承于 ArrayAdapter
package com.example.myapplication.bean;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.R;
import java.util.List;
public class MyAdapter extends ArrayAdapter {
private int imageId;
public MyAdapter(Context context, int resource, List<MyBean> myBeanList){
super(context, resource, myBeanList);
this.imageId = resource;
}
@Override
public View getView(final int position, @NonNull View convertView, @NonNull ViewGroup parent){
MyBean myBean = (MyBean) getItem(position);
View view = LayoutInflater.from(getContext()).inflate(imageId, parent, false);
LinearLayout linearLayout = view.findViewById(R.id.llayout);
TextView textView = view.findViewById(R.id.text_view);
ImageView imageView = view.findViewById(R.id.image_view);
textView.setText(myBean.getText());
imageView.setImageResource(myBean.getImageId());
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"You click the "+ position+" item", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
4)修改MainActivity.java
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.myapplication.bean.MyAdapter;
import com.example.myapplication.bean.MyBean;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String data[] = {"aa", "bb", "cc", "dd", "ee"};
private List<MyBean> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<MyBean>();
initData();
ListView listView = (ListView) findViewById(R.id.list_view);
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
// android.R.layout.simple_list_item_1, data);
MyAdapter arrayAdapter = new MyAdapter(MainActivity.this,
R.layout.layout_list_item, arrayList);
listView.setAdapter(arrayAdapter);
// listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// switch (position){
// case 0:
// Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
// case 1:
// Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
// case 2:
// Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
// case 3:
// Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
// case 4:
// Toast.makeText(MainActivity.this, "You click "+position+" Item!", Toast.LENGTH_SHORT).show();
// }
// }
// });
}
public void initData(){
MyBean myBean1 = new MyBean("aa", R.drawable.aa);
arrayList.add(myBean1);
MyBean myBean2 = new MyBean("bb", R.drawable.aa);
arrayList.add(myBean1);
MyBean myBean3 = new MyBean("cc", R.drawable.aa);
arrayList.add(myBean1);
MyBean myBean4 = new MyBean("dd", R.drawable.aa);
arrayList.add(myBean1);
MyBean myBean5 = new MyBean("ee", R.drawable.aa);
arrayList.add(myBean1);
}
}
演示效果