1.二级联动(spinner)
2.仿设置页面(ListView分组)
3.仿QQ好友列表(ExpandableListView)
用上面这些控件实现相应的功能
1.二级联动
xml
直接写2个spinner
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.a_02.a_07.MainActivity">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/s_main_spinner"
>
Spinner>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/s_main_spinner1">
Spinner>
LinearLayout>
然后activity这边
package com.example.a_02.a_07;
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.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Spinner s_main_spinner;
private Spinner s_main_spinner1;
private List city;
private Map> mmp;
private String[] ss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s_main_spinner = (Spinner) findViewById(R.id.s_main_spinner);
s_main_spinner1 = (Spinner) findViewById(R.id.s_main_spinner1);
//添加数据
ss = new String[]{"A","B","C"};
mmp=new HashMap>();
city=new ArrayList<>();
city.add("a");
city.add("aa");
city.add("aaa");
mmp.put("A",city);
city=new ArrayList<>();
city.add("b");
city.add("bb");
city.add("bbb");
mmp.put("B",city);
city=new ArrayList<>();
city.add("c");
city.add("cc");
city.add("cc");
mmp.put("C",city);
//适配器
ArrayAdapter pro=new ArrayAdapter(this,android.R.layout.simple_list_item_1, ss);
s_main_spinner.setAdapter(pro);
s_main_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
//给子项设值
List sa=mmp.get(ss[position]);
ArrayAdapter pp=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,sa);
s_main_spinner1.setAdapter(pp);
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
}
}
2.仿设置页面
xml代码
这里需要3个xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/addexam_list_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#111211"
android:background="#7b8d7b"
android:text="金融考试"
android:gravity="center_vertical"
/>
LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/addexam_list_item_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="center"
android:textColor="#000000"
android:text="测试数据"/>
LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a_02.a_09.ListviewActivity"
>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview_lv"
android:background="#545458">ListView>
LinearLayout>
activity这边要自定义一个适配器(Array Adapter)
package com.example.a_02.a_09;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class ListviewActivity extends AppCompatActivity {
private ListView listView;
private List list=null;
private List groupkey=new ArrayList();
private List alist=new ArrayList();
private List blist=new ArrayList();
private TextView textView;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
listView = (ListView) findViewById(R.id.listview_lv);
init();
MyAdapter MyAdapter=new MyAdapter();
listView.setAdapter(MyAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
String text= (String) listView.getItemAtPosition(position);
Toast.makeText(ListviewActivity.this, "选择了:"+text, Toast.LENGTH_SHORT).show();
}
});
}
public void init(){
list=new ArrayList();
groupkey.add("无线和网络");
groupkey.add("个人");
alist.add("WLAN");
alist.add("流量使用情况");
alist.add("更多...");
list.add("无线和网络");
list.addAll(alist);
blist.add("位置信息");
blist.add("安全");
blist.add("语言和输入法");
list.add("个人");
list.addAll(blist);
}
//自定义适配器
class MyAdapter extends BaseAdapter{
private TextView textView;
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override//控制item是否可以点击
public boolean isEnabled(int position) {
if(groupkey.contains(getItem(position))){
return false;
}
return super.isEnabled(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(groupkey.contains(getItem(position))){
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_listview_tag, null);
}
else{
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_listview__item, null);
}
textView = (TextView) convertView.findViewById(R.id.addexam_list_item_text);
textView.setText((CharSequence) getItem(position));
return convertView;
}
}
}
3.qq好友列表
同样需要3个xml页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textChild"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/textGroup"/>
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ExpandableListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content">ExpandableListView>
LinearLayout>
然后是activity这边
package com.example.a_02.a_07;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by ming on 2017/6/13 0013.
*/
public class qq extends ExpandableListActivity{
/**
* 创建一条目录容器
* @param savedInstanceState
*/
List