ListView的简单使用--Android
1、本例实现效果图
2、主要是activity_main.xml(布局文件)和Activity类文件,实现过程比较简单,直接附源码了哈!
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/color_main_bg">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
MainActivity.java:
package livetelecast.thonlon.example.cn.thonlonlivetelecast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.list_view);
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,getData());
listView.setAdapter(adapter);// 不关心数据是从哪里来的只需要设置 adapter,adapter会给listview数据
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override //items的点击事件
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String txt=adapter.getItem(i);
// Toast.makeText(MainActivity.this,txt,0).show();
// setContentView(R.layout.activity_play);
}
});
}
private String[] getData(){
return new String[]{"CCTV1","CCTV2","CCTV3","CCTV4","CCTV5","CCTV6","CCTV7","CCTV8","CCTV9","湖南卫视"};
}
}
3、源码下载:ListView.zip