-1
我想要一個帶有圖像然後兩個片段的活動,一個帶有列表視圖的片段和另一個帶有textview的片段。帶有列表視圖的片段沒有被帶有textview的片段替換
所以,當圖像被點擊時,我想要顯示帶有列表視圖的片段,並且當片段上的項目被點擊時,我想用帶有textview的片段替換帶有列表視圖的片段,點擊列表項。
我用下面的代碼做這件事,但它不工作,當一個列表項目的項目被點擊時,帶有listview的片段不會被帶有textview的片段替換,所以列表項目文本不會出現。
主要業務類:
public class MainActivity extends AppCompatActivity {
private ImageView img;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
img = (ImageView) findViewById(R.id.imageView);
}
private void addFragment() {
Fragment fragment = new Fragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.listviewInFragment, fragment, "frag");
transaction.commit();
}
public void AddFragmentList(View view) {
Fragment fragment = new Fragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.container, new FragmentA(), "frag");
transaction.commit();
}
}
主要活動的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
>
<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_absoluteY="71dp"
tools:layout_editor_absoluteX="0dp" />
<FrameLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
tools:layout_editor_absoluteY="390dp">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
片段與列表視圖:
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);
Toast.makeText(getActivity().getApplicationContext(), clickedValue, Toast.LENGTH_SHORT).show();
Bundle b = new Bundle();
b.putString("clickedValue", clickedValue);
FragmentA fragA = new FragmentA();
fragA.setArguments(b);
getFragmentManager().beginTransaction().replace(R.id.container, fragA);
}
});
}
}
片段與列表視圖的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>
片段與文本視圖:
public class fragmentb extends Fragment {
private TextView tv;
Intent intent;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tv = (TextView) getView().findViewById(R.id.textView);
Bundle b = this.getArguments();
if(b != null){
String clickedValue =b.getString("clickedValue");
tv.setText(clickedValue);
}
}
}
片段與文本視圖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="17dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
/>
</android.support.constraint.ConstraintLayout>
謝謝,但它不工作時,在列表視圖中的項目被點擊與文本視圖的fragmentb不會出現只有列表視圖。 – JonD
是否叫做fragmentb – UltimateDevil
不,它不會出現。 – JonD