2017-10-15 89 views
0

我在主要活動中有一個圖像,當單擊此圖像時,我想顯示具有列表視圖的fragmentA。用fragmentB問題替換fragmentA

當此的ListView的項目被點擊我想有一個TextView的fragmentB更換fragmentA,我想在這個展示的TextView相關的點擊列表項的文本。

所以在主要活動我有這樣的:

public class MainActivity extends AppCompatActivity implements Listener{ 
    private ImageView img; 
    private String text; 
    FragmentManager fragmentManager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     fragmentManager = getFragmentManager(); 
     img = (ImageView) findViewById(R.id.imageView); 
    } 
    public void AddFragmentA(View view) { 
     FragmentA fragmentA = new FragmentA(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(R.id.containerFragmentA, new FragmentA(), "fragA"); 
     transaction.commit(); 
    } 
    public void AddFragmentB() { 
     FragmentB fragment = new FragmentB(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
     transaction.commit(); 
    } 
    @Override 
    public void addText(String text) { 
     this.text = text; 
     Toast.makeText(this, "Text received in Activity:" +text, Toast.LENGTH_SHORT).show(); 
     sendDataToFragmentB(); 
    } 

public void sendDataToFragmentB(){ 

      FragmentB fragmentB = (FragmentB) fragmentManager.findFragmentByTag("fragB"); 
      fragmentB.addText(text); 
     } 
} 

注:在addText()的麪包出現在這一點正確的文本,所以列表視圖的文本被接收成功的活動。

問題:現在如何用fragmentB替換fragmentA,並在活動中顯示接收文本的textview,而不是顯示fragmentA的列表視圖?

以下是所有完整的示例。

FragmentA類:

public class FragmentA extends Fragment{ 
    private ListView listItems; 
    private String[] items = { 
      "item1", 
      "item2", 
      "item3", 
      "item4" 
    }; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment, container, false); 
     return view; 
    } 
    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     listItems = (ListView) getView().findViewById(R.id.listviewInFragment); 

     ArrayAdapter<String> adapter = 
       new ArrayAdapter<String>(getActivity().getApplicationContext(), 
         android.R.layout.simple_list_item_1, android.R.id.text1, items); 

     listItems.setAdapter(adapter); 
     listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       int positionCode = i; 

       String clickedValue = (String) adapterView.getItemAtPosition(i); 
       Listener listener = (Listener) getActivity(); 
       listener.addText(clickedValue); 
      } 
     }); 
    } 

} 

FramentB:

public class FragmentB extends Fragment { 
    private TextView tv; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_b, container, false); 
     tv = (TextView) getView().findViewById(R.id.textView); 
     return view; 
    } 
    public void addText(String text) { 
     String result = text; 
     tv.setText(result); 
    } 
} 

主要活動的xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/image" 
     android:onClick="AddFragmentA" 
     tools:layout_editor_absoluteX="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" /> 
    <FrameLayout 
     android:id="@+id/containerFragmentA" 
     android:layout_width="0dp" 
     android:layout_height="300dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     tools:layout_editor_absoluteY="1dp"> 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/containerFragmentB" 
     android:layout_width="0dp" 
     android:layout_height="300dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     tools:layout_editor_absoluteY="1dp"> 
    </FrameLayout> 
</android.support.constraint.ConstraintLayout> 

片段的XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#0ff"> 

    <ListView 
     android:layout_width="368dp" 
     android:layout_height="495dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="16dp" 
     android:id="@+id/listviewInFragment"/> 

</android.support.constraint.ConstraintLayout> 

片段B的xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ff0"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="300dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     /> 
</android.support.constraint.ConstraintLayout> 
+0

檢查我的答案[這裏](https://stackoverflow.com/a/46707510/8244632),您可以使用帶有兩個片段的ViewPager,通過限制滾動觸摸並在ListView的項目點擊中使用提供的解決方案。橫向滾動會給你的用戶界面帶來不錯的效果,而不是取代 –

回答

0

這是你將如何實現所需的任務。 相應地在下面給你的班級打電話。 MainActivity.java

public class MainActivity extends AppCompatActivity implements FragmentA.ClickListener{ 
    private Button button; 
    public static String EXTRA_STRING = "extraString"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     button = (Button)findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       AddFragment(FragmentA.getInstance()); 
      } 
     }); 
    } 
    public void AddFragment(Fragment fragment) { 
     getSupportFragmentManager().beginTransaction().replace(R.id.containerFragmentA, fragment).commit(); 
    } 

    @Override 
    public void onClick(String data) { 
     AddFragment(FragmentB.getInstance(data)); 
    } 
} 

activity_main.xml中

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

    <FrameLayout 
     android:id="@+id/containerFragmentA" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_editor_absoluteY="1dp"/> 

    <Button 
     android:id="@+id/button" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_margin="8dp" 
     android:layout_alignParentBottom="true" 
     android:text="Add Fragment B" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

FragmentA.java

public class FragmentA extends Fragment { 
private ListView listItems; 
private String[] items = {"item1", "item2", "item3", "item4"}; 
private ClickListener clickListener; 

public static FragmentA getInstance() { 
    return new FragmentA(); 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    clickListener = (ClickListener)context; 
} 

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

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    listItems = (ListView) getView().findViewById(R.id.listviewInFragment); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1, items); 
    listItems.setAdapter(adapter); 
    listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      String clickedValue = items[i]; 
      clickListener.onClick(clickedValue); 
     } 
    }); 
} 

public interface ClickListener{ 
    void onClick(String data); 
} 

}

fragment_a.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ListView 
     android:layout_width="368dp" 
     android:layout_height="495dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="16dp" 
     android:id="@+id/listviewInFragment"/> 

</android.support.constraint.ConstraintLayout> 

FragmentB.java

public class FragmentB extends Fragment { 
    private TextView tv; 
    private String data; 

    public static FragmentB getInstance(String data){ 
     Bundle bundle = new Bundle(); 
     bundle.putString(MainActivity.EXTRA_STRING,data); 
     FragmentB fragmentB = new FragmentB(); 
     fragmentB.setArguments(bundle); 
     return fragmentB; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_b, container, false); 
     tv = view.findViewById(R.id.textView); 
     data = getArguments().getString(MainActivity.EXTRA_STRING); 
     addText(data); 
     return view; 
    } 
    public void addText(String text) { 
     String result = text; 
     tv.setText(result); 
    } 
} 

fragment_b.xml

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

    <TextView 
     android:gravity="center" 
     android:textSize="24dp" 
android:textAppearance="@style/TextAppearance.AppCompat.Widget.PopupMenu.Header" 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
1

要放置片段A在下面函數而不是片段乙

public void AddFragmentB() { 
     FragmentB fragment = new FragmentB(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
     transaction.commit(); 
    } 

它應該是

public void AddFragmentB() { 
     FragmentB fragment = new FragmentB(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.replace(R.id.containerFragmentB, new FragmentB(), "fragB"); 
     transaction.commit(); 
    } 

如果您想用另一個替換片段只使用相同的容器 請勿創建單獨的容器(R.id.containerFragmentB,R.id.containerFragmentA)容器。

+0

謝謝。但是現在當我點擊一個列表項時,它會出現「應用程序停止」。這似乎是因爲我會在問題中更新的sendDataToFragmentB方法。 – JonD

+0

在最後發表回覆 –

+0

添加另一個答案檢查出來。對我來說工作正常 –

1

你需要改變你的

public void AddFragmentA(View view) { 
    FragmentA fragmentA = new FragmentA(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentA, new FragmentA(), "fragA"); 
    transaction.commit(); 
} 
public void AddFragmentB() { 
    FragmentB fragment = new FragmentB(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
    transaction.commit(); 
} 

public void AddFragmentA(View view) { 
    FragmentA fragmentA = new FragmentA(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.container, fragmentA , "fragA"); 
    transaction.commit(); 
} 
public void AddFragmentB() { 
    FragmentB fragment = new FragmentB(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.container, fragment, "fragB"); 
    transaction.commit(); 
} 

和activity_main。XML應該

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/image" 
     android:onClick="AddFragmentList" 
     tools:layout_editor_absoluteX="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" /> 
    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="0dp" 
     android:layout_height="300dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     tools:layout_editor_absoluteY="1dp"> 
    </FrameLayout> 


</android.support.constraint.ConstraintLayout> 

編輯

listItems.setAdapter(adapter); 
    listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      int positionCode = i; 

      String clickedValue = (String) adapterView.getItemAtPosition(i); 
      Listener listener = (Listener) getActivity(); 
      listener.addText(clickedValue); 
     } 
    }); 

監聽

public interface IScanner { 

void addText(String Text); 

} 

MainActivity

public class MainActivity extends AppCompatActivity implements Listener{ 
private ImageView img; 
private String text; 
FragmentManager fragmentManager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    fragmentManager = getFragmentManager(); 
    img = (ImageView) findViewById(R.id.imageView); 
} 
public void AddFragmentA(View view) { 
    FragmentA fragmentA = new FragmentA(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentA, new FragmentA(), "fragA"); 
    transaction.commit(); 
} 

@Override 
public void addText(String text) { 
    this.text = text; 
    Toast.makeText(this, "Text received in Activity:" +text, Toast.LENGTH_SHORT).show(); 
    sendDataToFragmentB(text); 
} 

public void sendDataToFragmentB(String clickedValue){ 
    Bundle b = new Bundle(); 
    b.putString("clickedValue", clickedValue); 

    FragmentB fragment = new FragmentB(); 
    fragment.setArguments(b); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
    transaction.commit(); 
    } 
} 

FragmentB

public class FragmentB extends Fragment { 
private TextView tv; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_b, container, false); 
    tv = (TextView) getView().findViewById(R.id.textView); 
    return view; 
} 
Bundle bundle = this.getArguments(); 
if (bundle != null) { 
     String text = bundle.getString("clickedValue", ""); 
} 
} 

這將解決你的問題

+0

謝謝,但它也沒有工作,當我點擊列表中的項目時出現「應用程序停止」。我認爲它是因爲AddFragmentB或sendDataToFragmentB方法。你知道AddFragmentB()應該放在哪裏嗎? – JonD

+0

@JonD你的數據沒有通過,因爲你正在傳遞一個新的實例,同時提交你的片段,你應該通過該對象 – UltimateDevil

+0

我已經改變新的FragmentB()只是片段,但問題continue.I注意到我沒有使用AddFragmentB函數我將它添加到onCreate方法中,但是我使用onCreate主要活動方法上的此方法AddFragmentB運行應用程序doesent。 – JonD