ListView单选效果 简单实现
2018-05-10 本文已影响20人
秋刀鱼_87c6
ListView 单选效果
通过控制列表图片是否显示 来实现选中效果
核心代码1,初始化选中位置,记录点击位置:
private int selectedPosition = -1;// 选中的位置
private int departmentSelectedPosition = 0;//记录已点击的位置 设置为-1,则无初始选中效果
核心代码2,处理选中的逻辑:
if (selectedPosition == position) {
convertView.setSelected(true);
convertView.setPressed(true);
holder.iv.setVisibility(View.VISIBLE);
} else {
convertView.setSelected(false);
convertView.setPressed(false);
holder.iv.setVisibility(View.INVISIBLE);
}
if (position == departmentSelectedPosition) {
convertView.setSelected(true);
convertView.setPressed(true);
holder.iv.setVisibility(View.VISIBLE);
}
核心代码3,处理条目点击逻辑
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
departmentSelectedPosition = position;
myAdapter.notifyDataSetChanged();
}
});
Xml Activity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Xml Itemt布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rel_item_service" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/tv_item_name"
android:layout_marginLeft="12dp"
android:layout_marginBottom="9dp"
android:layout_marginTop="9dp"
android:text=""
android:textSize="14sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:gravity="left"
android:layout_marginRight="60dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="12dp"
android:layout_marginBottom="9dp"
android:layout_marginTop="9dp"
android:text="¥ 1200元"
android:textSize="14sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:visibility="invisible"
android:src="@mipmap/icon_yiyuancheng"
android:id="@+id/iv_item_select"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:layout_marginBottom="9dp"
android:layout_marginTop="9dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Activity 代码逻辑
public class MainActivity extends AppCompatActivity {
private ListView listView;
private MyAdapter myAdapter = new MyAdapter();
private List<String> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
initData();
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
departmentSelectedPosition = position;
myAdapter.notifyDataSetChanged();
}
});
}
private void initData() {
for (int i = 0; i < 20; i++) {
mList.add(String.format("饮 %s 壶浊酒",i));
}
}
private int selectedPosition = -1;// 选中的位置
private int departmentSelectedPosition = 0;//记录已点击的位置 设置为-1,则无初始选中效果
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return mList == null?0:mList.size();
}
@Override
public Object getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MainActivity.this,R.layout.item_list,null);
holder = new ViewHolder();
holder.tv = convertView.findViewById(R.id.tv_item_name);
holder.iv = convertView.findViewById(R.id.iv_item_select);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(mList.get(position));
if (selectedPosition == position) {
convertView.setSelected(true);
convertView.setPressed(true);
holder.iv.setVisibility(View.VISIBLE);
} else {
convertView.setSelected(false);
convertView.setPressed(false);
holder.iv.setVisibility(View.INVISIBLE);
}
if (position == departmentSelectedPosition) {
convertView.setSelected(true);
convertView.setPressed(true);
holder.iv.setVisibility(View.VISIBLE);
}
return convertView;
}
class ViewHolder {
TextView tv ;
ImageView iv;
}
} }
![sad.png](https://img.haomeiwen.com/i5301282/eb551c5783456545.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![sdb.png](https://img.haomeiwen.com/i5301282/159bedf999fdaecd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)