Android二级分类列表ListView

1.aty

package com.louis.classifymenuview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    List> mainMapList=new ArrayList<>();
    List>> moreMapList_List=new ArrayList<>();

    ListView moreLv;
    MainListBaseAdapter myBaseAdapter;
    MoreListBaseAdpter moreListBaseAdpter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        ListView mainLv= (ListView) findViewById(R.id.classify_mainlist);

        moreLv= (ListView) findViewById(R.id.classify_morelist);
        myBaseAdapter=new MainListBaseAdapter(this,mainMapList);
      //  mainLv.setAdapter(new SimpleAdapter(this, mainMapList, R.layout.list_items, new String[]{"mianName"}, new int[]{R.id.id_tv_list_item}));
        mainLv.setAdapter(myBaseAdapter);
       // mainLv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        initMoreBaseAdapter(0);
        mainLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {

             myBaseAdapter.setNowSelectedIndex(position);

              initMoreBaseAdapter(position);
            }
        });

    }

    private void initMoreBaseAdapter(int position) {
         moreListBaseAdpter=new MoreListBaseAdpter(this,moreMapList_List.get(position));
       // SimpleAdapter simpleAdapter= new SimpleAdapter(MainActivity.this, moreMapList_List.get(position), R.layout.list_items, new String[]{"moreName"}, new int[]{R.id.id_tv_list_item});
        moreLv.setAdapter(moreListBaseAdpter);

       moreLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView parent, View view, int position, long id) {
               moreListBaseAdpter.setNowSelectedIndex(position);
           }
       });
        moreListBaseAdpter.notifyDataSetChanged();
    }

    private void initMoreAdapter(int position) {
        SimpleAdapter simpleAdapter= new SimpleAdapter(MainActivity.this, moreMapList_List.get(position), R.layout.list_items, new String[]{"moreName"}, new int[]{R.id.id_tv_list_item});
        moreLv.setAdapter(simpleAdapter);
        simpleAdapter.notifyDataSetChanged();
    }

    private void initData() {
        for (int i=0;i<15;i++){
            Map  mianMap=new HashMap<>();
            mianMap.put("mainName","mainName"+i);
            mainMapList.add(mianMap);

            List> moreMapList=new ArrayList<>();
            for (int j=0;j<10;j++){
                Map  moreMap=new HashMap<>();
                moreMap.put("moreName","mainName"+i+"下moreName"+j);
                moreMapList.add(moreMap);
            }
            moreMapList_List.add(moreMapList);
        }
    }
}

2.xml 4aty


<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"
    tools:context="com.louis.classifymenuview.MainActivity">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <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"
            android:scrollbars="none" />

    LinearLayout>
LinearLayout>

3.简陋的item xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:id="@+id/id_tv_list_item"
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:layout_height="match_parent" />
LinearLayout>

4.main adpter MainListBaseAdapter

package com.louis.classifymenuview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/17.
 */
public class MainListBaseAdapter extends BaseAdapter {
    Context context;
    List> mainMapList;

    public int getNowSelectedIndex() {
        return nowSelectedIndex;
    }

    public void setNowSelectedIndex(int nowSelectedIndex) {
        this.nowSelectedIndex = nowSelectedIndex;
        this.notifyDataSetChanged();//及时通知显示
    }

    private int nowSelectedIndex = 0;
    public MainListBaseAdapter(Context context, List> mainMapList) {
        this.context = context;
        this.mainMapList=mainMapList;
    }

    @Override
    public int getCount() {
        return mainMapList.size();
    }

    @Override
    public Object getItem(int position) {
        return mainMapList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.list_items,null);
        }
        TextView tv_list_item= (TextView) convertView.findViewById(R.id.id_tv_list_item);
        tv_list_item.setText(mainMapList.get(position).get("mainName").toString());

        if (position==nowSelectedIndex){
            tv_list_item.setBackgroundColor(0xFFFFFFFF);
        }else
        {
            tv_list_item.setBackgroundColor(0xFFEBEBEB);
        }

        return convertView;
    }
}

5。 more list adapter

package com.louis.classifymenuview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/17.
 */
public class MoreListBaseAdpter extends BaseAdapter {

    Context context;
    List> moreMapList;
    private int nowSelectedIndex = 0;

    public MoreListBaseAdpter(Context context, List> moreMapList) {
        this.context = context;
        this.moreMapList = moreMapList;
    }

    @Override
    public int getCount() {
        return moreMapList.size();
    }

    @Override
    public Object getItem(int position) {
        return moreMapList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_items, null);
        }
        TextView tv_list_item = (TextView) convertView.findViewById(R.id.id_tv_list_item);
        tv_list_item.setText(moreMapList.get(position).get("moreName").toString());

        if (position == nowSelectedIndex) {
            tv_list_item.setTextColor(0xFFB3EE3A);
        } else {
            tv_list_item.setTextColor(0xFF525252);
        }
        return  convertView;
    }

    public int getNowSelectedIndex() {
        return nowSelectedIndex;
    }

    public void setNowSelectedIndex(int nowSelectedIndex) {
        this.nowSelectedIndex = nowSelectedIndex;
        this.notifyDataSetChanged();//及时通知显示
    }
}

结果

Android二级分类列表ListView_第1张图片

demo下载:http://download.csdn.net/detail/richiezhu/9464387

Android二级分类列表GirdView见:http://blog.csdn.net/richiezhu/article/details/50920384

你可能感兴趣的:(Android)