0
回答
android ExpandableListView
华为云4核8G,高性能云服务器,免费试用   

为什么ExpandableListView中除第一个item可见,其它只要有一个不可见,第一个item就变成不可见。

为什么点击ExpandableListview中上的箭头,例如第一次点击是1,2,3,4。第二次点击是4,3,2,1。

package com.example.testproject;

import android.content.Context;
import android.media.tv.TvContentRating;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


public class TestAdapter extends BaseExpandableListAdapter {


char s[] = { 'a' };
char c[] = { '1', '2', '3', '4' };
private Context mContext;
private View view;
private TextView tv;


public TestAdapter(Context context) {
mContext = context;
}


@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return s.length;
}


@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return c.length;
}


@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}


@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}


@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}


@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}


@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}


@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
TextView groupView = new TextView(mContext);
groupView.setLayoutParams(lp);
groupView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
groupView.setPadding(40, 20, 0, 20);
groupView.setTextSize(14);
groupView.setText(String.valueOf(s[groupPosition]));
return groupView;
}


@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub


if (convertView == null) {
view = View.inflate(mContext, R.layout.test, null);
tv = (TextView) view.findViewById(R.id.tv);
} else {
view = convertView;
}
tv.setText(String.valueOf(c[childPosition]));
tv.setVisibility(View.VISIBLE);
//tv.setText(String.valueOf(c[childPosition]));
char a = c[childPosition];
System.out.println(a);
// switch (a) {
// case '1':
// tv.setText(String.valueOf(c[childPosition]));
// break;
// case '2':
// tv.setVisibility(View.GONE);
// break;
// case '3':
// tv.setText(String.valueOf(c[childPosition]));
// break;
// case '4':
// tv.setText(String.valueOf(c[childPosition]));
// break;
// default:
// break;
// }
return view;
}


@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}


}


package com.example.testproject;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;


public class TestActivity extends Activity {
private ExpandableListView listview;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ExpandableListView) findViewById(R.id.el);
TestAdapter ta=new TestAdapter(this);
listview.setAdapter(ta);
int groupCount = listview.getCount();


for (int i=0; i<groupCount; i++) {


listview.expandGroup(i);


};

<?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:orientation="vertical" >
    <ExpandableListView
       android:id="@+id/el"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp"
        android:cacheColorHint="#00000000"
   
        android:choiceMode="singleChoice"
    
        android:scrollingCache="false">
    </ExpandableListView>


</LinearLayout>

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:gravity="center_vertical"
    android:minHeight="50dp"
    android:orientation="vertical" 


    >
 
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_weight="4"
        android:gravity="left|center_vertical"
        android:text="1" />


    
    </LinearLayout>

举报
HappyLin
发帖于2年前 0回/129阅
顶部