#java #android #android-layout #android-fragments
Вопрос:
На главном экране у меня есть 7 кнопок (дней) и фрагмент. Для каждого дня недели я хотел создать фрагмент с некоторыми текстами редактирования и представлением списка, но создание 7 фрагментов с одинаковым макетом и почти одинаковым содержимым класса кажется слишком повторяющимся. Я делал это только в понедельник и вторник.
Единственное различие между фрагментом понедельника и фрагментом вторника заключается в том, что во фрагменте вторника я переименовал переменную mondayTV в tuesdayTV, а в макете изменил идентификаторы кнопки отправки и представления списка. Также ключ для пакета отличается. Я не думаю, что стоит публиковать это, так как это так похоже на фрагмент понедельника.
Я хочу знать, возможно ли создать шаблон фрагмента и использовать его для создания фрагмента для каждой кнопки действия с учетом этого кода, который я написал. Есть еще над чем поработать, когда дело доходит до функциональности, но я не могу выбросить эту идею из головы.
MainActivity.java
package com.example.dietmanagement;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public Button monday, tuesday, wednesday, thursday, friday, saturday, sunday;
final MondayFragment mondayFragment = new MondayFragment();
final TuesdayFragment tuesdayFragment = new TuesdayFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Intent i = getIntent();
String current_user = i.getStringExtra("current_user");
TextView current_user_txtview = findViewById(R.id.current_user);
current_user_txtview.setText("Welcome, " current_user);*/
monday = (Button)findViewById(R.id.monday_btn);
tuesday = (Button)findViewById(R.id.tuesday_btn);
wednesday = (Button)findViewById(R.id.wednesday_btn);
thursday = (Button)findViewById(R.id.thursday_btn);
friday = (Button)findViewById(R.id.friday_btn);
saturday = (Button)findViewById(R.id.saturday_btn);
sunday = (Button)findViewById(R.id.sunday_btn);
monday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFragment(mondayFragment);
getDay(mondayFragment, "monday", (String) monday.getContentDescription());
}
});
tuesday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFragment(tuesdayFragment);
getDay(tuesdayFragment, "tuesday", (String) tuesday.getContentDescription());
}
});
}
private void openFragment(final Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.daysfragment, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void getDay(final Fragment fragment, String key, String value)
{
Bundle bnd = new Bundle();
bnd.putString(key, value);
fragment.setArguments(bnd);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@color/background_green"
tools:context=".MainActivity">
<Button
android:id="@ id/tuesday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/button_states"
android:contentDescription="@string/tuesday_context"
android:text="@string/tuesday"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.194"
app:layout_constraintStart_toEndOf="@ id/monday_btn"
app:layout_constraintTop_toBottomOf="@ id/main_title"
app:layout_constraintVertical_bias="0.017" />
<TextView
android:id="@ id/main_title"
android:layout_width="147dp"
android:layout_height="93dp"
android:fontFamily="sans-serif-medium"
android:text="@string/welcome_label"
android:textColor="#FFFFFF"
android:textSize="70sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.414"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<Button
android:id="@ id/monday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/button_states"
android:contentDescription="@string/monday_context"
android:text="@string/monday"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/main_title"
app:layout_constraintVertical_bias="0.017" />
<Button
android:id="@ id/thursday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/button_states"
android:text="@string/thursday"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.018"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@ id/sunday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/button_states"
android:text="@string/sunday"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.786"
app:layout_constraintStart_toEndOf="@ id/saturday_btn"
app:layout_constraintTop_toBottomOf="@ id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@ id/saturday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/button_states"
android:text="@string/saturday"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.225"
app:layout_constraintStart_toEndOf="@ id/friday_btn"
app:layout_constraintTop_toBottomOf="@ id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@ id/friday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/button_states"
android:text="@string/friday"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toEndOf="@ id/thursday_btn"
app:layout_constraintTop_toBottomOf="@ id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@ id/wednesday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/button_states"
android:text="@string/wednesday"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toEndOf="@ id/tuesday_btn"
app:layout_constraintTop_toBottomOf="@ id/main_title"
app:layout_constraintVertical_bias="0.017" />
<ImageView
android:id="@ id/logo"
android:layout_width="52dp"
android:layout_height="58dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:contentDescription="@string/app_name"
app:layout_constraintStart_toEndOf="@ id/main_title"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_logo" />
<TextView
android:id="@ id/current_user"
android:layout_width="362dp"
android:layout_height="27dp"
android:text="@string/current_user"
android:textAlignment="viewEnd"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@ id/main_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.938"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.318" />
<FrameLayout
android:id="@ id/daysfragment"
android:layout_width="match_parent"
android:layout_height="460dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MondayFragment.java
package com.example.dietmanagement;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MondayFragment extends Fragment {
public TextView mondayTV;
public ArrayList<String> hour_food;
public ArrayAdapter<String> listViewAdapter;
public ListView listView;
public EditText input_meal;
public EditText input_time;
public Button submit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_monday, container, false);
mondayTV = (TextView) v.findViewById(R.id.day);
Bundle bndMon = getArguments();
String day = bndMon.getString("monday");
mondayTV.setText(day);
hour_food = new ArrayList<String>();
listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, hour_food);
listView = (ListView)v.findViewById(R.id.monday_list_item);
listView.setAdapter(listViewAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
hour_food.remove(position);
Toast.makeText(getActivity(), "Meal Removed", Toast.LENGTH_SHORT).show();
listViewAdapter.notifyDataSetChanged();
return true;
}
});
input_meal = v.findViewById(R.id.input_meal);
input_time = v.findViewById(R.id.input_time);
submit = (Button) v.findViewById(R.id.submit_food_btn);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(TextUtils.isEmpty(input_time.getText())) {
Toast.makeText(getActivity(), "Empty time input", Toast.LENGTH_SHORT).show();
} else if(TextUtils.isEmpty(input_meal.getText())){
Toast.makeText(getActivity(), "Empty meal input", Toast.LENGTH_SHORT).show();
}
else
{
hour_food.add(String.format("%s - %s", input_meal.getText().toString(), input_time.getText().toString()));
listViewAdapter.notifyDataSetChanged();
input_meal.setText("");
input_time.setText("");
}
}
});
return v;
}
}
fragment_monday.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@color/background_green"
tools:context=".MondayFragment">
<TextView
android:id="@ id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/day" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@ id/input_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="@string/time"
android:hint="@string/time"
android:inputType="time" />
<EditText
android:id="@ id/input_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="@string/meal"
android:hint="@string/meal"
android:inputType="textAutoCorrect|textCapSentences" />
</LinearLayout>
<Button
android:id="@ id/submit_food_btn_monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:background="@color/white"
android:textColor="@color/background_green"
android:layout_gravity="end"
android:layout_marginTop="10dp"/>
<ListView
android:id="@ id/monday_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
strings.xml
Комментарии:
1. Может быть, взглянем на абстрактные классы
2. Почему ваши фрагменты вообще имеют разную компоновку? Похоже, что каждый может быть примером а
DayOfTheWeekFragment
.
Ответ №1:
Создание DayFragment
и верстка только на один день. Передавайте дополнительную информацию Fragment
и обрабатывайте ситуации в разные дни с помощью этой дополнительной информации (в вашем случае это выглядит так, как будто отличается только String
от пакета).
fragment_day.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:background="@color/background_green">
<TextView
android:id="@ id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/day" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@ id/input_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="@string/time"
android:hint="@string/time"
android:inputType="time" />
<EditText
android:id="@ id/input_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="@string/meal"
android:hint="@string/meal"
android:inputType="textAutoCorrect|textCapSentences" />
</LinearLayout>
<Button
android:id="@ id/submit_food_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:background="@color/white"
android:textColor="@color/background_green"
android:layout_gravity="end"
android:layout_marginTop="10dp"/>
<ListView
android:id="@ id/list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
DayFragment.java
public class DayFragment extends Fragment {
private TextView dayTV;
private ArrayList<String> hour_food;
private ArrayAdapter<String> listViewAdapter;
private ListView listView;
private EditText input_meal;
private EditText input_time;
private Button submit;
private String text;
public DayFragment(String text) {
this.text = text;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_day, container, false);
dayTV = v.findViewById(R.id.day);
dayTV.setText(text);
hour_food = new ArrayList<>();
listViewAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, hour_food);
listView = v.findViewById(R.id.list_item);
listView.setAdapter(listViewAdapter);
listView.setOnItemLongClickListener((parent, view, position, id) -> {
hour_food.remove(position);
Toast.makeText(getActivity(), "Meal Removed", Toast.LENGTH_SHORT).show();
listViewAdapter.notifyDataSetChanged();
return true;
});
input_meal = v.findViewById(R.id.input_meal);
input_time = v.findViewById(R.id.input_time);
submit = v.findViewById(R.id.submit_food_btn);
submit.setOnClickListener(v1 -> {
if (TextUtils.isEmpty(input_time.getText())) {
Toast.makeText(getActivity(), "Empty time input", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(input_meal.getText())) {
Toast.makeText(getActivity(), "Empty meal input", Toast.LENGTH_SHORT).show();
} else {
hour_food.add(String.format("%s - %s", input_meal.getText().toString(), input_time.getText().toString()));
listViewAdapter.notifyDataSetChanged();
input_meal.setText("");
input_time.setText("");
}
});
return v;
}
}
Используйте конструктор для передачи информации.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button monday = findViewById(R.id.monday_btn),
tuesday = findViewById(R.id.tuesday_btn),
wednesday = findViewById(R.id.wednesday_btn),
thursday = findViewById(R.id.thursday_btn),
friday = findViewById(R.id.friday_btn),
saturday = findViewById(R.id.saturday_btn),
sunday = findViewById(R.id.sunday_btn);
openFragment(monday);
openFragment(tuesday);
openFragment(wednesday);
openFragment(thursday);
openFragment(friday);
openFragment(saturday);
openFragment(sunday);
}
private void openFragment(Button btn) {
btn.setOnClickListener(v -> {
String contentDescription = btn.getContentDescription().toString();
getSupportFragmentManager().beginTransaction()
.replace(R.id.daysfragment, new DayFragment(contentDescription))
.addToBackStack(null)
.commit();
});
}
}
В MainActivity
openFragment()
методе принимает Button
параметр и с помощью этого Button
и устанавливает значение OnClickListener на это Button
. Когда вы нажимаете на любой Button
из них, он получает описание содержимого, Button
передает его Fragment
и открывает его Fragment
с этим описанием содержимого.