2013-08-21 3 views
4

나는 프래그먼트에 expandablelistview를 추가하는 방법을 찾고 있었지만 문서를 읽었지만 어떻게해야 할 지 혼란 스럽다. 따라서 다음과 같은 두 가지 질문이 있습니다.expandableListView 예제를 프래그먼트로 바꿈

  1. 내가 이것을 돌릴 수있는 방법이 있습니까 : http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ 조각에?

  2. 아래 코드에서 android:groupIndicator의 표시기를 expandablelistview에 추가하려고 시도했지만 표시되지 않습니다. 여기에 코드가 있습니다. 직접 지표에 추가 할 수있는 방법이 있습니까?

    public class Services extends SherlockFragment { 
    
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.services_layout, null); 
    ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.list); 
    elv.setAdapter(new SavedTabsListAdapter()); 
    return v; 
    } 
    
    public class SavedTabsListAdapter extends BaseExpandableListAdapter { 
    
    private String[] groups = { "Class 13", "Class 12", "Class 11", "Class 10", "Class 9" }; 
    
    private String[][] children = { 
        { "Blue", "Green" }, 
        { "Orange", "Purple", "Black" }, 
        { "White", "Brown" }, 
        { "Golden", "Silver", "Red" }, 
        { "Pink", "Gray", "Yellow" } 
    }; 
    
    @Override 
    public int getGroupCount() { 
        return groups.length; 
    } 
    
    @Override 
    public int getChildrenCount(int i) { 
        return children[i].length; 
    } 
    
    @Override 
    public Object getGroup(int i) { 
        return groups[i]; 
    } 
    
    @Override 
    public Object getChild(int i, int i1) { 
        return children[i][i1]; 
    } 
    
    @Override 
    public long getGroupId(int i) { 
        return i; 
    } 
    
    @Override 
    public long getChildId(int i, int i1) { 
        return i1; 
    } 
    
    @Override 
    public boolean hasStableIds() { 
        return true; 
    } 
    
    @Override 
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { 
        TextView textView = new TextView(Services.this.getActivity()); 
        textView.setText(getGroup(i).toString()); 
        textView.setBackgroundColor(0xFF3F9FE0); 
        return textView; 
    } 
    
    @Override 
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { 
        TextView textView = new TextView(Services.this.getActivity()); 
        textView.setText(getChild(i, i1).toString()); 
        return textView; 
    } 
    
    @Override 
    public boolean isChildSelectable(int i, int i1) { 
        return true; 
    } 
    

    } }

답변

0
  1. 여기에 문제 없습니다, 당신은 당신이 문맥을해야합니다 때, 당신은 단지 getActivity()를 사용, 활동과 같은 방법으로이 작업을 수행 할 수 있습니다.
  2. xml 코드 android:groupIndicator="@drawable/indicator_pic" 또는 프로그래밍 방식으로 expanadableListView.setGroupIndicator(R.drawable.your_pic);. 비슷한 것은 here입니다. 저에게 물어보십시오.
1

다음과 비슷한 것을 사용할 수 있습니다. 나를 위해 일했습니다 :

public static class LineupFragment extends Fragment { 

View rootView; 
ExpandableListView lv; 
private String[] groups; 
private String[][] children; 


public LineupFragment() { 

} 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    groups = new String[] { "Test Header 1", "Test Header 2", "Test Header 3", "Test Header 4" }; 

    children = new String [][] { 
     { "s simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." }, 
     { "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of comes from a line in section 1.10.32." }, 
     { "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)." }, 
     { "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc." } 
    }; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.fragment_lineup, container, false); 

return rootView; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    lv = (ExpandableListView) view.findViewById(R.id.expListView); 
    lv.setAdapter(new ExpandableListAdapter(groups, children)); 
    lv.setGroupIndicator(null); 

} 

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private final LayoutInflater inf; 
    private String[] groups; 
    private String[][] children; 

    public ExpandableListAdapter(String[] groups, String[][] children) { 
     this.groups = groups; 
     this.children = children; 
     inf = LayoutInflater.from(getActivity()); 
    } 

    @Override 
    public int getGroupCount() { 
     return groups.length; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return children[groupPosition].length; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return children[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 getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 

     ViewHolder holder; 
      if (convertView == null) { 
       convertView = inf.inflate(R.layout.list_item, parent, false); 
       holder = new ViewHolder(); 

       holder.text = (TextView) convertView.findViewById(R.id.lblListItem); 
       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      holder.text.setText(getChild(groupPosition, childPosition).toString()); 

      return convertView; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     ViewHolder holder; 

     if (convertView == null) { 
      convertView = inf.inflate(R.layout.list_group, parent, false); 

      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.lblListHeader); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.text.setText(getGroup(groupPosition).toString()); 

     return convertView; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    private class ViewHolder { 
     TextView text; 
    } 
} 
관련 문제