Android将片段添加到listview中的标题

时间:2013-07-16 07:01:31

标签: android android-listview android-fragments

我在listview中添加了一个可绘制的图像作为标题。我已经在活动的情况下实现了如下。

listView1 = (ListView)findViewById(R.id.listView1);

    View header = (View)getLayoutInflater().inflate(R.layout.header, null);
    listView1.addHeaderView(header);

但是,现在我试图在像这样的

这样的viewpager片段中实现相同的功能
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ShowFrag1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.frag1, container,false);
        ListView lv1=(ListView) v.findViewById(R.id.listView1);

        Level weather_data[] = new Level[]
                {
                    new Level(R.drawable.s1, "L1", R.drawable.p),
                    new Level(R.drawable.s2, "L2",R.drawable.p),
                    new Level(R.drawable.s3, "L3",R.drawable.p),
                    new Level(R.drawable.s4, "L4",R.drawable.p),
                    new Level(R.drawable.s6, "L5",R.drawable.p)
                };
        LevelAdapter adapter = new LevelAdapter(getActivity(),
                R.layout.list_item, weather_data);

       View header = (View)getLayoutInflater().inflate(R.layout.header, null);
        lv1.addHeaderView(header);

        lv1.setAdapter(adapter);
        return v;
    }
}

现在,我收到一个错误“

  

Fragment类型中的方法getLayoutInflater(Bundle)不是   适用于arguments()

“在行

View header = (View)getLayoutInflater().inflate(R.layout.header, null);

如何解决此错误? 感谢

1 个答案:

答案 0 :(得分:3)

更改

View header = (View)getLayoutInflater().inflate(R.layout.header, null);

  View header = inflater.inflate(R.layout.header, null);

onCreatView的第一个参数是LayoutInflater