public class listfragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_1,container,false); return view; } }
<fragment android:id="@+id/fragment" android:name="com.example.myapplicationtest.listfragment" android:layout_width="395dp" android:layout_height="445dp" android:layout_weight="1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/linearLayout" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_marginTop="10dp" android:layout_marginBottom="6dp" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/list"> <ImageView android:id="@+id/image1" android:layout_width="10dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/ic_launcher_background" /> <ImageView android:id="@+id/image2" android:layout_width="10dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/ic_launcher_background" /> <ImageView android:id="@+id/image3" android:layout_width="10dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/ic_launcher_background" /> <ImageView android:id="@+id/image4" android:layout_width="10dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/ic_launcher_background" /> </LinearLayout>
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment1); ImageView imageView1=findViewById(R.id.image1); ImageView imageView2=findViewById(R.id.image2); ImageView imageView3=findViewById(R.id.image3); ImageView imageView4=findViewById(R.id.image4); imageView1.setOnClickListener(l); imageView2.setOnClickListener(l); imageView3.setOnClickListener(l); imageView4.setOnClickListener(l); } View.OnClickListener l=new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm=getSupportFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); Fragment f=null; switch (v.getId()) { case R.id.image1: f=new listfragment(); break; case R.id.image2: f=new fragmen2(); break; case R.id.image3: f=new fragment3(); break; case R.id.image4: f=new fragment3(); default:break; } ft.replace(R.id.fragment,f); ft.commit(); } };