1.renderShowEditView里面的2個圖片自己替換一下
2.復(fù)制代碼直接就可以跑
import React, {PureComponent} from 'react'
import {View, Text, StyleSheet, ScrollView, TouchableOpacity, ListView, Image, Dimensions} from 'react-native'
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
let {width, height} = Dimensions.get('window');
export default class CircleList extends PureComponent {
constructor(props) {
super(props);
this.state = {
isEdict: false,//是否編輯狀態(tài)
selectArray: [],
dataSource: [{id: '1', title: '文章1'}, {id: '2', title: '文章2'}, {id: '3', title: '文章3'},],
}
}
componentDidMount() {
let array = this.state.dataSource;
let newArray = []
//服務(wù)器返回的數(shù)據(jù),自己增加一個狀態(tài),控制是否選中
for (let i = 0; i < array.length; i++) {
let dict = array[i]
dict.isSelect = false;
newArray.push(dict);
}
this.setState({
dataSource: newArray
});
}
render() {
return (
<View style={{flex: 1}}>
<Text onPress={this.onEditOnPress} style={{color: 'black'}}>{'點我進入編輯/非編輯狀態(tài)'}</Text>
<ListView
renderRow={this.renderRow}
dataSource={ds.cloneWithRows(this.state.dataSource)}/>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Text>{JSON.stringify(this.state.selectArray)}</Text>
</View>
</View>
);
}
renderRow = (rowData, sectionID, rowID, highlightRow) => {
return (
<View style={{height: 111, borderBottomColor: 'gray', borderBottomWidth: 1, flexDirection: 'row'}}>
{this.renderShowEditView(this.state.isEdict, rowData, rowID, ()=> {
this.rightOnPress(rowData, rowID)
})}
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Text>{rowData.title}</Text>
</View>
</View>
)
}
//是否進去編輯狀態(tài)
onEditOnPress = ()=> {
this.setState({isEdict: !this.state.isEdict});
}
//左邊按鈕選擇
rightOnPress = (rowData, index)=> {
let selectArray = this.state.selectArray;
let data = this.state.dataSource;
let newArray = []
for (let i = 0; i < data.length; i++) {
let dict = data[i];
if (index == i) {
if (dict.isSelect == true) {
dict.isSelect = false
for (let j = 0; j < selectArray.length; j++) {
let id = selectArray[j];
if (id == dict.id) {
selectArray.splice(j, 1);
}
}
} else {
dict.isSelect = true
selectArray.push(dict.id);
}
}
newArray.push(dict);
}
this.setState({
selectArray: selectArray,
dataSource: newArray
});
}
//是否選中
renderShowEditView(isEdict, rowData, index, onPress) {
if (isEdict == true) {
let imageURL = require('../../res/img/circle/沒選中.png')
if (rowData.isSelect == true) {
imageURL = require('../../res/img/circle/選中.png')
}
return (
<TouchableOpacity onPress={()=> {
onPress(rowData, index)
}} style={{height: 111, width: 40, justifyContent: 'center', alignItems: 'center'}}>
<Image style={{width: 30, height: 30}} source={imageURL}/>
</TouchableOpacity>
)
}
}
}