添加ExpandableListView的Button底部?

时间:2018-05-22 07:17:17

标签: android button expandablelistview

我正在创建一个应用程序,我在Fragment Activity中使用了这个ExpandableListView。我想在ListView的底部添加Button。我正在尝试但是当我添加Button时,它无法正常工作,在每个列表标题中添加了Button。我的代码如下。

enter image description here这是当前的截图

enter image description here我想要这样

这是我的xml:

class Calc extends Component {
        constructor(props) {
            super(props);
            this.state = {       // You can set the state
                total: 0
            };

            this._handleChange = this._handleChange.bind(this);
        }

        _handleChange(e) {
            const { value } = e.target;
            var sub = this.state.total + parseInt(value)
            this.setState({
                total: sbu
            });

        }
        render() {
            return (
                <div>
                    <div>{this.state.total}</div>
                    <input
                        id="a"
                        name="a"
                        onChange={this._handleChange} />

                    <input
                        id="b"
                        name="b"
                        onChange={this._handleChange} />

                    <input
                        id="c"
                        name="c"
                        onChange={this._handleChange} />
                </div>

            )
        }
    }

//这是我的java代码:

<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".TurbooneFragment"
    android:background="@color/gray_50">

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:id="@+id/turboone_view">
    </ExpandableListView>


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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:id="@+id/turboone_item"
            android:textColor="@android:color/black"/>

    </LinearLayout>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/blue_500"
            android:textSize="14sp"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:id="@+id/listturbooneheader"/>

    </LinearLayout>

</RelativeLayout>

//我的ListViewAdapter:

    public class TurbooneFragment extends Fragment {

    private TurbooneListAdapter listadapter;
    private ExpandableListView listView;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHash;

    public TurbooneFragment() {
        // Required empty public constructor
    }

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_turboone, container, false);
        listView = view.findViewById(R.id.turboone_view);
        initData();
        listadapter = new TurbooneListAdapter(this.getContext(),listDataHeader,listHash);
        listView.setAdapter(listadapter);

        return view;
    }

    private void initData(){
        listDataHeader = new ArrayList<>();
        listHash = new HashMap<>();

        listDataHeader.add("first");
        listDataHeader.add("second");
        listDataHeader.add("third");

        List<String> first= new ArrayList<>();
        first.add("blabla");

        List<String> second= new ArrayList<>();
        second.add("bla");

        List<String> third= new ArrayList<>();
        third.add("blabla");
        third.add("blabla");

        listHash.put(listDataHeader.get(0),first);
        listHash.put(listDataHeader.get(1),second);
        listHash.put(listDataHeader.get(2),third);

    }

}

1 个答案:

答案 0 :(得分:1)

您对layout以及每个组行和子行使用相同的ExpandableListView(fragment_turboone)。您必须为3个组件中的每个组件提供不同的布局。 fragment的一个布局包含ExpandableListView,另一个布局包含您的组布局(标题),另一个布局包含您的子视图(组内的行)。

由于您对所有组件使用相同的布局,因此您在此布局中添加的任何View也会添加到列表,组和子项中。这就是你必须创建单独布局的原因。

<强>片段

因此,您的片段布局应如下所示: fragment_turboone.xml

<RelativeLayout 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"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 tools:context=".TurbooneFragment"
 android:background="@color/gray_50">

  <Button
    android:id="+@id/buttonId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="your text"/>

  <ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:id="@+id/turboone_view"
    android:layout_above="@id/buttonId">
  </ExpandableListView>
</RelativeLayout>

群组视图

然后,您需要为组视图创建另一个布局(资源中layout文件夹内的XML文件): group_layout.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/blue_500"
        android:textSize="14sp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:id="@+id/listturbooneheader"/>
</LinearLayout>

子视图

您孩子视图的另一个布局: child_layout.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:id="@+id/turboone_item"
        android:textColor="@android:color/black"/>
</LinearLayout>

最后,您必须正确地扩充每个布局。 onCreateView() TurbooneFragment的{​​{1}}因其膨胀fragment_turboone而正确无误。

在适配器中,您必须更改为您的组和子项充气的布局。

在您getGroupView()的{​​{1}}中,您必须更改该行:

TurbooneListAdapter

view = inflater.inflate(R.layout.fragment_turboone, null);

在你view = inflater.inflate(R.layout.group_layout, null); 的{​​{1}}中,您必须更改该行:

getChildView()

TurbooneListAdapter

然后你应该让你的名单有效。