简体   繁体   中英

Dynamically add divider to last item in list view

i am progrmatically adding list view to a linear layout like this:

ArrayList<Answer> ans = (ArrayList<Answer>) ques.getAnswers();
    adapter = new AnswerAdapter(Test.this, ans);

    ansList = new ListView(Test.this); // my list view adding dynamically
    ansList.setAdapter(adapter);
    ansList.setVerticalScrollBarEnabled(false);
    ansList.setOnItemClickListener(cellClickListener);
    ansLayout.addView(ansList);

Now the problem is it has the default divider after each item except the last item, i want the divider to be visible after the last item also.

I have looked into many questions in SO where they are adding views in the layout but i need to add programatically.

You can add footer view in listview... so make a layout of footerview... and bind as below

View footerView = ((LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.track_footer_view, null, false);
listview.addFooterView(footerView);

so you can have footerview at end of last item..

- Add that separator as the footer to your ListView .

Eg:

View mfooter = View.inflate(MyClass.this, R.layout.imagelayout, null);
lv.addFooterView(mfooter, null, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM