Android的双扩展列表视图隐藏项目,如果列表过大 [英] Android double expandable listview hides items if list is too big
问题描述
我创建了一个树作为一个双扩展列表视图中,有太多的孩子的(15)比别人多,并没有显示出他所有孩子的项目之一。
_ ______
+ | _______ |
_______
+ | _______ |
_______
| _______ |
_______
| _______ |
...
...
_______
| _______ | // 11个项目
_______ // 12个项目中可以看出一点,它-S点击
_______
+ | _______ |
_______
+ | _______ |
...
...
_______
+ | _______ |
在12'th项目后的列表被切断。我还没有提上了名单的大小的任何约束。 任何想法,为什么出现这种情况? 谢谢!
修改 这是code的冷杉扩展列表。
< ExpandableListView机器人:layout_width =FILL_PARENT机器人:ID =@ + ID / ParentLevel
机器人:groupIndicator =@空的Android版本:choiceMode =singleChoice
机器人:layout_height =FILL_PARENT
机器人:layout_above =@ ID / button_layout
机器人:paddingRight =5DP>
的其他子列表被添加在适配器getView()方法
code例如
// MainActivity.java
公共类MainActivity扩展活动实现OnChildClickListener {
ExpandableListView ELV;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
// QUI SOTTO
树根=新树(根,123);
的for(int i = 0;我3;;我++){
树firstLevel =新树((firstLevel+ I),I);
对于(INT J = 0; J< 6,J ++){
树secondLevel =新树(secondLevel+ J,J);
对于(INT Z = 0; Z< 27; Z ++){
树的第三层子=新树(第三层子+ Z,Z);
secondLevel.addChild(第三层子);
}
firstLevel.addChild(secondlevel);
}
root.addChild(firstLevel);
}
ELV =新ExpandableListView(本);
最后TreeAdapter treeAdapter =新TreeAdapter(这一点,根,这一点);
elv.setAdapter(treeAdapter);
elv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
elv.setOnGroupClickListener(新ExpandableListView.OnGroupClickListener(){
@覆盖
公共布尔onGroupClick(ExpandableListView父,视图V,
INT groupPosition,长ID){
的for(int i = 0; I< treeAdapter.getGroupCount();我++){
elv.collapseGroup(ⅰ);
}
elv.expandGroup(groupPosition);
返回true;
}
});
的setContentView(ELV);
elv.expandGroup(0);
进入E = treeAdapter.lsfirst [0];
e.cls.expandGroup(1);
treeAdapter.selectedClass = 33;
}
TreeAdapter.java
公共类TreeAdapter扩展BaseExpandableListAdapter {
最终静态字符串变量= TreeAdapter.class.getSimpleName();
静态公共整数selectedClass = NULL;
静态INT CHILD_PADDING;
静浮TEXT_SIZE;
私人树树;
私人OnChildClickListener LST;
最后的私人上下文的背景下;
类条目{
最后CustExpListview CLS;
最后SecondLevelAdapter sadtp;
公共入口(CustExpListview CLS,SecondLevelAdapter sadtp){
this.cls = CLS;
this.sadtp = sadtp;
}
}
输入[lsfirst]
公共TreeAdapter(MainActivity CTX,树树,OnChildClickListener LST){
this.context = CTX;
this.tree =树;
this.lst = LST;
TEXT_SIZE = 35;
CHILD_PADDING = 40;
lsfirst =新条目[tree.getChilds()的大小()];
的for(int i = 0; I< tree.getChilds()的大小();我++){
CustExpListview SecondLevelexplv =新CustExpListview(上下文);
SecondLevelAdapter ADP =新
SecondLevelAdapter(tree.getChilds()获得(一));
SecondLevelexplv.setAdapter(ADP);
SecondLevelexplv.setGroupIndicator(空);
SecondLevelexplv.setOnChildClickListener(LST); //添加监听器
lsfirst [我] =新条目(SecondLevelexplv,ADP);
}
}
@覆盖
公共对象getChild(INT为arg0,诠释ARG1){
返回ARG1;
}
@覆盖
众长getChildId(INT groupPosition,诠释childPosition){
返回childPosition;
}
@覆盖
公共查看getChildView(INT groupPosition,INT childPosition,
布尔isLastChild,查看convertView,ViewGroup中父){
// LISTA DI secondlevel
返回lsfirst [groupPosition] .CLS;
}
@覆盖
公众诠释getChildrenCount(INT groupPosition){
返回1;
}
@覆盖
公共对象getGroup(INT groupPosition){
返回groupPosition;
}
@覆盖
公众诠释getGroupCount(){
返回tree.getChilds()的大小()。
}
@覆盖
众长getGroupId(INT groupPosition){
返回tree.getChilds()得到(groupPosition).getId()。
}
@覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,ViewGroup中父){
// firstlevel
TextView的电视=新的TextView(上下文);
tv.setText(tree.getChilds()得到(groupPosition).getName());
tv.setPadding(3,7,7,7);
返回电视;
}
@覆盖
公共布尔hasStableIds(){
返回true;
}
@覆盖
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
返回true;
}
////////////////////////////////////////////////// ///////////////////////////
// _____________ ------------- ________ ________ ---------- --------- _______----- //
////////////////////////////////////////////////// ///////////////////////////
公共类CustExpListview扩展ExpandableListView {
INT intGroupPosition,intChildPosition,intGroupid;
公共CustExpListview(上下文的背景下)
{
超(上下文);
this.setSelector(R.drawable.divider_gray);
}
保护无效onMeasure(INT widthMeasureSpec,INT heightMeasureSpec)
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
}
公共类SecondLevelAdapter扩展BaseExpandableListAdapter {
树tree2;
公共SecondLevelAdapter(树TR){
this.tree2 = TR;
}
//返回选定类的ID
@覆盖
公共对象getChild(INT groupPosition,诠释childPosition){
返回tree2.getChilds()得到(groupPosition).getChilds()得到(childPosition).getId()。
}
@覆盖
众长getChildId(INT groupPosition,诠释childPosition){
返回tree2.getChilds()得到(groupPosition).getChilds()得到(childPosition).getId()。
}
@覆盖
公共查看getChildView(INT groupPosition,INT childPosition,
布尔isLastChild,查看convertView,ViewGroup中父){
//第三层子
TextView的电视=新的TextView(上下文);
tv.setText(tree2.getChilds()得到(groupPosition).getChilds()得到(childPosition).getName());
如果(selectedClass = NULL和放大器;!&安培; selectedClass == tree2.getChilds()得到(groupPosition).getChilds()得到(childPosition).getId()){
tv.setBackgroundResource(R.drawable.divider_gray);
}
tv.setPadding(2 * CHILD_PADDING,5,300,5);
返回电视;
}
@覆盖
公众诠释getChildrenCount(INT groupPosition){
返回tree2.getChilds()得到(groupPosition).getChilds()的大小()。;
}
@覆盖
公共对象getGroup(INT groupPosition){
返回groupPosition;
}
@覆盖
公众诠释getGroupCount(){
返回tree2.getChilds()的大小()。
}
@覆盖
众长getGroupId(INT groupPosition){
返回groupPosition;
}
@覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,父母的ViewGroup)
{// secondlevel
TextView的电视=新的TextView(上下文);
tv.setText(tree2.getChilds()得到(groupPosition).getName());
tv.setPadding(CHILD_PADDING,7,200,7);
返回电视;
}
@覆盖
公共布尔hasStableIds(){
返回true;
}
@覆盖
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
返回true;
}
}
}
Tree.java
公共final类树{
最终静态字符串变量= Tree.class.getSimpleName();
最终私人字符串名称;
最后的私人诠释身份证;
私人树父;
私人链表<树>孩子的=新的LinkedList<树>();
公共树(字符串名称,诠释ID){
父=无效;
this.name =名称;
this.id = ID;
}
公共无效的addChild(树子){
child.parent =这一点;
childs.add(子);
}
公众诠释的getId(){
返回ID;
}
公共字符串的getName(){
返回名称;
}
公共树的getParent(){
回报父母;
}
公共链表<树> getChilds(){
回孩子的;
}
@覆盖
公共字符串的toString(){
迭代器<树> ITER = childs.iterator();
孩子的字符串=[;
而(iter.hasNext()){
树CH = iter.next();
蔡尔兹= childs.concat(CH +,);
}
蔡尔兹= childs.concat(]);
返回名称++编号+孩子的;
}
}
我没有测试你的code,但看的 onMeasure()
方法你的 CustExpListview
,我看到你指定的最大宽度和高度。如果您删除的最大高度,那么它应该能够适合所有的孩子。
I have created a tree as a double expandable list view , one of the items that has too many childs (15) more than the others , does not show all his childs.
_ ______
+|_______|
_______
+|_______|
_______
|_______|
_______
|_______|
...
...
_______
|_______| // 11-th item
_______ // 12-th item can be seen a little and it-s clickable
_______
+|_______|
_______
+|_______|
...
...
_______
+|_______|
After the 12'th item the list is cut. I have not put any constrains on the size of the list. Any idea why this happen ? Thanks!
EDIT This is the code for the firs expandable list .
<ExpandableListView android:layout_width="fill_parent" android:id="@+id/ParentLevel"
android:groupIndicator="@null" android:choiceMode="singleChoice"
android:layout_height="fill_parent"
android:layout_above="@id/button_layout"
android:paddingRight="5dp">
The other sublist are added in the adapter getView() method.
code example
//MainActivity.java
public class MainActivity extends Activity implements OnChildClickListener {
ExpandableListView elv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// QUI SOTTO
Tree root = new Tree("root", 123);
for (int i = 0; i < 3; i++) {
Tree firstLevel = new Tree(("firstLevel" + i), i);
for (int j = 0; j < 6; j++) {
Tree secondLevel = new Tree("secondLevel" + j, j);
for (int z = 0; z < 27; z++) {
Tree thirdLevel = new Tree("thirdLevel" + z, z);
secondLevel.addChild(thirdLevel);
}
firstLevel.addChild(secondlevel);
}
root.addChild(firstLevel);
}
elv = new ExpandableListView(this);
final TreeAdapter treeAdapter = new TreeAdapter(this, root, this);
elv.setAdapter(treeAdapter);
elv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
for (int i = 0; i < treeAdapter.getGroupCount(); i++) {
elv.collapseGroup(i);
}
elv.expandGroup(groupPosition);
return true;
}
});
setContentView(elv);
elv.expandGroup(0);
Entry e = treeAdapter.lsfirst[0];
e.cls.expandGroup(1);
treeAdapter.selectedClass = 33;
}
TreeAdapter.java
public class TreeAdapter extends BaseExpandableListAdapter {
final static String TAG = TreeAdapter.class.getSimpleName();
static public Integer selectedClass = null;
static int CHILD_PADDING;
static float TEXT_SIZE;
private Tree tree;
private OnChildClickListener lst;
final private Context context;
class Entry{
final CustExpListview cls;
final SecondLevelAdapter sadtp;
public Entry(CustExpListview cls, SecondLevelAdapter sadtp) {
this.cls = cls;
this.sadtp = sadtp;
}
}
Entry[] lsfirst;
public TreeAdapter(MainActivity ctx, Tree tree, OnChildClickListener lst){
this.context = ctx;
this.tree = tree;
this.lst = lst;
TEXT_SIZE = 35;
CHILD_PADDING = 40;
lsfirst = new Entry[tree.getChilds().size()];
for(int i=0; i<tree.getChilds().size();i++){
CustExpListview SecondLevelexplv = new CustExpListview(context);
SecondLevelAdapter adp = new
SecondLevelAdapter(tree.getChilds().get(i));
SecondLevelexplv.setAdapter(adp);
SecondLevelexplv.setGroupIndicator(null);
SecondLevelexplv.setOnChildClickListener(lst);// add listener
lsfirst[i] = new Entry(SecondLevelexplv, adp);
}
}
@Override
public Object getChild(int arg0, int arg1){
return arg1;
}
@Override
public long getChildId(int groupPosition, int childPosition){
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent){
//LISTA DI secondlevel
return lsfirst[groupPosition].cls;
}
@Override
public int getChildrenCount(int groupPosition){
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return tree.getChilds().size();
}
@Override
public long getGroupId(int groupPosition) {
return tree.getChilds().get(groupPosition).getId();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent){
//firstlevel
TextView tv = new TextView(context);
tv.setText(tree.getChilds().get(groupPosition).getName());
tv.setPadding(3, 7, 7, 7);
return tv;
}
@Override
public boolean hasStableIds(){
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/////////////////////////////////////////////////////////////////////////////
//_____________-------------________----------________---------_______-----//
/////////////////////////////////////////////////////////////////////////////
public class CustExpListview extends ExpandableListView{
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context)
{
super(context);
this.setSelector(R.drawable.divider_gray);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter{
Tree tree2;
public SecondLevelAdapter(Tree tr) {
this.tree2 = tr;
}
//Returns the id of the selected class
@Override
public Object getChild(int groupPosition, int childPosition) {
return tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getId();
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getId();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//thirdlevel
TextView tv = new TextView(context);
tv.setText(tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getName());
if(selectedClass!=null && selectedClass ==tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getId()){
tv.setBackgroundResource(R.drawable.divider_gray);
}
tv.setPadding(2*CHILD_PADDING, 5, 300, 5);
return tv;
}
@Override
public int getChildrenCount(int groupPosition){
return tree2.getChilds().get(groupPosition).getChilds().size();
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return tree2.getChilds().size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{// secondlevel
TextView tv = new TextView(context);
tv.setText(tree2.getChilds().get(groupPosition).getName());
tv.setPadding(CHILD_PADDING, 7, 200, 7);
return tv;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
Tree.java
public final class Tree {
final static String TAG=Tree.class.getSimpleName();
final private String name;
final private int id;
private Tree parent;
private LinkedList<Tree> childs = new LinkedList<Tree>();
public Tree(String name, int id) {
parent = null;
this.name = name;
this.id = id;
}
public void addChild(Tree child){
child.parent = this;
childs.add(child);
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Tree getParent() {
return parent;
}
public LinkedList<Tree> getChilds() {
return childs;
}
@Override
public String toString() {
Iterator<Tree> iter = childs.iterator();
String childs = "[";
while(iter.hasNext()){
Tree ch = iter.next();
childs = childs.concat(ch+",");
}
childs = childs.concat("]");
return name + " "+ id + childs;
}
}
I haven't tested your code, but looking at the onMeasure()
method of your CustExpListview
, I see that you have specified a maximum width and height. If you remove the maximum height, then it should be able to fit all the children.
这篇关于Android的双扩展列表视图隐藏项目,如果列表过大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!