android 商城左边菜单,右边联动
项目的需要,类似于大多数商城一样的左菜单,右内容的。实现左右联动的效果。在网上也找了些Demo 但是效果都不理想,所以就自己手动写了一个,虽然也没有太多的技术含量,但是效果还是不错的。
实现的原理就是两个Listview 进行绑定。根据滑动的位置进行相应的左右联动。直接上效果图:
下面是核心代码MainActicity,主要是使用了ListViewde的滑动监听 setOnScrollListener和ListView的滑动跳转方法: setSelection()和 smoothScrollToPositionFromTop()
- public class MainActivity extends Activity {
- private ListView titleList, conList;
- private List<String> list = new ArrayList<String>();
- private Map<String, List<String>> map = new HashMap<String, List<String>>();
- private ContAdapter adapter2;
- private ImageView point;
- public static int POSITION=0;
- private ListtitleAdapter adapter;
- private boolean flag=false;
- private int CURRENTID=0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- titleList = (ListView) findViewById(R.id.title);
- conList = (ListView) findViewById(R.id.content);
- for (int i = 0; i < 8; ++i) {
- list.add(i + "");
- }
- adapter = new ListtitleAdapter(this, list);
- titleList.setAdapter(adapter);
- for (int k = 0; k < 8; k++) {
- List<String> list = new ArrayList<String>();
- for (int j = 0; j < 8; j++) {
- list.add("第"+k+"组"+"第" + j+ "个");
- }
- map.put(String.valueOf(k), list);
- }
- adapter2 = new ContAdapter(MainActivity.this, map);
- conList.setAdapter(adapter2);
- adapter2.notifyDataSetChanged();
- conList.setOnScrollListener(new OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- if(firstVisibleItem!=CURRENTID){
- CURRENTID=firstVisibleItem;
- if(CURRENTID!=POSITION)
- POSITION=CURRENTID;
- adapter.notifyDataSetChanged();
- }
- }
- });
- titleList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- POSITION=position;
- adapter.notifyDataSetChanged();
- adapter2.notifyDataSetChanged();
- conList.setSelection(position);
- conList.smoothScrollToPositionFromTop(position, 0, 100);
- }
- });
- }
- }
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- tools:context="com.example.listdemo.MainActivity" >
- <ListView
- android:id="@+id/title"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:scrollbars="none" >
- </ListView>
- <View android:layout_width="1dp"
- android:background="@android:color/black"
- android:layout_height="match_parent"/>
- <ListView
- android:id="@+id/content"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="3"
- android:scrollbars="none" >
- </ListView>
- </LinearLayout>
下面是Demo 的链接: 点击打开链接