将两个textViews添加到可扩展listview的标题中 [英] adding two textViews to the header of the expandable listview

查看:72
本文介绍了将两个textViews添加到可扩展listview的标题中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在android studio中进行开发,并且试图使用Expandable List View.

I'm developing in android studio and I'm trying to use Expandable List View.

我想在每个部分的标题中添加另一个textView表示: 每个可扩展视图的标题上将有两个textViews. 它应该看起来像这样我该怎么做?

I want to add another textView to the header of every part means that: I'll have a two textViews on the header of each expendable view. it should look like this how do i do that?

我已经做了可扩展的列表视图,但是我剩下的唯一一件事就是在标题中添加另一个textview.

i already did the expandable list view but the only thing i have left is to add another textview to the header.

扩展Activity的主类如下所示:

the main class extends Activity is looking like this:

package com.example.yuliaaa.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;

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

public class reasults extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reasults_layout);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.expandable_supers);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

    }

    /*
    * Preparing the list data
    */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("סופר X");
        listDataHeader.add("סופר Y");
        listDataHeader.add("סופר Z");

        List<String> productsX = new ArrayList<String>();
        productsX.add("במבה");
        productsX.add("ביסלי");

        List<String> productsY = new ArrayList<String>();
        productsY.add("מים");
        productsY.add("עגבניות");
        productsY.add("קורנפלקס");

        List<String> productsZ = new ArrayList<String>();
        productsZ.add("קוקה קולה");

        listDataChild.put(listDataHeader.get(0), productsX); // Header, Child data
        listDataChild.put(listDataHeader.get(1), productsY);
        listDataChild.put(listDataHeader.get(2), productsZ);
    }



}

我有: 1)名为 content_resault.xml 的xml,其具有ID为 expandable_supers

i have: 1)xml named content_resault.xml that has the ExpandableListView tag with the id: expandable_supers

2)名为 list_group_super.xml 的XML,该XML具有TextView标签,其ID为: lblListHeader

2)xml named list_group_super.xml that has TextView tag with the id: lblListHeader

3)名为 list_items_products.xml 的xml,它具有一个ID为 lblListItem

3)xml named list_items_products.xml that has a TextView tag with the id: lblListItem

和一个名为:ExpandableListAdapter的java类,该类扩展了BaseExpandableListAdapter,看起来像这样:

and a java class named: ExpandableListAdapter that extends BaseExpandableListAdapter and looks like this:

package com.example.yuliaaa.myapplication;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Tzlil on 01/06/2016.
 */
public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_items_products, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group_super, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

推荐答案

在您的xml list_group_super.xml中添加另一个textView,您需要在其中使用id = lbl2ListHeader.然后在getGroupView中添加此代码段.

Add in your xml list_group_super.xml another textView, where you need it with id = lbl2ListHeader. Then add this code snippet, in getGroupView.

     TextView lbl2ListHeader = (TextView) convertView
            .findViewById(R.id.lbl2ListHeader);
      lbl2ListHeader.setTypeface(null, Typeface.BOLD);
      lbl2ListHeader.setText("whatever you want");

package com.example.yuliaaa.myapplication;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Tzlil on 01/06/2016.
 */
public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_items_products, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group_super, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

      TextView lbl2ListHeader = (TextView) convertView
            .findViewById(R.id.lbl2ListHeader);
      lbl2ListHeader.setTypeface(null, Typeface.BOLD);
      lbl2ListHeader.setText("whatever you want");

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

这篇关于将两个textViews添加到可扩展listview的标题中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆