单选listview效果,默认点击第一个item,当已经选中的item再次点击的时候让item变成不选中的情况,当点击其他的item的时候选中其他的item。(图片背景果然是太粉嫩了。)
(1)listview的代码(不解释了,大家改吧改吧希望能用到你们的项目当中。)
import java.util.ArrayList;
import com.example.singchooselistviewdemo.Model.Data;
import com.example.singchooselistviewdemo.adapter.MyAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listview;
private ArrayList<Data> dataList;
private MyAdapter mAdapter;
private int beforePostion;
private boolean isChecked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
initData();
getSingData();
}
private void initData() {
dataList = new ArrayList<Data>();
for (int i = 0; i < 10; i++) {
Data data = new Data();
data.setId(i + "");
data.setName("data" + i);
dataList.add(data);
}
mAdapter = new MyAdapter(MainActivity.this, dataList);
listview.setAdapter(mAdapter);
}
private int afterposition;
private void getSingData() {
beforePostion = 0;
mAdapter.setSelectItem(beforePostion);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
afterposition = position;
if (afterposition == beforePostion) {
if (!isChecked) {
beforePostion = afterposition;
mAdapter.setSelectItem(-1);
isChecked = true;
} else {
beforePostion = position;
mAdapter.setSelectItem(position);
isChecked = false;
}
} else {
isChecked = false;
beforePostion = position;
mAdapter.setSelectItem(position);
}
mAdapter.notifyDataSetInvalidated();
}
});
}
}
(2)adapter
import java.util.ArrayList;
import com.example.singchooselistviewdemo.R;
import com.example.singchooselistviewdemo.Model.Data;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<Data> dataList;
public MyAdapter(Context context, ArrayList<Data> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Data getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(context, R.layout.item, null);
}
Data data = dataList.get(position);
ViewHolder holder = ViewHolder.getHolder(convertView);
holder.tvText.setText(data.getName());
if (position == pos) {
holder.ivIcon.setVisibility(View.VISIBLE);
} else {
holder.ivIcon.setVisibility(View.GONE);
}
return convertView;
}
static class ViewHolder {
public ImageView ivIcon;
public TextView tvText;
public ViewHolder(View convertView) {
ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
tvText = (TextView) convertView.findViewById(R.id.tv_text);
}
public static ViewHolder getHolder(View convertView) {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
if (viewHolder == null) {
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
return viewHolder;
}
}
private int pos = -1;
public void setSelectItem(int i) {
pos = i;
}
public int getSelectItem() {
return pos;
}
}
这样就可以实现了listview单选效果了。有问题可以沟通(qq:1250882930)
这里写链接内容