ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,比如查看QQ聊天记录,翻阅微博最新消息等。
方法一:entries
res-values-arrays.xml:
mac小技巧:把全部苹果改成香蕉,把光标放到苹果1的 苹 前面,按住option,就可以拖过鼠标拖动选中全部苹果
<resources>
<string-array name="fruit">
<item>苹果1</item>
<item>苹果2</item>
<item>苹果3</item>
<item>苹果4</item>
<item>苹果5</item>
<item>苹果6</item>
<item>苹果7</item>
<item>苹果8</item>
<item>苹果9</item>
<item>苹果10</item>
<item>香蕉1</item>
<item>香蕉2</item>
<item>香蕉3</item>
<item>香蕉4</item>
<item>香蕉5</item>
<item>香蕉6</item>
<item>香蕉7</item>
<item>香蕉8</item>
<item>香蕉9</item>
<item>香蕉10</item>
</string-array>
</resources>
activity_main.xml:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/fruit">
</ListView>
红色框中的是当列表已经在最顶部,再往下拉的时候波浪效果,提醒用户已经是最顶部。底部同理。
方法二:ArrayAdapter
public class MainActivity extends AppCompatActivity {
public String[]data={"apple","banana","orange","watermelon","pear"
,"grape","pineapple","strawberry","cherry","mango","apple",
"banana","orange","watermelon","grape","pineapple","strawberry","cherry","mango"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String>adapter=new ArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1,data);
ListView listView=(ListView)findViewById(R.id.my_lv);
listView.setAdapter(adapter);
}
}
ListView的一些效果:
<ListView
android:id="@+id/my_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#f00"
android:dividerHeight="2px"
android:scrollbars="none"
android:overScrollMode="never">
第四行设置分隔线颜色,第五行设置分隔线高度,
第六行设置取消滚动条,最后一行取消拉到两边时的波浪效果
</ListView>