2011-11-29 1 views
1

첫 번째 사용자 정의 GUI 요소를 작성하려고했지만 나중에 코드를 사용하여 모양 또는 어댑터 (갤러리 사용)를 변경하려고 할 때 문제가 발생합니다.사용자 정의 갤러리 레이아웃 변경 등록 정보가 비정상 화되었습니다.

내 문제 : 나는 사용자 정의 갤러리 속성을 변경할 수 없습니다

내 실제 코드 :

우선은

<?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> 

나중에 나는를 만들 위젯 customGallery.xml에게 인 XML을 생성 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(); 
} 

}

내가 (ArrayAdapter와의 _adapter) 아무것도 변화 setNewAdapter를 호출하면 .... 나는 또한 갤러리의 크기를 바꾸려고했지만 또한 실패했다 (nothig 일어난다). 내 접근 방식이 거짓인가?
인사말 marcel

답변

0

가장 먼저 발견 할 수있는 것은 사용자 정의보기를 두 번 생성한다는 것입니다.

첫 번째 생성은 레이아웃을 설정할 때 ViewStub으로 발생합니다. 두 번째 것은 R.layout.test을 부 풀릴 때 contentView에 추가되지 않습니다.

어댑터를보기 계층에 추가되지 않은 두 번째 사용자 정의보기로 설정 중입니다.

+0

안녕하세요, 저는 이것을 바꿉니다. 하지만 호출 setnewAdapter() 아무 것도 변경 실제 거기에 .. 나는 notyfiyDataSetChange 문제를 해결할 것이라고 생각했다. 어떤 생각? – marcel

+0

@marcel : Plz, 업데이트로 질문에있는 코드를 수정하십시오. – Macarse

+0

안녕하세요, 저는 울부 짖었습니다 .. 지금 실수를 찾았습니다. View st3InflaView = st3.inflate(); GallerySlider gSlider = (GallerySlider) st3InflaView.findViewById (R.id.choose); 방향을 알려 주셔서 감사합니다 .... – marcel