如何为ListView添加标题

时间:2017-09-15 07:08:18

标签: java android xml listview android-fragments

我已使用ViewPager创建标签式活动包含两个标签,标签一包含EditText,标签二包含ListView,当您在EditText中写一些内容并点击保存时文字传递给ListView,我想为ListView添加标题。我该怎么做?

我的fragment_one.java文件:

public class FragmentOne extends Fragment {

SendMessage SM;
ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.fragment_one, container, false);
    return rootView;


}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
    viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
    final EditText inData = (EditText) view.findViewById(R.id.inMessage);

    btnPassData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SM.sendData(inData.getText().toString().trim());
            viewPager.setCurrentItem(1);
        }
    });

}

interface SendMessage {
    void sendData(String message);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        SM = (SendMessage) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException("Error in retrieving data. Please try again");
    }
}

}

我的fragment_two.xml文件:`

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="store.exercise.com.store.MainActivity$PlaceholderFragment">

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /></RelativeLayout>

0 个答案:

没有答案