带标题的列表视图

时间:2012-04-25 12:44:04

标签: android android-listview

我正在创建一个带标题的列表视图。我创建了header.xml,其中有一个图像。但是当我滚动列表标题时也会滚动。它应该始终位于顶部。公共类CustomListActivity扩展了ListActivity {

    static final String[] Actress = 
               //new String[] { "Katrina Kaif", "Aishwarya", "Genelia", "Vidya", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid"};
    new String[] { "Aishwarya", "Genelia","Katrina Kaif",   "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Khalid", "Vidya",};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lv = getListView();
        View header = getLayoutInflater().inflate(R.layout.header, null);
        lv.addHeaderView(header);
        setListAdapter(new MobileArrayAdapter(getApplicationContext(), Actress));


        lv.setTextFilterEnabled(true);


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}`

4 个答案:

答案 0 :(得分:2)

只需在列表视图上方放置一个视图,无需使用标题吗?

答案 1 :(得分:1)

不要将标题作为另一个单元格,而是创建自定义组件。像( grosso modo ):

<LinearLayout orientation="vertical">
  <Header/>
  <ScrollablePanel>
    <ListView/>
  </ScrollablePanel>
</LinearLayout>

答案 2 :(得分:0)

如果你只是将list.xml中的任何视图放在ListA活动布局文件中ListView的顶部,它就不应该滚动

答案 3 :(得分:0)

试试这个:

LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);