目录
一、选择自己喜欢的图片(5~10张)
二、创建一个 ListView 的项目
三、新建一个类,用于对显示内容进行操作
四、新建一个布局,用于设置显示内容
五、新建一个Adapter适配器
六、在MainActivity中创建一个ArrayList,再用一个方法把每个内容加载到列表
一、添加照片
把自己喜欢的照片重命名并添加到drawable
?二、创建一个ListView的项目
选择EmptyActivity
?
?三、在项目中新建一个类
新建一个类:ListView / app / src / main / java / com.example.listview ,
然后右击com.example.listview
New / Java Class
以汽车为例,创建一个名为 Cat 的类
?Cat 类里面的代码为:
package com.example.listview;
public class Cat {
private static String name;
private String text;
private int id;
public Cat(String name,String text,int id){
this.name = name;
this.text = text;
this.id = id;
}
public static String getName(){
return name;
}
public String getText(){
return text;
}
public int getId(){
return id;
}
}
四、在ListView中新建一个布局
新建布局:?ListView / app / src / main / res / layout
然后右击 layout
New / Layout Reoursce File
?content_view 里面的代码为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/content_image"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content_text"/>
</LinearLayout>
</LinearLayout>
五、新建一个Adapter适配器
方法同新建Cat类一样,名为CatAdapter
?CatAdapter中的代码为:
package com.example.listview;
import android.content.Context;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class CatAdapter extends ArrayAdapter<Cat> {
private int resouceID;
public CatAdapter(@NonNull Context context, int id, List<Cat> objexts) {
super(context, id,objexts);
resouceID = id ;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Cat cat = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resouceID,parent,false);
ImageView catImage = (ImageView) view.findViewById(R.id.content_image);
TextView catText = (TextView) view.findViewById(R.id.content_name);
TextView catText2 = (TextView) view.findViewById(R.id.content_text);
catImage.setImageResource(cat.getId());
catText.setText(cat.getName());
catText2.setText(cat.getText());
return view;
}
}
六、MainActivity中方法的使用
1、先修改activity_main 的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view"/>
</LinearLayout>
2、MainActivity中的代码如下:
package com.example.listview;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Cat> CatList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCat();
CatAdapter adapter = new CatAdapter(MainActivity.this,R.layout.content_view,CatList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Cat cat = CatList.get(position);
Toast.makeText(MainActivity.this,Cat.getName(),Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int pos, long l) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("删除确认");
dialog.setMessage("是否确认删除");
dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
CatList.remove(pos);
adapter.notifyDataSetChanged();
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
return true;
}
});
}
private void initCat(){
for (int i = 0; i<3;i++){
Cat cat_a = new Cat("cata","看不到我",R.drawable.cat_a_pic);
CatList.add(cat_a);
Cat cat_b= new Cat("catb","小可爱",R.drawable.cat_b_pic);
CatList.add(cat_b);
Cat cat_c= new Cat("catc","和你对视",R.drawable.cat_c_pic);
CatList.add(cat_c);
Cat cat_d= new Cat("catd","好难过",R.drawable.cat_d_pic);
CatList.add(cat_d);
Cat cat_e= new Cat("cate","看看我",R.drawable.cat_e_pic);
CatList.add(cat_e);
Cat cat_f= new Cat("catf","我好懒",R.drawable.cat_f_pic);
CatList.add(cat_f);
}
}
}
|