我正在尝试将值特别添加到父groupPosition [0]中。但是它反映到父列表项的所有groupPositions。蝶草帮助我。
public class MainActivity extends Activity
{
ExpandableListView explvlist;
ArrayList<String> StringArray = new ArrayList<String>();
ArrayList<String> StringArrayChild=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayValueAddFunction();
ArrayValueAddFunction_Level2();
explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
explvlist.setAdapter(new ParentLevel(StringArray));
}
private void ArrayValueAddFunction_Level2() {
StringArrayChild.add("Level1 item1");
StringArrayChild.add("Level1 item2");
StringArrayChild.add("Level1 item3");
StringArrayChild.add("Level1 item4");
StringArrayChild.add("Level1 item5");
StringArrayChild.add("Level1 item6");
}
private void ArrayValueAddFunction() {
StringArray.add("ONE");
StringArray.add("TWO");
StringArray.add("THREE");
StringArray.add("FOUR");
StringArray.add("FIVE");
StringArray.add("SIX");
}
public class ParentLevel extends BaseExpandableListAdapter {
ArrayList<String> StringArray = new ArrayList<String>();
private Context context;
public ParentLevel(ArrayList<String> stringArray) {
this.StringArray=stringArray;
}
@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) {
CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}
@Override
public int getChildrenCount(int groupPosition) {
return 3;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return StringArray.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
if (groupPosition==0) {
tv.append(StringArray.get(0));
}else if (groupPosition==1){
tv.append(StringArray.get(1));
}else if (groupPosition==2){
tv.append(StringArray.get(2));
}else if (groupPosition==3){
tv.append(StringArray.get(3));
}else if (groupPosition==4){
tv.append(StringArray.get(4));
}else if (groupPosition==5){
tv.append(StringArray.get(5));
}
return tv;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public class SecondLevelAdapter extends BaseExpandableListAdapter
{
@Override
public Object getChild(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("child");
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.GRAY);
tv.setLayoutParams(new ListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
return tv;
}
@Override
public int getChildrenCount(int groupPosition) {
return 6;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
TextView tv = new TextView(MainActivity.this);
if (groupPosition==0) {
tv.append(StringArrayChild.get(0));
}else if (groupPosition==1){
tv.append(StringArrayChild.get(1));
}else if (groupPosition==2){
tv.append(StringArrayChild.get(2));
}else if (groupPosition==3){
tv.append(StringArrayChild.get(3));
}else if (groupPosition==4){
tv.append(StringArrayChild.get(4));
}else if (groupPosition==5){
tv.append(StringArrayChild.get(5));
}
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.YELLOW);
return tv;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
}
}
CustExpListview
class CustExpListview extends ExpandableListView
{
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) {
super(context);
}
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);
}
}
具有expandableListView的布局设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<ExpandableListView
android:layout_width="fill_parent"
android:id="@+id/ParentLevel"
android:groupIndicator="@null"
android:layout_height="fill_parent">
</ExpandableListView>
</LinearLayout>
下面给出的图像是输出,所有项目都进入所有位置,但是我需要放置position [0],但它反映到父列表的所有groupPositins
答案 0 :(得分:1)
我认为问题发生在getGroupView
。 groupPosition
始终返回0。检查您的代码以解决该问题。
顺便说一下,您可以使用此NLeveLExpandableListView。