本文用于对ListView简单使用的总结
首先建立item的界面
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:layout_margin="15dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/hand_user"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="30dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="30dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="赵晓二"
android:textSize="20sp"
android:textColor="@color/xui_config_color_black"
android:layout_marginBottom="5dp"
/>
<TextView
android:id="@+id/tv_user_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/btn_state_user"
android:text="当前登录"
android:padding="5dp"
android:layout_marginTop="10dp"
android:visibility="gone"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="30dp"
android:orientation="vertical"
>
<Button
android:id="@+id/bt_user_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="@color/xui_config_color_white"
android:background="@mipmap/btn_user"
android:layout_marginBottom="10dp"
android:focusable="false"
/>
<Button
android:id="@+id/bt_user_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"
android:textColor="@color/xui_config_color_white"
android:background="@mipmap/btn_user"
android:layout_marginTop="10dp"
android:focusable="false"
/>
</LinearLayout>
</LinearLayout>
我这个界面是用作用户登录中选择用户的,有一张头像,名称,状态,以及两个按钮登录与删除。头像是固定的,状态由item的值决定是否显示。按钮的focusable属性是用作区分item与item中按钮的点击事件,不将focusable设置成false,点击按钮也会触发item自身的点击事件
item的界面做好了,接下来要自定义适配器
public class UserApadter extends ArrayAdapter {
//保存构造函数传来的resource的值,即item的地址
private int resourceId;
//存放构造函数传来的context的值,即活动,如MainActivity
private Context contextId;
public UserApadter(@NonNull Context context, int resource, List<UserItem>list) {
super(context, resource,list);
resourceId = resource;
userOperation = new UserInfoOperationTask(context);
contextId = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
ViewHolderUser viewHolderUser;
final UserItem userItem = (UserItem) getItem(position);
//这里判断是否是第一次创建item
if (convertView == null) {
//第一次创建item,绑定界面元素
view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
viewHolderUser = new ViewHolderUser();
viewHolderUser.tvUserName = view.findViewById(R.id.tv_user_name);
viewHolderUser.tvUserState = view.findViewById(R.id.tv_user_state);
viewHolderUser.btUserLogin = view.findViewById(R.id.bt_user_login);
viewHolderUser.btUserDetele = view.findViewById(R.id.bt_user_delete);
//将绑定的界面元素添加在view,可以下次直接使用
view.setTag(viewHolderUser);
}else {
//将之前绑定好的界面元素,拿出来使用
view = convertView;
viewHolderUser = (ViewHolderUser) view.getTag();
}
//界面元素绑定完成,将item数据填入
viewHolderUser.tvUserName.setText(userItem.getName());
//通过item的值判断状态的显示效果
if (userItem.isState()){
//VISIBLE => 显示,INVISIBLE => 不显示,占用空间,GONE => 不显示,不占用空间
viewHolderUser.tvUserState.setVisibility(View.VISIBLE);
}
//其中删除按钮的点击事件
viewHolderUser.btUserDetele.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//MaterialDialog是XUI的提示框,Android自带提示框是AlertDialog
new MaterialDialog.Builder(contextId)
.content("确认删除此账户")
.positiveText("确定")
.negativeText("取消")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
remove(userItem);
Log.d("///TAG//","删除成功");
notifyDataSetChanged();
dialog.dismiss();
}
})
.show();
}
});
//登录按钮的点击事件
viewHolderUser.btUserLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("///TAG//","登录成功");
}
});
return view;
}
}
//用于界面元素与item的数值绑定的中间字段。
class ViewHolderUser{
TextView tvUserName;
TextView tvUserState;
Button btUserLogin;
Button btUserDetele;
}
上面我基本做了注释,适配器主要用于item的界面与数据的绑定,如同活动的界面元素绑定基本相同。
界面和适配器都完成了,接下来就是使用了
private void initList(){
if (userInfos.size() != 0){
for (UserInfo info:userInfos){
if (info.getUsername().equals(user)){
list.add(0,new UserItem(info.getName(),info.getUsername(),info.getPassword(),true));
}else {
list.add(new UserItem(info.getName(),info.getUsername(),info.getPassword(),false));
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
//添加数据
initList();
//适配器初始化
userApadter = new UserApadter(this,R.layout.user_item_layout,list);
//绑定适配器
userList.setAdapter(userApadter);
//item自身的点击事件
userList.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//item整体的点击事件
String s = list.get(position).getName();
Log.d("///TAG//","你点击的用户名是:" + s);
}