0

나는 Activity와 3 개의 파편이 있습니다. 주요 활동의 하단에는 활성 조각을 전환하기위한 3 개의 버튼이 있습니다. 각 조각에는 ListView가 포함되어 있습니다. 개발하는 좋은 방법이라고 생각하십니까? 첫 번째 단편을 서버에서 다운로드하여 구문 분석하고 ListView에 요소를 추가하는 방법을 개발했지만 버튼을 눌러 Fragment2로 전환하면 첫 번째 단락이 유지됩니다.ListView가있는 조각은 유지됩니다.

이것은 주요 활동입니다. listView를 사용하는 Fragment는 일부 TextView와 하나의 ImageView에서 맞춤 어댑터를 사용하고 있습니다. listView를 제거하면 조각이 문제없이 바뀝니다.

import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.Window; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 

public class Home extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_home); 
    } 

    public void selectFrag(View view) { 
     Fragment fr; 
     int stringTitleID = R.string.title_fragment1; 

     if(view == findViewById(R.id.imgBtn3)) 
     { 
      fr = new FragmentThree(); 
      stringTitleID = R.string.title_fragment3; 
     } 
     else if(view == findViewById(R.id.imgBtn2)) 
     { 
      fr = new FragmentTwo(); 
      stringTitleID = R.string.title_fragment2; 
     } 
     else 
     { 
      fr = new FragmentOne(); 
      stringTitleID = R.string.title_fragment1; 
     } 

     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_place, fr); 
     fragmentTransaction.commit(); 

     // I change the title in the top_bar 
     TextView textViewActivityTitle = (TextView) findViewById(R.id.textViewActivityTitle); 
     textViewActivityTitle.setText(getResources().getString(stringTitleID).toString()); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.home, menu); 
     return true; 
    } 

    /* 
    * This functions clears the app cache. The json responses are stored in che cacheDir or cacheExternalDir. 
    * I clean them when the app is started. I only use the cache between the Fragments 
    */ 

} 

조각 솔루션이 좋은 것입니까? 왜 listview가있는 조각이 남았습니까? ListFragment를 사용해야합니까?

고마워요.

+0

내 문제는 XML에있었습니다. android : id = "@ + id/fragment_place"대신 를 사용했습니다. –

답변

0

나는 탭 탐색 더 나은 생각하지만 당신이 시도 할 수 있습니다 : 다음 식별하는 매개 변수를 사용하여, 먼저 당신의 버튼에 기본 활동 추가를 클릭 리스너에

1) :

button1.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View view){ 
      changeFragment(1) 
     } 
}; 

public void changeFragment(int whichfragment){ 
    switch(whichfragment){ 
     case 1: 
     fr=new FragmentOne(); 
     replaceFragment(); 
     break; 
     case 2: 
     fr=new FragmentTwo(); 
     replaceFragment(); 
     break; 
    } 

} 

2) 이제 조각을 바꿀 수 있습니다

public void replaceFragment(){ 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction transaction=fragmentManager.beginTransaction(); 
    transaction.replace(R.id.fragment_place, fr); 
transaction.commit(); 
} 
+0

탭을 전환하는 데 사용자 정의 버튼을 사용할 수 있습니까? 왜 더 낫다고 생각하니? 최대한 빨리 코드를 테스트 해 보겠습니다. 감사합니다. –

+0

예, 직접 단추를 만들 수 있습니다. 도움이 될 수도 있습니다. http://stackoverflow.com/questions/18074303/how-to-create-custom-shape-button-with-selector-in-android –