1

嗨我想將視圖動態添加到線性佈局。動態水平視圖

我只需要以下

從Web服務我將得到類別名稱和圖像的URL,並試圖所有類別下的水平視圖添加到線性佈局使用addview功能,但只有最後一次類別被顯示。

請看下面的圖片我需要這樣一個佈局被標記爲紅色的矩形部分必須動態加載圖像類別,該部分應該可水平滾動。在advacnce

我被困在這裏的感謝。

enter image description here

+0

請您否決像情侶別人這裏之前沒有,請發表評論,並說有什麼錯的問題,否則,我無法改善它! – 2014-09-30 08:41:28

回答

1

簡單的創建自定義的水平列表視圖。用此來生成列表視圖,並使用陣列適配器設置數據

這是我的工作示例enter link description here

在你的佈局XML

<com.jeekiarn.horizontal_listview.HorizoantalListView 
    android:id="@+id/hlvSimpleList" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" /> 


mHlvSimpleList = (HorizoantalListView) findViewById(R.id.hlvSimpleList); 
    CustomArrayAdapter adapter = new CustomArrayAdapter(this, mCustomData); 

    // Assign adapter to HorizontalListView 
    mHlvSimpleList.setAdapter(adapter); 
    mHlvSimpleList.setAdapter(adapter); 
2

創建自定義水平列表視圖像下面 並結合您的適配器類,它

package com.sujith.custom_layout; 
import android.content.Context; 
import android.database.DataSetObserver; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.Adapter; 
import android.widget.HorizontalScrollView; 
import android.widget.LinearLayout; 
import android.widget.FrameLayout.LayoutParams; 

public class GalleryHorizontal extends HorizontalScrollView{ 

private LinearLayout.LayoutParams defaultTabLayoutParams; 
private LinearLayout.LayoutParams expandedTabLayoutParams; 


private LinearLayout tabsContainer; 
private Adapter adapter; 
private DataSetObserver dataSetObserver=new DataSetObserver() { 

     @Override 
     public void onChanged() { 
      // TODO Auto-generated method stub 
      super.onChanged(); 
      reloadChildViews(); 
     } 

    }; 
public GalleryHorizontal(Context context) { 
    this(context, null); 
    // TODO Auto-generated constructor stub 
} 

public GalleryHorizontal(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
    // TODO Auto-generated constructor stub 
} 

public GalleryHorizontal(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 

    setFillViewport(true); 
//  setWillNotDraw(false); 

    tabsContainer = new LinearLayout(context); 
    tabsContainer.setOrientation(LinearLayout.HORIZONTAL); 
    tabsContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    addView(tabsContainer); 



} 
public void setAdapter(Adapter adapter) { 
    if (this.adapter == adapter) return; 
    this.adapter = adapter; 
    if (adapter != null) adapter.registerDataSetObserver(dataSetObserver); 
    reloadChildViews(); 
} 

private void reloadChildViews() { 
    tabsContainer.removeAllViews(); 
     if (adapter == null) return; 
     int count = adapter.getCount(); 
     for (int position = 0; position < count; ++position) { 
      View v = adapter.getView(position, null, this); 
      if (v != null){ 

       tabsContainer.addView(v); 

      } 
     } 

     tabsContainer.requestLayout(); 

} 

}

+1

男人很好。保持它 – Jeekiran 2014-10-07 06:52:48