具有ViewPager作为子元素的ExpandableListView两次执行getChildView

时间:2019-04-09 19:54:48

标签: android android-viewpager expandablelistview expandablelistadapter

我想创建一个将viewpagers作为子视图的可扩展列表。现在的问题是,使用我的代码,它调用了getChildView方法 两次 ,从而两次创建了我的viewpager。

我还使用TextView(为方便起见在此处发布了示例代码)进行了尝试,以简化代码,但出现了同样的问题。

我的最佳猜测是它与布局的height属性有关,但是无论我如何更改,我都无法解决问题。 请帮忙。我完全迷路了。我使用了此代码的教程,从youtube评论部分来看,我似乎是唯一遇到该问题的人。 (https://www.youtube.com/watch?v=jZxZIFnJ9jE

(如果您需要代码的其他任何部分,请告诉我。)

delete.xml 文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/sukahead">

    <TextView
        android:id="@+id/suka"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="suka"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingStart="?android:attr/expandableListPreferredChildPaddingLeft"
        />

</LinearLayout>

sublist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp" android:background="@color/white"
    android:id="@+id/sublist">

    <TextView
        android:id="@+id/dict_entry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingStart="?android:attr/expandableListPreferredChildPaddingLeft"
        android:textSize="16sp"
        android:textColor="@color/colorAccent"
        android:text="test"
        />

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/colorPrimary">

    <EditText
        android:id="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search_hint"
        android:layout_margin="15dp"
        android:padding="5dp"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/white"
        android:adjustViewBounds="true"
        android:maxLines="1"
        android:inputType="text"
        android:onClick="lookup_word"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/search_bar"
        android:orientation="horizontal">

        <ExpandableListView
            android:id="@+id/search_result_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="#333"
            android:dividerHeight="1dp"
            android:background="@color/white"
            android:layout_margin="15dp"/>


     </LinearLayout>

</RelativeLayout>

ExpandableListAdapter.java

package com.lunaticcoding.linguodict;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.view.ViewPager;
import android.text.Layout;
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;

public class ExpendableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> data;
    private HashMap<String, String[]> listHashMap;

    private LayoutInflater inflater;
    private String pageData[];

    public ExpendableListAdapter(Context context, List<String> list, HashMap<String, String[]> hashMap) {
        this.context = context;
        this.data = list;
        this.listHashMap = hashMap;
    }


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

    @Override
    public int getChildrenCount(int groupPosition) {
        return listHashMap.get(data.get(groupPosition)).length;
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listHashMap.get(data.get(groupPosition))[childPosition];
    }

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

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

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

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

        TextView textView = (TextView) convertView.findViewById(R.id.dict_entry);
        textView.setText(data.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String)getChild(groupPosition, childPosition);
        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.delete, null);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.suka);
        return convertView;
    }

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

MainActivity (仅OnCreate)

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

        searchBar = (EditText) findViewById(R.id.search_bar);
        searchResults = (ExpandableListView) findViewById(R.id.search_result_list);

        list_results = new ArrayList<>();
        examples_results = new HashMap<>();
        list_results.add("test1");
        list_results.add("test2");
        list_results.add("test3");

        String pageData1[] = new String[]{"si", "siiii"};
        String pageData2[] = new String[]{"jifasfa", "sfasdfasiii"};
        String pageData3[] = new String[]{"jifasfa", "sfasdfasiii"};
        examples_results.put(list_results.get(0), pageData1);
        examples_results.put(list_results.get(1), pageData2);
        examples_results.put(list_results.get(2), pageData3);

        searchResultsAdapter = new ExpendableListAdapter(this, list_results, examples_results);
        searchResults.setAdapter(searchResultsAdapter);

        lastExpandedPosition = -1;
        searchResults.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                if(lastExpandedPosition != -1 && (lastExpandedPosition != groupPosition)){
                    searchResults.collapseGroup(lastExpandedPosition);
                }
                lastExpandedPosition = groupPosition;
            }
        });

    }

1 个答案:

答案 0 :(得分:0)

在sub_list.xml中,layout_height为“ wrap_content”。将其更改为固定高度(某些dp)或match_parent,它将起作用。

原因: wrap_content必须计算其必须匹配的布局内容的高度,从而在以后重新加载布局->多次创建列表。