1. 程式人生 > >listview的使用-兩個listview的聯動

listview的使用-兩個listview的聯動

原始碼:

知識點:

listview的單選,兩個listview的聯動

效果:

Markdown

佈局:

左邊一個listview,右邊一個listview

程式碼:

public class ListListActivity extends Activity {

    private ListView mainlist;
    private ListView morelist;
    private List<Map<String, Object>> mainList;
    ClassifyMainAdapter mainAdapter;
    ClassifyMoreAdapter moreAdapter;

    @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_classify); initModle(); initView(); } private void initView() { mainlist = (ListView) findViewById(R.id.classify_mainlist); morelist = (ListView) findViewById(R.id.classify_morelist); mainAdapter = new
ClassifyMainAdapter(ListListActivity.this, mainList); mainAdapter.setSelectItem(0); mainlist.setAdapter(mainAdapter); mainlist.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int
position, long id) { initAdapter(Model.MORELISTTXT[position]); mainAdapter.setSelectItem(position); mainAdapter.notifyDataSetChanged(); } }); mainlist.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 一定要設定這個屬性,否則ListView不會重新整理 initAdapter(Model.MORELISTTXT[0]); morelist.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { moreAdapter.setSelectItem(position); moreAdapter.notifyDataSetChanged(); } }); } private void initAdapter(String[] array) { moreAdapter = new ClassifyMoreAdapter(this, array); morelist.setAdapter(moreAdapter); moreAdapter.notifyDataSetChanged(); } private void initModle() { mainList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < Model.LISTVIEWIMG.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("img", Model.LISTVIEWIMG[i]); map.put("txt", Model.LISTVIEWTXT[i]); mainList.add(map); } } }

關鍵:在左邊listview的點選事件中給右邊listview來setadapter

左邊的adapter:

public class ClassifyMainAdapter extends BaseAdapter {

    private Context context;
    private List<Map<String, Object>> list;
    private int position = 0;
    private boolean islodingimg = true;
    Holder hold;

    public ClassifyMainAdapter(Context context, List<Map<String, Object>> list) {
        this.context = context;
        this.list = list;
    }

    public ClassifyMainAdapter(Context context, List<Map<String, Object>> list,
            boolean islodingimg) {
        this.context = context;
        this.list = list;
        this.islodingimg = islodingimg;
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(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();
        }
        if (islodingimg == true) {
            hold.img.setImageResource(Integer.parseInt(list.get(arg0)
                    .get("img").toString()));
        }
        hold.txt.setText(list.get(arg0).get("txt").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;
        ImageView img;
        TextView txt;

        public Holder(View view) {
            txt = (TextView) view.findViewById(R.id.mainitem_txt);
            img = (ImageView) view.findViewById(R.id.mainitem_img);
            layout = (LinearLayout) view.findViewById(R.id.mainitem_layout);
        }
    }
}

右邊的adapter:

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);
        }
    }
}