具有固定页眉和页脚的ListView以编程方式

时间:2017-02-21 21:34:23

标签: android listview header

所以我试图以编程方式创建一个带有固定页眉和页脚的listView,但我已经看过了,我不能使用listView上的addHeader修复标题,所以我和#39;我试图将linearlayout与其他linearlayout一起使用,这将是我的固定页眉和页脚,在页眉和页脚之间是listview,但是当我尝试listView只是覆盖了entiry屏幕时,我已经使用了relativelayout试图强制标题在listView上方,但我不能。我昨天开始使用Android工作室和制作Android程序,所以我试图学习更多能力。但我已经在互联网上看了一下,我发现的任何东西都没有帮助我,因为这些例子使用了很多xml,我试图用作最后的资源。出于兼容性原因,我使用的是16版本的android。

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_user_event_search"
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="takeme.testers.UserEventSearch">

</RelativeLayout>

java文件:

private static String[] items = { "test 1", "test 2", "test 3", "test 4",
        "test 5", "test 6", "test 7", "test 8", "test 9", "test 10",
        "test 11", "test 12", "test 13", "test 14", "test 15"};

private ListView eventsView;
private LinearLayout mainLayout, viewHeader, viewFooter, listViewHeader;
private RelativeLayout.LayoutParams params1, params2, params3;
private ArrayAdapter adapter;

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

    mainLayout = new LinearLayout(this.getApplicationContext());
    mainLayout.setOrientation(LinearLayout.VERTICAL);
    mainLayout.setBackgroundColor(Color.TRANSPARENT);
    mainLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    this.setContentView(mainLayout);
    params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


    viewHeader = new LinearLayout(this.getApplicationContext());
    viewHeader.setOrientation(LinearLayout.HORIZONTAL);
    viewHeader.setBackgroundColor(Color.BLACK);
    viewHeader.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 100));
    viewHeader.setId(R.id.l0);
    viewHeader.setGravity(Gravity.TOP);



    eventsView = new ListView(this.getApplicationContext());
    eventsView.setBackgroundColor(Color.RED);
    eventsView.setId(R.id.l1);
    eventsView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 300));
    params2.addRule(RelativeLayout.BELOW, viewHeader.getId());


    listViewHeader = new LinearLayout(this.getApplicationContext());
    listViewHeader.setOrientation(LinearLayout.HORIZONTAL);
    listViewHeader.setBackgroundColor(Color.WHITE);
    listViewHeader.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 100));

    eventsView.addHeaderView(listViewHeader, null, false);


    viewFooter = new LinearLayout(this.getApplicationContext());
    viewFooter.setOrientation(LinearLayout.HORIZONTAL);
    viewFooter.setGravity(Gravity.BOTTOM);
    viewFooter.setBackgroundColor(Color.BLUE);
    viewFooter.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, 100));
    viewFooter.setId(R.id.l2);
    params3.addRule(RelativeLayout.BELOW, eventsView.getId());

    this.addContentView(viewHeader, params1);
    this.addContentView(eventsView, params2);
    this.addContentView(viewFooter, params3);

    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items);
    eventsView.setAdapter(adapter);
}

目前的结果:

enter image description here

enter image description here

0 个答案:

没有答案