【问题标题】:Is it possible to have one fragment class common to different fragments, all having the same structure?是否有可能有一个片段类对不同的片段通用,所有片段都具有相同的结构?
【发布时间】:2015-07-15 18:09:16
【问题描述】:

我是 Android 开发的新手,我想知道是否可以让一个片段类对不同的片段具有相同的结构(ListViewTextView)。

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

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

并且必须为每个主题重复相同的事情,并为其片段使用相同的布局。

所以我的问题是:不是为每个主题创建不同的片段类,有没有一种方法可以创建一个只对所有主题片段通用的片段类,并在运行时对其进行修改? (更改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>

我要使用的片段布局如下所示

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

片段类如下所示

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);
    }
}

【问题讨论】:

    标签: java android listview android-fragments


    【解决方案1】:

    只需为控件元素(在您的情况下为列表项)实现一个单击侦听器。在该项目的 onclick 中,只需更改相应的文本视图内容

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2020-02-13
    相关资源
    最近更新 更多