2014-09-01 52 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

相關問題