【发布时间】:2015-07-15 18:09:16
【问题描述】:
我是 Android 开发的新手,我想知道是否可以让一个片段类对不同的片段具有相同的结构(ListView 和 TextView)。
我将说明我的意思:该程序有一个显示文本视图和列表视图的片段。此列表视图显示数学、物理、计算等科目的名称。单击该项目时,我想使用与前一个片段相同的布局结构将(此)片段替换为另一个片段,唯一的区别是显示的文本视图,单击的项目的名称以及显示该模块的列表学科。例如点击数学将显示:
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