2011-11-29 103 views
1

我试图构建我的第一个自定义gui元素,但当我尝试在代码中稍后更改外观或适配器(使用Gallery)时遇到问题。充气自定义图库布局更改属性失败

我的问题:我不能改变自定义画廊性质

我的实际代码:

首先我创建一个XML这是小工具customGallery.xml

<?xml version="1.0" encoding="utf-8"?> 


<merge xmlns:android="http://schemas.android.com/apk/res/android">  
    <ImageButton android:id="@+id/toLeft" 
    android:background="@drawable/arrow_left" 
     android:layout_width="wrap_content" 
     android:layout_height="40dip" 
     android:layout_marginBottom="1dip" /> 
    <Gallery 
     android:id="@+id/gallery" 
     android:layout_width="fill_parent" 
     android:layout_height="40dip" 
     android:layout_toRightOf="@+id/toLeft" 
     android:spacing="40dip" 
     android:scrollbars="horizontal"/> 

    <ImageButton android:id="@+id/toRight" 
     android:background="@drawable/arrow_right" 
     android:layout_width="wrap_content" 
     android:layout_height="40dip" 
     android:layout_toRightOf="@+id/gallery" /> 
</merge> 

后来我创建test.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/lin_layout" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<com.Test.GallerySlider 
    android:id="@+id/choose" 
    android:layout_span="2" 
    android:layout_width="300dip" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

我的下一步是包含此自定义控件到我的项目,并从我的窗口小部件更改适配器:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    LinearLayout lineLayout = (LinearLayout) findViewById(R.id.lin_layout); 
    ViewStub st3 = new ViewStub(TestwidgetActivity.this); 
    LinearLayout.LayoutParams paramst3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    lineLayout.addView(st3,paramst3); 
    st3.setLayoutResource(R.layout.test); 
    View st3InflaView =st3.inflate(); 
    GallerySlider gSlider= (GallerySlider) st3InflaView.findViewById(R.id.choose); 
    gSlider.setNewAdapter(new ArrayAdapter<String>(this, android.R.layout.customGallery, new String[] {"1 ","2","3","4"})); 
} 

这是Widgetclass我写道:

public class GallerySlider extends RelativeLayout implements OnClickListener { 
    private ArrayAdapter<String> adapter; 
private Gallery gallery; 
private ImageButton toLeftBtn = null; 
private ImageButton toRightbtn = null; 

public GallerySlider(Context context) { 
    super(context, null); 
    init(context); 
} 

public GallerySlider(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

public GallerySlider(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs); 
    init(context); 
} 

public void init(Context ctx){ 
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ; 
    inflater.inflate(R.layout.customGallery, this, true); 

    toLeftBtn = (ImageButton) findViewById(R.id.toLeft); 
    toLeftBtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if(gallery.getSelectedItemPosition() > 0){ 
       gallery.setSelection(gallery.getSelectedItemPosition()-1); 
      } 
     } 

    }); 

    toRightbtn = (ImageButton) findViewById(R.id.toRight); 
    toRightbtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if(gallery.getSelectedItemPosition() < gallery.getAdapter().getCount()-1){ 
       gallery.setSelection(gallery.getSelectedItemPosition()+1); 
      } 
     } 

    }); 
    adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_gallery_item, new String[] {"1","1", 
                            "1")}); 
    gallery = (Gallery) findViewById(R.id.gallery); 
    gallery.setBackgroundResource(R.drawable.h_sliderl); 
    gallery.setAdapter(adapter); 
} 



@Override 
public void onClick(DialogInterface dialog, int which) { 

    switch(which){ 
    case R.id.toLeft: gallery.setSelection(gallery.getFocusedChild().getId()-1); 
         break; 

    case R.id.toRight: gallery.setSelection(gallery.getFocusedChild().getId()+1); 
         break; 
    } 

} 


public void setNewAdapter(ArrayAdapter<String> _adapter){ 

     gallery.setAdapter(_adapter); 
     ((ArrayAdapter) gallery.getAdapter()).notifyDataSetChanged(); 
} 

}

如果我打电话setNewAdapter(ArrayAdapter _adapter)没有什么变化..。我也试图改变画廊的大小,但它也失败了(nothig发生)。我的方法是否为假?
问候马塞尔

回答

0

我可以检测到的第一件事是您正在创建您的自定义视图两次。

当您设置布局时,第一次创建与ViewStub发生。 而第二个,当您膨胀R.layout.test时不会将其添加到contentView

您正在将适配器设置为未添加到视图层次结构中的第二个自定义视图。

+0

你好,我改变这一点。但调用setnewAdapter()没有什么变化的问题是实际存在的..我认为notyfiyDataSetChange将解决问题。任何想法? – marcel

+0

@marcel:Plz,使用更新修改您的问题中的代码。 – Macarse

+0

你好,我是sry ..我发现我的错误我现在使用查看st3InflaView = st3.inflate(); GallerySlider gSlider =(GallerySlider)st3InflaView.findViewById(R.id.choose);感谢您指点方向.... – marcel