赞
踩
实现的效果图如下
ListView 光标移动,实现很是简单,这里是指item背景的改变 使用 listSelector
listSelector 用户指定当前选中item 的颜色.
下面说下代码 listview 的代码
- <?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="match_parent">
-
- <ListView
- android:id="@+id/list"
- android:listSelector="@android:color/holo_green_light"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
-
- </LinearLayout>
主要的activity
- public class ArrayAdapterActivity extends AppCompatActivity {
-
- private String[] data = {"Android", "Java", "C++", "PHP", "Python", "JS", "HTML", "CSS"};
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.array_adapter_activity_layout);
- final ListView listView = findViewById(R.id.listView);
- List<String> list = new ArrayList<>();
- list.addAll(Arrays.asList(data));
- // adapter设置中布局设定为我们自定义的
- final ArrayAdapter<String> adapter =
- new ArrayAdapter<String>(this, R.layout.simple_single_item);
- adapter.addAll(list);
- listView.setAdapter(adapter);
-
-
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Toast.makeText(ArrayAdapterActivity.this, adapter.getItem(position),
- Toast.LENGTH_SHORT).show();
- }
- });
-
- }
- }
这里使用的是自定义布局
simple_single_item xml 代码如下
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center"
- android:text="1111"
- android:textColor="#ff0000"
- android:textSize="25sp" />
其他的效果也是差不多,这里就补贴代码,感兴趣的也已下载demo 看下.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。