listview整理笔记
通过SimpleAdapter来实现listView
主函数代码
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goat);
init();
}
public void init(){
listView = (ListView) findViewById(R.id.listView);
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
new String[]{"title","img"},
new int[]{R.id.title,R.id.img});
listView.setAdapter(adapter);
}
private List
布局文件代码
<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="com.example.steven.testoct.MainActivity">
<LinearLayout
android:layout_width="250dp"
android:layout_height="300dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imageView"
android:layout_marginStart="100dp"
app:layout_constraintVertical_bias="0.495">
<android.support.v7.widget.ListViewCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView">
android.support.v7.widget.ListViewCompat>
LinearLayout>
android.support.constraint.ConstraintLayout>
自定义内部view片代码
vlist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
/>
LinearLayout>
这样就实现了一个基本的带图片与文字的listView。
能够响应点击事件的listview
我们只需要对上述代码进行修改,将原来的simpleAdapter改成一个自定义的Adapter就可以响应
先定义一个数据组,方便后面调用
private List<Map<String,Object>> mData;
随后定义
private final class ViewHolder{
private ImageView img;
private TextView title;
}
再自定义MyAdapter类
private class MyAdapter extends BaseAdapter{
//view片获取
private LayoutInflater mInflater;
private MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
//获取数组大小
@Override
public int getCount() {
return mData.size();
}
//获取数组
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//获取布局
if (convertView==null){
//重新获取布局
holder = new ViewHolder();
convertView= mInflater.inflate(R.layout.vlist,null);
holder.img = convertView.findViewById(R.id.img);
holder.title = convertView.findViewById(R.id.title);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//获取数据内容
holder.img.setBackgroundResource((Integer) mData.get(position).get("img"));
holder.title.setText((String) mData.get(position).get("title"));
//设置监听事件
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(GoatActivity.this,TestActivity.class));
}
});
return convertView;
}
}
这样我们就可以在OnCreate中引用
mData = getData();
MyAdapter myAdapter = new MyAdapter(this);
listView.setAdapter(myAdapter);
以下为完整代码,XML文件一致
public class MainActivity extends AppCompatActivity {
private ImageView imageView7;
private ListView listView;
private List> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goat);
init();
}
public void init(){
listView = (ListView) findViewById(R.id.listView);
// SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
// new String[]{"title","img"},
// new int[]{R.id.title,R.id.img});
// listView.setAdapter(adapter);
mData = getData();
MyAdapter myAdapter = new MyAdapter(this);
listView.setAdapter(myAdapter);
}
//Map
private List> getData() {
List> list = new ArrayList>();
Map map = new HashMap();
map.put("title", "G1");
map.put("img", R.drawable.pc1);
list.add(map);
map = new HashMap();
map.put("title", "G2");
map.put("img", R.drawable.pc1);
list.add(map);
map = new HashMap();
map.put("title", "G3");
map.put("img", R.drawable.pc1);
list.add(map);
map = new HashMap();
map.put("title", "G4");
map.put("img", R.drawable.pc1);
list.add(map);
return list;
}
private final class ViewHolder{
private ImageView img;
private TextView title;
}
private class MyAdapter extends BaseAdapter{
//view片获取
private LayoutInflater mInflater;
private MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
//获取数组大小
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//获取布局
if (convertView==null){
//重新获取布局
holder = new ViewHolder();
convertView= mInflater.inflate(R.layout.vlist,null);
holder.img = convertView.findViewById(R.id.img);
holder.title = convertView.findViewById(R.id.title);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//获取数据内容
holder.img.setBackgroundResource((Integer) mData.get(position).get("img"));
holder.title.setText((String) mData.get(position).get("title"));
//设置监听事件
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,TestActivity.class));
}
});
return convertView;
}
}
}