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