修复了带有FragmentList和动态内容的标头

时间:2014-10-31 18:39:55

标签: android listview android-fragments header

我试图在我的应用中使用动态内容修复列表的标题。问题是我只看到标题而不是内容。如果我评论某些行,列表内容会出现,但标题不会出现。我尝试使用addHeader但它使标题不固定。

我的控制器

public class RadioListFragment extends ListFragment {
    /** The Radios. */
    private ArrayList<Radio> mRadios;

    /** The Header view. */
    private View mHeaderView;

    /** The Radio adapter. */
    private RadioAdapter mRadioAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRadios = RadioRepo.get(getActivity()).getRadios();
        mRadioAdapter = new RadioAdapter(mRadios);

        setListAdapter(mRadioAdapter);
    }

    @TargetApi(11)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
            Bundle savedInstanceState) {

        super.onCreateView(inflater, parent, savedInstanceState);

        View v = inflater.inflate(R.layout.list_header_radio, parent, false);
        //mHeaderView = v.findViewById(R.id.list_header_radioTextView);

        // Inflate the Header View
        /*
        mHeaderView = (View)getActivity()
                .getLayoutInflater()
                .inflate(R.layout.list_header_radio, null, false);
        */

        return v;
    }


    private class RadioAdapter extends ArrayAdapter<Radio> {

        public RadioAdapter(ArrayList<Radio> radios) {
            super(getActivity(), 0, radios);
        } 

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // If we weren't given a view, inflate one
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater()
                        .inflate(R.layout.list_item_radio, null);
            }

            // Configure the view for this Radio
            Radio r = getItem(position);
            TextView nameTextView = 
                    (TextView)convertView.findViewById(R.id.radio_list_item_nameTextView);
            nameTextView.setText(r.getName());
            return convertView;
        }
    }
}

list_header_radio.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/list_header_radioTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/dummy_text_view" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="0dp" />

</RelativeLayout>

list_item_radio.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/radio_list_item_logoImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/logo" />

    <TextView
        android:id="@+id/radio_list_item_nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/radio_list_item_logoImageView"
        android:text="@string/radios_name" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试按以下步骤更新ListView:

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/list_header_radioTextView" />

您可以将标题视图作为注释行:

mHeaderView = v.findViewById(R.id.list_header_radioTextView);