先来看效果图
这里主要实现了一个二级菜单,并且将所点击的item的id以及名称传递出去。
下面来看代码
首先是布局,第一个就不用讲了,都是button,关键是下面这个doublelist。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/classify_mainlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="#fff"
android:cacheColorHint="#00000000"
android:divider="#fff"
android:dividerHeight="1px"
android:fastScrollEnabled="true"
android:listSelector="#00000000"
android:scrollbars="none" />
<ListView
android:id="@+id/classify_morelist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="17"
android:background="#fff"
android:cacheColorHint="#00000000"
android:divider="#ffebebeb"
android:dividerHeight="0.5px"
android:fastScrollEnabled="true"
android:listSelector="#00000000"/>
</LinearLayout>
这里就是两个listview拼接起来,然后设置adapter时,mainlist点击变色,变成morelist的主色,这样在视觉效果上就感觉像是mainlist打开了morelist一样。
然后就是两个listview的adapter:
ClassifyMainAdapter:
public class ClassifyMainAdapter extends BaseAdapter {
private Context context;
private String[] list;
private int position;
Holder hold;
public ClassifyMainAdapter(Context context, String[] list,int position) {
this.context = context;
this.list = list;
this.position = position;
}
public int getCount() {
return list.length;
}
public Object getItem(int position) {
return list[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int arg0, View view, ViewGroup viewGroup) {
if (view == null) {
view = View.inflate(context, R.layout.item_classify_mainlist, null);
hold = new Holder(view);
view.setTag(hold);
} else {
hold = (Holder) view.getTag();
}
hold.txt.setText(list[arg0].toString());
hold.layout.setBackgroundColor(0xFFEBEBEB);
if (arg0 == position) {
hold.layout.setBackgroundColor(0xFFFFFFFF);
}
return view;
}
public void setSelectItem(int position) {
this.position = position;
}
public int getSelectItem() {
return position;
}
private static class Holder {
LinearLayout layout;
TextView txt;
public Holder(View view) {
txt = (TextView) view.findViewById(R.id.tv_main);
layout = (LinearLayout) view.findViewById(R.id.mainitem_layout);
}
}
}
ClassifyMoreAdapter:
public class ClassifyMoreAdapter extends BaseAdapter {
private Context context;
private String[] text_list;
private int position = 0;
Holder hold;
public ClassifyMoreAdapter(Context context, String[] text_list) {
this.context = context;
this.text_list = text_list;
}
public int getCount() {
return text_list.length;
}
public Object getItem(int position) {
return text_list[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int arg0, View view, ViewGroup viewGroup) {
if (view == null) {
view = View.inflate(context, R.layout.item_classify_morelist, null);
hold = new Holder(view);
view.setTag(hold);
} else {
hold = (Holder) view.getTag();
}
hold.txt.setText(text_list[arg0]);
hold.txt.setTextColor(0xFF666666);
if (arg0 == position) {
hold.txt.setTextColor(0xFFFF8C00);
}
return view;
}
public void setSelectItem(int position) {
this.position = position;
}
private static class Holder {
TextView txt;
public Holder(View view) {
txt = (TextView) view.findViewById(R.id.moreitem_txt);
}
}
}
两个adapter是实现了点击变色。接下来是doublelist的主代码:
MainActiivy:
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
private Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_10,btn_11,btn_12,btn_13,
btn_14,btn_15;
private Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_10 = (Button) findViewById(R.id.btn_10);
btn_11 = (Button) findViewById(R.id.btn_11);
btn_12 = (Button) findViewById(R.id.btn_12);
btn_13 = (Button) findViewById(R.id.btn_13);
btn_14 = (Button) findViewById(R.id.btn_14);
btn_15 = (Button) findViewById(R.id.btn_15);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this)