2014-09-01 84 views
0

我想动态地将多个horizo​​ntalscrollView添加到线性布局。为此,我使用id为mainList的linearLayout。和一个带有horizo​​ntalScrollLayout的xml,如下所示。它不工作。什么是写入方式。如何动态添加多个horizo​​ntalScrollView

mainListView = (LinearLayout) findViewById(R.id.mainList); 
hrscroll = (HorizontalScrollView) findViewById(R.id.hrscroll); 
// hrtxt=(TextView) findViewById(R.id.hrtxt); 
for(int i=0;i<categories.length;i++) { 
    mainListView.addView(hrscroll); 
} 

XML为horizo​​ntalScrollLayout

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/hrscroll" 
    android:layout_height="wrap_content"> 
</HorizontalScrollView> 
+0

你不能一次又一次地添加相同的布局。 – SilentKiller 2014-09-01 12:29:50

+0

但我想显示图像滑动到另一个分类wize.How我可以做到这一点。 – Manasi 2014-09-01 12:31:31

回答

0

添加HorizontalScrollView动态的LinearLayout

mainListView = (LinearLayout) findViewById(R.id.mainList); 
// Creating mHorizontalScrollView array for multiple Categories 
HorizontalScrollView[] mHorizontalScrollView = new HorizontalScrollView[categories.length]; 
for(int i = 0; i < categories.length; i++) { 
    mHorizontalScrollView[i] = new HorizontalScrollView(context); 
    mainListView.addView(mHorizontalScrollView[i]); 
} 

更新答:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/mainListView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="8dp" > 

    <HorizontalScrollView 
     android:id="@+id/categoryOne" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <HorizontalScrollView 
     android:id="@+id/categoryTwo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <HorizontalScrollView 
     android:id="@+id/categoryThree" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <HorizontalScrollView 
     android:id="@+id/categoryFour" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <HorizontalScrollView 
     android:id="@+id/categoryFive" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <HorizontalScrollView 
     android:id="@+id/categorySix" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <HorizontalScrollView 
     android:id="@+id/categorySeven" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

活动文件:

绑定XML浏览:

mainListView = (LinearLayout) findViewById(R.id.mainList); 
categoryOne = (HorizontalScrollView) findViewById(R.id.categoryOne); 
categoryTwo = (HorizontalScrollView) findViewById(R.id.categoryTwo); 
categoryThree = (HorizontalScrollView) findViewById(R.id.categoryThree); 
categoryFour = (HorizontalScrollView) findViewById(R.id.categoryFour); 
categoryFive = (HorizontalScrollView) findViewById(R.id.categoryFive); 
categorySix = (HorizontalScrollView) findViewById(R.id.categorySix); 
categorySeven = (HorizontalScrollView) findViewById(R.id.categorySeven); 
+0

您的类别是预定义的还是动态生成的? – SilentKiller 2014-09-01 12:53:58

+0

然后,为什么要在这里添加Horizo​​ntalScrollView,只需将它添加到LinearLayout中的XML本身中,然后使用Java进行绑定即可。你有多少种类? – SilentKiller 2014-09-01 13:08:37

+0

7类别。现在我已经把它们保存在XML中。后来如果这个工作,我想把他们放在一个JSON文件,并保持在服务器上。没有类别可能会改变。 – Manasi 2014-09-01 13:14:00

相关问题