java - 是否有可能有一个 fragment 类对于不同的 fragment 是通用的,所有 fragment 都具有相同的结构?

标签 java android listview android-fragments

我是 Android 开发的新手,我想知道是否有可能有一个 fragment 类对于所有具有相同结构的不同 fragment 都是通用的(一个 ListView 和一个 TextView)。

我将说明我的意思:该程序有一个显示 TextView 和 ListView 的 fragment 。此 ListView 显示科目名称,例如数学、物理、计算等。单击该项目时,我想用与前一个 fragment 使用相同布局结构的另一个 fragment 替换(这个) fragment ,唯一的区别是显示的 TextView 、单击的项目名称和显示该模块的列表主题。例如,点击数学将显示:

TextView: Mathematics, ListView: Core 1, Core 2, Core 3, Core 4 and so on.

并且必须对使用相同 fragment 布局的每个主题重复同样的事情。

所以我的问题是:不是为每个主题创建不同的 fragment 类,有没有一种方法可以只创建一个对所有主题 fragment 通用的 fragment 类,并在运行时修改它? (更改 textView 名称并更改 ArrayAdapter 中的 String 数组用户)?

谢谢!

我的 activity_main 布局是这样的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <LinearLayout
        android:id="@+id/fragment_holder"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

    <EditText
        android:id="@+id/display_content_editText"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="start"
        android:editable="false"/>

</LinearLayout>

我想使用的 fragment 布局是这样的

<?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">

    <TextView
        android:id="@+id/subject_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textAppearance="?android:textAppearanceLarge"
        android:layout_marginBottom="20dp"/>

    <ListView
        android:id="@+id/subject_list_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

fragment 类看起来像这样

package com.example.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SubjectFragment extends Fragment {

    OnItemSelected onItemSelected;

    ListView listView;
    String[] subjectName;
    ArrayAdapter<String> arrayAdapter;
    TextView textView;

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

        textView = (TextView) view.findViewById(R.id.subject_textView);
        textView.setText(R.string.subject_textView);

        //Setting up the list view and its adapter
        listView = (ListView) view.findViewById(R.id.subject_list_id);
        subjectName = getResources().getStringArray(R.array.subject_list_array);
        arrayAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.custom_list_view, R.id.custom_view_id, subjectName);

        listView.setAdapter(arrayAdapter);

        //Set the click listener for the list view created
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int  position, long id) {
                String subjectName =  parent.getItemAtPosition(position).toString();
                onItemSelected.itemSelected(subjectName);
            }
        });

        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            onItemSelected = (OnItemSelected) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface OnItemSelected{
        void itemSelected(String subjectName);
    }
}

最佳答案

简单地为控制元素实现一个点击监听器 - 在您的情况下是列表项。在该项目的 onclick 中,只需更改相应的 TextView 内容

关于java - 是否有可能有一个 fragment 类对于不同的 fragment 是通用的,所有 fragment 都具有相同的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437944/

相关文章:

java - 访问 ListView 项目字符串的最简单方法是什么?

listview - 预加载 - ListView 中的 Admob 原生广告

android - 如何自定义 ListView 行android

java - ArrayList<R.String> 类型中的方法 add (int, R.String) 不适用于 arguments(int, String)

java - 在什么情况下 ApplicationEventPublisher.publishEvent 不会触发?

java - 限制嵌入式 Tomcat 中的密码

android - 在后台服务中使用 Google Play Services LocationClient

android - 如何在Android的TextView中使文本居中垂直在一行中

android - 自定义 ttf 字体在 Android 4.4 KitKat 上的 TextView 中显示不正确

java - 在 Struts Java 和 Javascript 之间共享常量