ListView 控件点击listview item 实时更新数据案例.

效果如下:

android ListView中含有按钮事件实时更新ListView数据案例-2_ide

点击list item 右边的选中状态对应的发生改变。

1、布局文件

android ListView中含有按钮事件实时更新ListView数据案例-2_ide_02

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ChoseRoomActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview_all_room"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:listSelector="@android:color/transparent"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
/>
</RelativeLayout>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

android ListView中含有按钮事件实时更新ListView数据案例-2_xml_03

2、activity

android ListView中含有按钮事件实时更新ListView数据案例-2_android_04

package com.jas.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jas.adapter.RoomAdapter;
import com.jas.model.RoomModel;

import java.util.ArrayList;
import java.util.List;

public class ChoseRoomActivity extends AppCompatActivity {


private List<RoomModel> list;
private ListView listView;
private RoomAdapter roomAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chose_room);

list = new ArrayList<>();
list.add(new RoomModel("0001","客厅"));
list.add(new RoomModel("0002","主卧"));
list.add(new RoomModel("0003","次卧"));
list.add(new RoomModel("0004","厨房"));
list.add(new RoomModel("0005","卫生间"));
initView();


}



private void initView(){

listView = findViewById(R.id.listview_all_room);


roomAdapter = new RoomAdapter(this,list);
listView.setAdapter(roomAdapter);

roomAdapter.SetOnClickListener(new RoomAdapter.OnClickListener() {
@Override
public void ClickListener(String roomName) {
setSelected(roomName);
}
});

}


private void setSelected(String roomName){

for(RoomModel r : list){
if(roomName.equals(r.getRoomName())){
r.setSelected(true);
}else{
r.setSelected(false);
}
}

roomAdapter.notifyDataSetChanged();

}

}

android ListView中含有按钮事件实时更新ListView数据案例-2_xml_05

3、RoomAdapter

android ListView中含有按钮事件实时更新ListView数据案例-2_ide_06

package com.jas.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jas.activity.R;
import com.jas.model.RoomModel;

import java.util.List;

public class RoomAdapter extends BaseAdapter {


private List<RoomModel> mlist;
private LayoutInflater inflater;
private Context context;
private OnClickListener onClickListener;

public RoomAdapter(Context context, List<RoomModel> list) {
this.context = context;
this.mlist = list;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override
public int getCount() {
return mlist.size();
}

@Override
public Object getItem(int position) {
return mlist.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView = inflater.inflate(R.layout.item_layout_room,null);
}
TextView idView = (TextView)convertView.findViewById(R.id.tv_room);
ImageView ivView = (ImageView)convertView.findViewById(R.id.iv_status);


final RoomModel p = mlist.get(position);
final int pos = position;
String roomName = p.getRoomName();
idView.setText(roomName);
if(p.isSelected()){
ivView.setImageResource(R.drawable.ic_room_selected);
}else{
ivView.setImageResource(R.drawable.ic_room_no_selected);
}

idView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onClickListener!=null){
onClickListener.ClickListener(roomName);
}
}
});

ivView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onClickListener!=null){
onClickListener.ClickListener(roomName);
}
}
});

return convertView;
}

public void SetOnClickListener(OnClickListener clickListener){
onClickListener = clickListener;
}


public interface OnClickListener{
void ClickListener(String name);
}

}

android ListView中含有按钮事件实时更新ListView数据案例-2_ide_07

4、list item 布局文件,item_layout_room.xml

android ListView中含有按钮事件实时更新ListView数据案例-2_ide_08

<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_dev_bg"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">

<TextView
android:id="@+id/tv_room"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12"
android:textColor="@color/home_txt2"
android:layout_marginLeft="20dp"
android:textSize="16sp"></TextView>

<ImageView
android:id="@+id/iv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_room_no_selected"
android:layout_marginRight="20dp"></ImageView>
</LinearLayout>
</LinearLayout>

android ListView中含有按钮事件实时更新ListView数据案例-2_ide_09

5、RoomModel

android ListView中含有按钮事件实时更新ListView数据案例-2_xml_10

package com.jas.model;

public class RoomModel {

public RoomModel() {
}

public RoomModel(String roomId, String roomName) {
this.roomId = roomId;
this.roomName = roomName;
}

private String roomId;

private String roomName;

private boolean selected;

public String getRoomId() {
return roomId;
}

public void setRoomId(String roomId) {
this.roomId = roomId;
}

public String getRoomName() {
return roomName;
}

public void setRoomName(String roomName) {
this.roomName = roomName;
}

public boolean isSelected() {
return selected;
}

public void setSelected(boolean selected) {
this.selected = selected;
}
}

android ListView中含有按钮事件实时更新ListView数据案例-2_ide_11