1. The effect diagram of this example
2. Mainly activity_main.xml (layout file) and Activity class file, the implementation process is relatively simple, and the source code is directly attached!
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"> <ListViewandroid: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;
@Overrideprotected 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); // Don't care where the data comes from, just set the adapter, the adapter will give the listview data listView. setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override //items click event 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. Download the source code: ListView.zip