在android中单击图像时显示下拉列表

时间:2010-09-09 05:51:56

标签: android

在我的Android应用程序中,我正在显示不同类别的图像。当我点击这些图像时,我想获得该特定类别中的项目的小列表。 我该怎么用呢。我不确定哪种控件能满足这个要求,我该怎样才能使用它。

有人可以建议我一个解决方案吗?

请分享您宝贵的建议。

提前致谢:)

1 个答案:

答案 0 :(得分:0)

将ExpandableListView与CursorAdapter一起使用。组视图可以是您的图像,子视图可以是文本。

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <ExpandableListView android:id="@id/android:list"
  android:layout_width="fill_parent" android:layout_height="wrap_content" />
 <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:gravity="center"
  android:layout_height="wrap_content" android:text="Sorry, no data" />
</LinearLayout>

public class myListy extends ExpandableListActivity {

 Cursor mCursor;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // Define group data in mCursor
  startManagingCursor(mCursor);

 ExpandableListView mExpandableListView = (ExpandableListView) findViewById(android.R.id.list);
  mExpandableListView.setGroupIndicator(null);

EAdapter adapter = new EAdapter(mCursor, getApplicationContext());
  mExpandableListView.setAdapter(adapter);
}

private class EAdapter extends CursorTreeAdapter {

  public EAdapter(Cursor cursor, Context context) {
   super(cursor, context);
  }

  @Override
  protected void bindChildView(View view, Context context, Cursor cursor,
    boolean isLastChild) {

   // Add your data to the child.

  }

  @Override
  protected void bindGroupView(View view, Context context, Cursor cursor,
    boolean isExpanded) {

   // Add your data to the group.

  }

  @Override
  protected View newChildView(Context context, Cursor cursor,
    boolean isLastChild, ViewGroup parent) {

   View view = getLayoutInflater().inflate(
     R.layout.text_layout, null);

   return view;
  }

  @Override
  protected View newGroupView(Context context, final Cursor cursor,
    boolean isExpanded, ViewGroup parent) {

                                View view = getLayoutInflater().inflate(
     R.layout.images_layout, null);

   return view;
  }

  @Override
  protected Cursor getChildrenCursor(Cursor groupCursor) {
                        // data for childern 

   return cursor;
  }
 }