使用ViewPager作为子元素的ExpandableListView执行两次getChildView

问题描述 投票:0回答:1

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

我也尝试使用TextView(为方便起见,这是我在这里发布的示例代码)来简化代码但出现了同样的问题。

我最好的猜测是它与布局的高度属性有关,但无论我如何改变它,我都无法解决问题。请帮忙。我完全迷失了。我使用了这个代码的教程,从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>

expandable list adapter.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;
            }
        });

    }
android android-viewpager expandablelistview expandablelistadapter
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.