2010-04-05 40 views
0

我想在单个活动中有多个列表视图。但是一次只能显示一个 列表视图。列表视图将被动态加载 。那么,我应该如何在同一个 时间填写所有四个列表视图,并且只显示一个?Android中单个Activity中的多个Listviews?

+0

仅显示一个手段,你要隐藏的树的其余部分,或者您希望只显示一个在时间和具体行动要显示另一个。 无论你想做什么,都可以做一件事, 你可以实现代码,它将加载所有3-4列表视图中的数据,意味着创建适配器并与列表视图绑定。然后隐藏3.的其余部分,并且可以根据需要逐个显示它们。 – 2010-04-05 05:19:51

回答

0

我使用的方式基本上与显示多个列表视图的方式相同,如 ,建议使用setvisibility。

但现在我想要的是所有四个 列表视图的位图图像。假设我的活动开始,并且通过将数据绑定到它们各自的适配器,将数据加载到全部四个列表视图中。 现在,当我试图使用getDrawingCache()来获取位图时,它总是向我返回null。

那么,你可以让我知道如何获取所有四个 列表视图的位图?

public class HomeScreenActivity extends Activity 
{ 
private ImageView imgBabble = null; 
private ListView listBabble = null; 
private ListView listVille = null; 
private ListView listPrivateMsgs = null; 
private ListView listBookmarks = null; 
private List<String> tempBabble = null; 
private List<String> tempVille = null; 
private List<String> tempPrivateMsgs = null; 
private List<String> tempBookmarks = null; 

//RelativeLayouts 
private RelativeLayout babbleLayout = null; 
private RelativeLayout villeLayout = null; 
private RelativeLayout privateMsgsLayout = null; 
private RelativeLayout bookmarksLayout = null; 

//Bitmap 
private Bitmap babbleShrunkView = null; 
private Bitmap villeShrunkView = null; 
private Bitmap privateMsgsShrunkView = null; 
private Bitmap bookmarksShrunkView = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.homescreen); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitle); 

    imgBabble = (ImageView) findViewById(R.id.imgBtnBabble); 
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.babble_top_logo); 
    BitmapDrawable bmpDrawable = new BitmapDrawable(bitmapOrg); 
    imgBabble.setBackgroundDrawable(bmpDrawable); 

    //Initialize the layouts for Babble, Ville, Private Messages and Bookmarks 
    babbleLayout = (RelativeLayout) findViewById(R.id.babbleLayout); 
    babbleLayout.setDrawingCacheEnabled(true); 

    villeLayout = (RelativeLayout) findViewById(R.id.villeLayout); 
    villeLayout.setDrawingCacheEnabled(true); 

    privateMsgsLayout = (RelativeLayout) findViewById(R.id.privatemsgsLayout); 
    privateMsgsLayout.setDrawingCacheEnabled(true); 

    bookmarksLayout = (RelativeLayout) findViewById(R.id.bookmarksLayout); 
    bookmarksLayout.setDrawingCacheEnabled(true); 

    //Babble 
    tempBabble = new ArrayList<String>(); 
    tempBabble.add("First Babble"); 
    tempBabble.add("Second Babble"); 
    tempBabble.add("Third Babble"); 

    listBabble = (ListView) findViewById(R.id.listBabble); 
    listBabble.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempBabble)); 
    //babbleShrunkView = babbleLayout.getDrawingCache(); 

    //babbleLayout = (RelativeLayout) findViewById(R.id.listLayout); 

    if(babbleShrunkView == null) 
    { 
     System.out.println("Babble View is null"); 
    } 

    //Ville 
    tempVille = new ArrayList<String>(); 
    tempVille.add("First Ville"); 
    tempVille.add("Second Ville"); 
    tempVille.add("Third Ville"); 

    listVille = (ListView) findViewById(R.id.listVille); 
    //listVille.setDrawingCacheEnabled(true); 
    listVille.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempVille)); 
    //villeShrunkView = villeLayout.getDrawingCache(); 

    if(villeShrunkView == null) 
    { 
     System.out.println("Ville View is null"); 
    } 

    //listVille.setVisibility(View.GONE); 

    //Private Messages 
    tempPrivateMsgs = new ArrayList<String>(); 
    tempPrivateMsgs.add("First PM"); 
    tempPrivateMsgs.add("Second PM"); 
    tempPrivateMsgs.add("Third PM"); 

    listPrivateMsgs = (ListView) findViewById(R.id.listPrivateMessages); 
    //listPrivateMsgs.setDrawingCacheEnabled(true); 
    listPrivateMsgs.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempPrivateMsgs)); 
    //privateMsgsShrunkView = privateMsgsLayout.getDrawingCache(); 

    if(privateMsgsShrunkView == null) 
    { 
     System.out.println("Private Messages View is null"); 
    } 

    //listPrivateMsgs.setVisibility(View.GONE); 

    //Bookmarks 
    tempBookmarks = new ArrayList<String>(); 
    tempBookmarks.add("First Bookmarks"); 
    tempBookmarks.add("Second Bookmarks"); 
    tempBookmarks.add("Third Bookmarks"); 

    listBookmarks = (ListView) findViewById(R.id.listBookmarks); 
    //listBookmarks.setDrawingCacheEnabled(true); 
    listBookmarks.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempBookmarks)); 
    //bookmarksShrunkView = bookmarksLayout.getDrawingCache(); 

    if(bookmarksShrunkView == null) 
    { 
     System.out.println("Bookmarks Messages View is null"); 
    } 

    //listBookmarks.setVisibility(View.GONE); 

    imgBabble.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      System.out.println("Babble Clicked"); 

      babbleShrunkView = babbleLayout.getDrawingCache(); 
      villeShrunkView = villeLayout.getDrawingCache(); 
      privateMsgsShrunkView = privateMsgsLayout.getDrawingCache(); 
      bookmarksShrunkView = bookmarksLayout.getDrawingCache(); 

      BabbleVilleShrinkView.addBitmap(0, resizeBitmap(200, 200, babbleShrunkView)); 
      BabbleVilleShrinkView.addBitmap(1, resizeBitmap(200, 200, villeShrunkView)); 
      BabbleVilleShrinkView.addBitmap(2, resizeBitmap(200, 200, privateMsgsShrunkView)); 
      BabbleVilleShrinkView.addBitmap(3, resizeBitmap(200, 200, bookmarksShrunkView)); 

      Gallery gallery = new Gallery(getApplicationContext()); 
      gallery.setAdapter(new ImageAdapter(getApplicationContext())); 
     } 
    }); 
} // End of class 
+0

我能够通过将所有列表视图的setVisibility设置为Visible来解决此问题,然后通过getDrawingCache获取位图。 – sunil 2010-06-01 10:39:17

0

你可以为你的问题做一件事。您只会看到一个列表视图的原因可能是因为一个列表的高度将具有填充父参数值。你必须做的是,使用绝对布局或者只是为每个布局修正高度。还可以使用滚动视图来放置控件。此外,如果您在此处发布您的XML布局,则可以更轻松地回答您的查询以了解需要进行更改的位置。

0

您应该使用FrameLayout;它允许对其子视图进行z排序,这意味着您可以将您的ListView堆叠在一起。然后,您可以使用View#setVisibility()方法从代码中切换每个ListView的可见性。 您将为每个这些ListView创建合适的适配器,并且无论何时要绘制ListView,框架都会调用所需适配器的getView()方法。

+0

我已经使用了大部分显示多个列表视图的相同方式,就像您使用setvisibility参数所建议的那样。但现在我想要的是所有四个列表视图的位图图像。假设我的活动开始,并且通过将数据绑定到它们各自的适配器,将数据加载到所有四个列表视图中。现在,当我试图使用getDrawingCache()获取位图时,它总是向我返回null。 那么,你能告诉我如何获取所有四个列表视图的位图吗? – sunil 2010-04-05 12:05:43

4

而不是使用所有的代码在列表视图之间翻转为什么不把所有的ListView放在ViewFlipper中?它是最简单的方法,需要几乎1行的代码! :)

-Moto

相关问题