使用ViewPager作为子元素的ExpandableListView执行两次getChildView [英] ExpandableListView with ViewPager as child-elements executes getChildView twice
问题描述
我想创建一个以viewpagers作为子视图的可扩展列表.现在的问题是,使用我的代码,它调用getChildView
方法 两次 ,从而两次创建了viewpager.
I want to create an expandable list with viewpagers as subviews. The problem is now that with my code it calls the getChildView
method twice and thus creates my viewpager twice.
我还使用TextView
(为方便起见在此处发布了示例代码)进行了尝试,以简化代码,但出现了同样的问题.
I also tried it with the TextView
(which is the sample code I posted here for convenience) to simplify the code but the same issue arose.
我的最佳猜测是它与布局的height属性有关,但是无论我如何进行更改,我都无法解决问题. 请帮忙.我完全迷路了.我使用了此代码的教程,从youtube评论部分来看,我似乎是唯一遇到该问题的人. ( https://www.youtube.com/watch?v=jZxZIFnJ9jE )
My best guess is it has something to do with the height property of the layouts but no matter how i changed it, I couldn't resolve the issue. Please help. I am totally lost. I used a tutorial for this code and I seem to be the only who has that issue judging from the youtube comment section. (https://www.youtube.com/watch?v=jZxZIFnJ9jE)
(如果您需要代码的其他任何部分,请告诉我.)
(If you need any other part of the code please let me know.)
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)
MainActivity (only 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;
}
});
}
推荐答案
在sub_list.xml中,layout_height是"wrap_content".将其更改为固定高度(某些dp)或match_parent,它将起作用.
In the sub_list.xml the layout_height is "wrap_content". Change it to a fixed height (some dp) or match_parent and it will work.
原因: wrap_content必须计算其必须匹配的布局内容的高度,从而在以后重新加载布局->多次创建列表.
Reason: wrap_content has to calculate the height of the layout content it has to match thus reloading the layout afterwards -> creating the list more than once.
这篇关于使用ViewPager作为子元素的ExpandableListView执行两次getChildView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!