【问题标题】:How to add two textviews to the child of an Expandable Listview如何将两个文本视图添加到可扩展列表视图的子级
【发布时间】:2016-08-12 07:14:57
【问题描述】:

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

这个链接看起来很相似,但显然不是解决方案 adding two textViews to the header of the expandable listview

我想要的是将两个文本视图放置到子级而不是可扩展列表的标题。

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

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>

【问题讨论】:

  • 他们现在看起来怎么样?也添加当前屏幕截图?
  • @SohailZahid 是,但在可扩展列表的子级上

标签: android expandablelistview


【解决方案1】:

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

类似这样的: 相对布局 relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, RelativeLayout.LayoutParams.WRAP_CONTENT);

parentView.addView(relativeLayout);

【讨论】:

    猜你喜欢
    • 2016-10-02
    • 2014-01-24
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多