2016-01-06 18 views
1

這是活動從片段擴展ListFragment的ListView正在重複自己。任何想法有什麼不對?

public class Homepage extends FragmentActivity implements ChatFragment.OnFragmentInteractionListener{ 

SearchFragment searchFragment; 
ChatFragment chatFragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_homepage); 

    searchFragment = new SearchFragment(); 
    chatFragment = new ChatFragment(); 


    if(savedInstanceState == null){ 
     getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit(); 
    } 
} 

@Override 
protected void onResume(){ 
    Bundle bundle = getIntent().getExtras(); 
    if(bundle != null){ 
     //text.setText("user_id: " + bundle.get("user_id") + " ,username: " + bundle.get("username") + " ,password: " + bundle.get("password")); 
    } 
      super.onResume(); 
} 


@Override 
public void onChatFragmentInteraction(Uri uri){ 

} 

public void openSearchFragment(View view){ 
    if(!searchFragment.isAdded()) 
     getFragmentManager().beginTransaction().replace(R.id.homepageFragment,searchFragment).commit(); 
} 

public void openChatFragment(View view){ 
    if(!chatFragment.isAdded()) 
     getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit(); 
    } 

    public void openProfile(View view){ 

    } 
} 

這是活動

我喜歡讓我的活動畫面響應的佈局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="the_activity_location" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="100" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:layout_weight="90" 
     android:orientation="vertical"> 

     <fragment 
      android:id="@+id/homepageFragment" 
      android:name="com.example.summer.toothbrush.SearchFragment" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      tools:layout="@layout/fragment_search"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:backgroundTint="@color/whiteClr" 
     android:background="@color/whiteClr" 
     android:layout_weight="10" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/search_img" 
      android:background="@null" 
      android:layout_weight="25" 
      android:layout_marginLeft="0dp" 
      android:layout_gravity="center" 
      android:onClick="openSearchFragment"/> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/chat_img" 
      android:background="@null" 
      android:layout_weight="50" 
      android:layout_gravity="center" 
      android:onClick="openChatFragment"/> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/profile_img" 
      android:background="@null" 
      android:layout_weight="25" 
      android:layout_gravity="center" 
      android:onClick="openProfile"/> 

    </LinearLayout> 

</LinearLayout> 

這是片段

public class SearchFragment extends ListFragment implements AdapterView.OnItemClickListener{ 

ArrayList<String> list; 
ListViewAdapter myAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){ 
    View view = inflater.inflate(R.layout.fragment_search,container,false); 

    list = new ArrayList<String>(); 

    list.add("I"); 
    list.add("am"); 
    list.add("Iron"); 

    myAdapter = new ListViewAdapter(list,getActivity().getBaseContext()); 

    setListAdapter(myAdapter); 

    return view; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    getListView().setOnItemClickListener(this); 

} 

public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
    Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show(); 
} 
} 

碎片的佈局是由 的ListView與「@android線性佈局內:ID /列表「作爲ID和TextView。

不要擔心適配器。它工作正常..我在一項活動中測試了它。完美的工作。

This is what it looks like now

+0

顯示''fragment_search XML代碼也 –

回答

2

這是正確的。你有兩個SearchFragment。一個在佈局中聲明爲靜態,另一個通過FragmentTransaction添加。在佈局中聲明爲靜態的Fragment不能用程序化事務替換。只需更換

<fragment 
     android:id="@+id/homepageFragment" 
     android:name="com.example.summer.toothbrush.SearchFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     tools:layout="@layout/fragment_search"/> 

<FrameLayout 
     android:id="@+id/homepageFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

,它是去工作,你希望

+0

謝謝黑帶。你救了我。 –

+0

不客氣 – Blackbelt