如何将两个文本视图添加到可扩展列表视图的 child

How to add two textviews to the child of an Expandable Listview

我正在尝试将两个文本视图并排放置在一个可展开的列表视图中。不幸的是,在堆栈溢出和 Google 搜索中找到的所有解决方案都被证明是徒劳的。

这个 link 看起来很相似,但显然不是解决方案

我想要的是将两个文本视图放置到 child 而不是可扩展列表的 header。

我希望两个文本视图在 child 视图中的样子

ExpandableListAdapter.java

  public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> ParentItem;
    private HashMap<String, List<String>> ChildItem;


    public ExpandableListAdapter(Context context, List<String> ParentItem,
                                 HashMap<String, List<String>> ChildItem) {
        this.context = context;
        this.ParentItem = ParentItem;
        this.ChildItem = ChildItem;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.ChildItem.get(this.ParentItem.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.child_item, null);
        }

        //TextView text1 = (TextView) convertView.findViewById(R.id.item1);
        TextView text2 = (TextView) convertView.findViewById(R.id.item2);
        //text1.setText("");
        text2.setText(expandedListText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.ChildItem.get(this.ParentItem.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.ParentItem.get(listPosition);
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.parent_item, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.listTitle);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);

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

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }


    public static HashMap<String, List<String>> getData() {
        HashMap<String, List<String>> ParentItem = new HashMap<String,List<String>>();

        List<String> NADMO = new ArrayList<String>();
        NADMO.add("38838333");
        NADMO.add("03220-34084");
        NADMO.add("03820-23110");
        NADMO.add("03222-22145");
        NADMO.add("03120-46676");
        NADMO.add("03720-23154");
        NADMO.add("03920-22657");
        NADMO.add("020-2006943/03620-27158");
        NADMO.add("03321-32127");
        NADMO.add("03520-27271");

        List<String> GPS = new ArrayList<String>();
        GPS.add("Lion");
        GPS.add("Tiger");
        GPS.add("Leopard");
        GPS.add("Cheetah");
        GPS.add("Bear");

        List<String> TPS = new ArrayList<String>();
        TPS.add("Cricket");
        TPS.add("Football");
        TPS.add("Tennis");
        TPS.add("Basket Ball");
        TPS.add("Base Ball");


        ParentItem.put("NADMO"), NADMO);
        ParentItem.put("GPS", GPS);
        ParentItem.put("TPS", TPS);

        return ParentItem;
    }
}

MainActivity.java

public class CodeDir extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;

@Override
protected void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);

    setContentView(R.layout.activity_code_dir);

    //TOOLBAR SUPPORT - Set a Toolbar to replace the ActionBar.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
    actionBar.setDisplayHomeAsUpEnabled(true);


    expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    expandableListDetail = ExpandableListAdapter.getData();
    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
    expandableListAdapter = new ExpandableListAdapter(this, expandableListTitle, expandableListDetail);
    expandableListView.setAdapter(expandableListAdapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " ListView Open.",
                    Toast.LENGTH_SHORT).show();
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " ListView Closed.",
                    Toast.LENGTH_SHORT).show();

        }
    });

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            Toast.makeText(getApplicationContext(),
                    expandableListDetail.get(
                            expandableListTitle.get(groupPosition)).get(
                            childPosition), Toast.LENGTH_SHORT
            )
                    .show();
            return false;
        }
    });
}

}

child_item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:padding="2dp">

       <TextView
            android:id="@+id/item1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:paddingTop="10dp"
            android:layout_weight="1.0"
            android:paddingBottom="10dp"
           android:text="Left Side"
           android:textSize="20sp"/>


        <TextView
            android:id="@+id/item2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:paddingTop="10dp"
            android:layout_weight="1.0"
            android:paddingBottom="10dp"
            android:text="Right side"
            android:textSize="20sp"/>

    </LinearLayout>
</LinearLayout>

这可能会有所帮助。以编程方式创建相对布局,将文本视图添加到相对布局并将相对布局添加到预期视图。

像这样: RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, RelativeLayout.LayoutParams.WRAP_CONTENT);

parentView.addView(relativeLayout);