Android中的单个活动中有多个Listviews?

时间:2010-04-05 05:12:42

标签: android listview android-activity

我希望在单个活动中拥有多个列表视图。但只有一个 列表视图应该一次显示。列表视图将被加载 动态。那么,我怎样才能同时填写所有四个列表视图 时间只显示一个?

4 个答案:

答案 0 :(得分:4)

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

-Moto

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我使用的方法大多与您一样显示多个列表视图 建议即使用setvisibility。

但现在我想要的是拥有所有四个的Bitmap图像 列表视图。假设,我的活动开始并且数据被加载到 所有四个列表视图通过将它们绑定到各自的适配器。 现在,当我尝试使用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