如何实现可扩展列表项?

时间:2014-04-04 18:55:34

标签: android

我是Android开发的新手。我有一个显示带有小图像和名称的项目列表。单击列表项时,应在展开视图中显示带图像的项目(可能是新布局)的详细说明,而其他列表项逐渐下降,为展开视图提供空间。有人可以就如何实现这个目标给我一些指导吗?

1 个答案:

答案 0 :(得分:0)

public class ExpandableProjectsListAdapter extends BaseExpandableListAdapter {

   private final SparseArray<Group> groups;
   public LayoutInflater inflater;
   public Activity activity;

   public ExpandableProjectsListAdapter(Activity act, SparseArray<Group> groups) {
      activity = act;
      this.groups = groups;
      inflater = act.getLayoutInflater();
   }

   @Override
   public Object getChild(int groupPosition, int childPosition) {
      return groups.get(groupPosition).children.get(childPosition);
   }

   @Override
   public long getChildId(int groupPosition, int childPosition) {
     return 0;
   }

   @Override
   public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
      final String children = (String) getChild(groupPosition, childPosition);
      TextView text = null;
      if (convertView == null) {
          convertView = inflater.inflate(R.layout.listrow_details, null);
      }
      text = (TextView) convertView.findViewById(R.id.textView1);
      text.setText(children);
      convertView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(activity, children,
                    Toast.LENGTH_SHORT).show();
         }
      });
      return convertView;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
     return groups.get(groupPosition).children.size();
  }

  @Override
  public Object getGroup(int groupPosition) {
     return groups.get(groupPosition);
  }

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

  @Override
  public void onGroupCollapsed(int groupPosition) {
     super.onGroupCollapsed(groupPosition);
  }

  @Override
  public void onGroupExpanded(int groupPosition) {
     super.onGroupExpanded(groupPosition);
  }

  @Override
  public long getGroupId(int groupPosition) {
     return 0;
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
      if (convertView == null) {
          convertView = inflater.inflate(R.layout.listrow_group, null);
      }
      Group group = (Group) getGroup(groupPosition);
      ((CheckedTextView) convertView).setText(group.string);
      ((CheckedTextView) convertView).setChecked(isExpanded);
      return convertView;
  }

  @Override
  public boolean hasStableIds() {
     return false;
  }

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

在你的片段中,做

public class ProjectsFragment extends Fragment {

  Activity activity;
  SparseArray<Group> groups = new SparseArray<Group>();

  @Override
  public void onAttach(Activity activity){
     super.onAttach(activity);
     this.activity = activity;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

      View rootView = inflater.inflate(R.layout.fragment_projects, container, false);

      createData();
      ExpandableListView listView = (ExpandableListView)    rootView.findViewById(R.id.projects);
      ExpandableProjectsListAdapter adapter = new ExpandableProjectsListAdapter(activity,
            groups);
      listView.setAdapter(adapter);

      return rootView;
 }

 public void createData() {
    for (int j = 0; j < 10; j++) {
        Group group = new Group("Project " + j);
        for (int i = 0; i < 1; i++) {
            group.children.add("Learn programming using a simple project developing android applications and be guided by a mentor free of charge");
        }
        groups.append(j, group);
    }
   }
  }

对于xml布局,您需要:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

        <ExpandableListView
                android:id="@+id/projects"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
        </ExpandableListView>
</LinearLayout>