2012-09-20 38 views
0

我目前有一些滑动页面,在所有这些页面中,我想列出具有不同数据的列表。
我仍然很新到Android,所以我尝试了所有可能的选择,我可以想出:如何将ListView放置在页面内?

  1. 首先我尝试添加一些XML布局页面的内部。
  2. 第二我试着创造和实际填充列表。

以上都不适合我。

对于初学者我要检查,如果我理解这个概念:
刷卡布局包含页面和数据可以通过getItem()Fragment类型的内容传递给他们。

现在怎么达到我想要的:
我要补充LinearLayout添加子ListView最后加一个孩子的TextView元素主要页面模板在我layout/main.xml,并创建一个数据群的方法。

我的推理有什么问题,所以我找不到答案,或者我找不到合适的来源?

代码示例:
main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <!-- 
    This title strip will display the currently visible page title, as well as the page 
    titles for adjacent pages. 
    --> 

    <android.support.v4.view.PagerTitleStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:background="#33b5e5" 
     android:paddingBottom="4dp" 
     android:paddingTop="4dp" 
     android:textColor="#fff" /> 

</android.support.v4.view.ViewPager> 

list_layout.xml

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

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

MainActivity.java

...  
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Create the adapter that will return a fragment for each of the three primary sections 
     // of the app. 

     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 


     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

    } 
... 

回答

0

为此,您可以通过声明的XML说listview.xml其中定义你的ListView。 您可以用语法创建一个新的TextView pro或创建新的布局,例如titleview.xml,其中使用TextView编写标题视图。

从语法上来说,你可以给这两个视图充气并将你的主布局父视图作为父视图传递给这些子视图。

LayoutInflater inflater = getLayoutInflater(); 
    ListView listview = inflater.inflate(R.layout.list_view, parent); 
    TextView textview = inflater.inflate(R.layout.textview, parent); 
    layout.addView(textview); 
    layout.addView(listview); 
+0

我在哪里获得父级和布局变量?我试过这个: RelativeLayout item =(RelativeLayout)findViewById(R.id.pager); 查看列表= getLayoutInflater()。inflate(R.layout.list_view,null); item.addView(list);不工作。 – PovilasID

+0

布局是您主视图布局的参考。就像如果你有布局显示在屏幕顶部的标题和屏幕中间的列表视图。然后,textview将是您的第一个布局,然后声明另一个父布局可以是LinearLayout或RelativeLayout。在您的活动中获取此布局的参考。现在将膨胀的视图添加到您的布局。 linearlayout.add(列表视图);如果你传递父视图,那么android将应用父子视图的所有属性。否则,您可以传递null来代替父代。 – knvarma

相关问题