Android перенос Activity в Fragment
У меня есть Activity в которую я интегрировал ExpandableRelativeLayout, и мне нужно содержимое перенести в Fragment, у меня не получается это сделать, помогите пожалуйста, я не понимаю. Я заметил что в Xml файле Activity, в onClick можно указать путь к нужному Java файлу, а на Fragment не получается указать, думаю вся проблема в этом. Подскажите прав я или нет. И как это исправить. Я сижу уже 4 час но так и не получается сделать желаемое.
Xml Activity
public class Main5Activity extends AppCompatActivity
{
com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout1, expandableLayout2, expandableLayout3, expandableLayout4;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
}
public void expandableButton1(View view) {
expandableLayout1 = findViewById(R.id.expandableLayout1);
expandableLayout1.toggle(); // toggle expand and collapse
}
public void expandableButton2(View view) {
expandableLayout2 = findViewById(R.id.expandableLayout2);
expandableLayout2.toggle(); // toggle expand and collapse
}
public void expandableButton3(View view) {
expandableLayout3 = findViewById(R.id.expandableLayout3);
expandableLayout3.toggle(); // toggle expand and collapse
}
public void expandableButton4(View view) {
expandableLayout4 = findViewById(R.id.expandableLayout4);
expandableLayout4.toggle(); // toggle expand and collapse
}
}
Java Activity
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/expandableButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ba1f04"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButto"
android:paddingRight="10dp"
android:text="Expandable Layout Example"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton1"
android:background="#90ba1f04"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expandable Layout provides an easy way to create a view called header with an expandable view. Views are external layout to allow a maximum of customization." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
<Button
android:id="@+id/expandableButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/expandableLayout1"
android:background="#b77803"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButton2"
android:paddingRight="10dp"
android:text="Expandable Views Tutorial"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton2"
android:background="#90b77803"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android library for creating an expandable and collapsible to full screen view inside a viewgroup composition. Expandable Layout provides an easy way to create a view called header with an expandable view. Views are external layout to allow a maximum of customization." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
<Button
android:id="@+id/expandableButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/expandableLayout2"
android:background="#10a106"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButton3"
android:paddingRight="10dp"
android:text="Expand/Collapse Android Widgets"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton3"
android:background="#9010a106"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="An android library that brings the expandable and collapsible layouts or views or widgets with various animation effect and material design color. You can include optional contents and use everywhere according to your demand.
An android library that brings the expandable and collapsible layouts or views or widgets with various animation effect and material design color. You can include optional contents and use everywhere according to your demand" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
<Button
android:id="@+id/expandableButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/expandableLayout3"
android:background="#066da1"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButton4"
android:paddingRight="10dp"
android:text="Expand/Collapse Android Example"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton4"
android:background="#90066da1"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition.
Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
</RelativeLayout>
По сути Xml в Fragment такой же, только id поменял чтобы не путаться
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/expandableButton1_frag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ba1f04"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButton1_frag"
android:paddingRight="10dp"
android:text="Expandable Layout Example"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout1_frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton1_frag"
android:background="#90ba1f04"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="true"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expandable Layout provides an easy way to create a view called header with an expandable view. Views are external layout to allow a maximum of customization." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
<Button
android:id="@+id/expandableButton2_frag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/expandableLayout1_frag"
android:background="#b77803"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButton2_frag"
android:paddingRight="10dp"
android:text="Expandable Views Tutorial"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout2_frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton2_frag"
android:background="#90b77803"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android library for creating an expandable and collapsible to full screen view inside a viewgroup composition. Expandable Layout provides an easy way to create a view called header with an expandable view. Views are external layout to allow a maximum of customization." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
<Button
android:id="@+id/expandableButton3_frag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/expandableLayout2_frag"
android:background="#10a106"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButton3_frag"
android:paddingRight="10dp"
android:text="Expand/Collapse Android Widgets"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout3_frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton3_frag"
android:background="#9010a106"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="An android library that brings the expandable and collapsible layouts or views or widgets with various animation effect and material design color. You can include optional contents and use everywhere according to your demand.
An android library that brings the expandable and collapsible layouts or views or widgets with various animation effect and material design color. You can include optional contents and use everywhere according to your demand" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
<Button
android:id="@+id/expandableButton4_frag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/expandableLayout3_frag"
android:background="#066da1"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="expandableButton4_frag"
android:paddingRight="10dp"
android:text="Expand/Collapse Android Example"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableLayout4_frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/expandableButton4_frag"
android:background="#90066da1"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition.
Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
</RelativeLayout>
Java в Fragment
public class ItemFiveFragment extends Fragment implements View.OnClickListener
{
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout1;
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout2;
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout3;
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout4;
public static ItemFiveFragment newInstance()
{
return new ItemFiveFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void expandableButton1_frag(View view)
{
expandableLayout1 = requireView().findViewById(R.id.expandableLayout1_frag);
expandableLayout1.toggle(); // toggle expand and collapse
}
public void expandableButton2_frag(View view)
{
expandableLayout2 = requireView().findViewById(R.id.expandableLayout2_frag);
expandableLayout2.toggle(); // toggle expand and collapse
}
public void expandableButton3_frag(View view)
{
expandableLayout3 = requireView().findViewById(R.id.expandableLayout3_frag);
expandableLayout3.toggle(); // toggle expand and collapse
}
public void expandableButton4_frag(View view)
{
expandableLayout4 = requireView().findViewById(R.id.expandableLayout4_frag);
expandableLayout4.toggle(); // toggle expand and collapse
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_item_setting, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{
}
public void onClick(View v)
{
}
}
~~~~~~~~~~~~~~~~~~~~~~
Ответы (1 шт):
Если я правильно вас понял, вам нужно вместо активности сделать фрагмент с такой же разметкой как и у активности. Для этого не нужно создавать новый xml файл с разметкой, вполне подойдет файл разметки который используется в активности. Дальше вы создали фрагмент в котором нужно использовать вашу разметку:
public class ItemFiveFragment extends Fragment implements View.OnClickListener
{
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout1;
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout2;
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout3;
private com.github.aakira.expandablelayout.ExpandableRelativeLayout expandableLayout4;
public void expandableButton1_frag(View view)
{
expandableLayout1 = requireView().findViewById(R.id.expandableLayout1_frag);
expandableLayout1.toggle(); // toggle expand and collapse
}
public void expandableButton2_frag(View view)
{
expandableLayout2 = requireView().findViewById(R.id.expandableLayout2_frag);
expandableLayout2.toggle(); // toggle expand and collapse
}
public void expandableButton3_frag(View view)
{
expandableLayout3 = requireView().findViewById(R.id.expandableLayout3_frag);
expandableLayout3.toggle(); // toggle expand and collapse
}
public void expandableButton4_frag(View view)
{
expandableLayout4 = requireView().findViewById(R.id.expandableLayout4_frag);
expandableLayout4.toggle(); // toggle expand and collapse
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.activity_main5, container, false); // здесь используем разметку активности
}
public void onClick(View v)
{
}
}
ну и дальше сам фрагмент нужно как-то использовать в приложении. Если у вас один фрагмент то нужно сделать активность которая будет контейнером для данного фрагмента и позволит проводить замену фрагментов. Вот есть интересный урок по данному вопросу.