2013-11-24 3 views
11

나는 내가하려는 프로젝트가있다. 나는 약간의 문제에 부딪쳤다. 그리고 나는 그것을 해결하는 방법을 모르고있다. 아래는 지금까지 응용 프로그램의 이미지입니다.onclick 동안 조각 사이를 전환하는 방법?

사용자가 "Hello! It 's Fragment2"라고 말한 부분이 목록 항목 중 하나를 onclick하면 응용 프로그램에 선언 된 새 xml로 변경됩니다.

AndroidListFragmentActivity : 나는 ATO되는 listItem를 클릭하면 그래서, 그 다음 오른쪽에있는 조각 같은 것을 변경한다 여기

enter image description here

지금까지 내 코드입니다! "안녕하세요는 ATO 조각이다"

package com.exercise.AndroidListFragment; 

import android.app.Activity; 
import android.os.Bundle; 

public class AndroidListFragmentActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

Fragment2 :

package com.exercise.AndroidListFragment; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class Fragment2 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     return inflater.inflate(R.layout.fragment2, container, false); 
    } 

} 

MyListFragment1 :

package com.exercise.AndroidListFragment; 

import android.app.ListFragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MyListFragment1 extends ListFragment { 

    String[] options ={ 
      "ATO", 
      "BETA", 
      "DELT", 
      "PHI DELT", 
      "SAE", 
      "SIG NU", 
      "FIJI", 
      "SIG CHI", 
      "PHI PSI" 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ListAdapter myListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, options); 
     setListAdapter(myListAdapter); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.listfragment1, container, false); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show(); 
    } 
} 

Fragment2.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/fragment2text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello! It's Fragment2" /> 
</LinearLayout> 

listfragment1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp"> 

    <ListView android:id="@id/android:list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:drawSelectorOnTop="false"/> 

    <TextView android:id="@id/android:empty" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="No data"/> 
</LinearLayout> 

main.xml에

코드를 얻을 수있는 최소한의 수정이 무엇이 있는지
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <fragment 
     android:name="com.exercise.AndroidListFragment.MyListFragment1" 
     android:id="@+id/fragment1" 
     android:layout_weight="1" 
     android:layout_width="0px" 
     android:layout_height="match_parent" /> 
    <fragment 
     android:name="com.exercise.AndroidListFragment.Fragment2" 
     android:id="@+id/fragment2" 
     android:layout_weight="2" 
     android:layout_width="0px" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

답변

13

하지 않음 일하지만, 가지고있다. 당신은 Navigation Drawer을 사용하여 조각 사이를 전환 했습니까? 공식적인 문서 일치 사항의 예제가 여러분이 성취하고자하는 것과 거의 비슷하게 보입니다.

키는 XML 에서처럼 <fragment>을 사용하는 대신 현재 표시된 단편에 대해 컨테이너 종류를 갖는 것입니다. 예를 들어 :

<FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

그리고, 조각을 전환하는 것은 이런 식입니다 :

Fragment fragment = new Fragment2(); 
// Insert the fragment by replacing any existing fragment 
FragmentManager fragmentManager = getFragmentManager(); 
fragmentManager.beginTransaction() 
       .replace(R.id.content_frame, fragment) 
       .commit(); 

추가 읽기 : Add a Fragment to an Activity at Runtime를 안드로이드 개발자 문서에.

+0

@Jonik http://stackoverflow.com/questions/23356848/transaction-between-fragment-and-activity 저를 도와 줄 수 있습니까? – Noufal

관련 문제