Scroll ListView and TextView Android - android

I want to create a layout composed of :
textview, as a title
listview
and all when the user scrolls down/up all the content scrolls together (not only inside the listview)
Any ideas?

Why don't you load the title into the listview, as the top item, and make your activity extend ListActivity?
If you want the title to be formatted differently, you can specify this on the getView() method --
if (position == 0) {
//format title item }
else {
//format other items

You can define your textview like this:
View textView = ((LayoutInflater)Activity.this.getSystemService(Activity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.textview_title, null, false);
and then add it as a listview header
myListView.addHeaderView(textView);
If you want to set the selection to be the first list item after the header view you can use
myListView.setSelectionAfterHeaderView();

Related

Cannot display empty textview in List Fragmant when the list has no items

I am displaying a list in a listfragmant inside a ViewPager. I have looked for varoius solutions but none worked for me.
Solution 1-
if ((ArrayList) myList.size() > 0) {
setListadapter(myAdapter);
} else {
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
}
Solution 2-
if ((ArrayList) myList.size() > 0) {
setListadapter(myAdapter);
} else {
setEmptyText("No Items");
}
When the list is empty it shows nothing and whole page is empty.I have defined the requisite XML too.
You still have to set the adapter. If the adapter is empty, then the ListFragment will automatically hide ListView and show the empty view. So, the proper way to do it would be just:
setEmptyText("No Items");
setListadapter(myAdapter);
you have three ways:
1) create fragment and add ListView to it's layout then behind Listview put Textview so when List was empty,TextView will appear.if list has child then TextView not appear.
2) if your array has no item , add one Item to your array with any text( like :"No Items") and set adapter for listview.
3) set header for your ListView(layout can be textview that you want)

Custom adapter replaces old items when adding new

I have a problem with my custom list adapter.
My main activity consist of a ListView, Button and a EditText field.
When the user inputs something in the EditText field, and presses the button. The text should
appear in the ListView. For the ListView i have made a custom adapter. That at the moment only consist of a TextView, but i will later add more stuff so i can not use a standard adapter.
The problem is that when the user types something, and presses the button, the string is placed in the ListView, (great!), but the problem is when the user presses again, that the old text is replaced by the new (the list has 2 items, both contain same text). When the user adds a third item to the list, this item replaces the old ones, and i now have 3 of the newest in the list?
So the problem is that the newest item replaces the old ones?
In my mainActivity i have this:
private ListView list_messages;
private Button send_button;
private TextView input_message;
public Custome_list_item adapter;
public ArrayList<String> message_holder;
message_holder = new ArrayList<String>();
adapter = new Custome_list_item(this,message_holder);
list_messages.setAdapter(adapter);
The custom list adapter only has a getView, like this:
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.message_in_list_row);
ImageView imageView = (ImageView) rowView.findViewById(R.id.user_profile);
TextView timeView = (TextView) rowView.findViewById(R.id.date_and_time);
txtTitle.setText(message_holder.get(message_holder.size()-1));
//imageView.setImageResource(imageId);
timeView.setText("DD:MM:YY");
return rowView;
when the user clicks the button in the main activity, i do this:
mainActivity.message_holder.add(item);
mainActivity.adapter.notifyDataSetChanged();
Any help is appreciated
Your getview is using size in the .get function, so it will always return the last string in the array as the text. It should be using the position passed in to the call.
Try Changing
txtTitle.setText(message_holder.get(message_holder.size()-1));
to
txtTitle.setText(message_holder.get(position));
Because position will replace the view's position to the item,i.e. On first item the txtTitle from first adapter position will be displayed.On second listItem the second Adapter View will be displayed and so on.

how to change the listview content only without changing list of items

I have a listview in which every listitem has one image and 4 textviews. I want to change only 2 textviews without changing the list and list of items.
Those textviews are changed every second.
Is it possible in android?
You can update the text items of listView like this:
private void updateText(int index){
View v = ListView.getChildAt(index);
TextView updatedText = (TextView) v.findViewById(R.id.txt_view);
updatedText .setText("changed Text");
}

Show header always in Android ListView

I need to show ListView header always, even when ListView has no items.
It is possible to do? Or better add header in ListView as first item?
If there's no elements for list and you are not adding the adapter, just add this:
mListView.setAdapter(null);
and the header will appear.
In your activity in onCreate You can add header by adding code:
View headerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header_view, null, false);
getListView().addHeaderView(headerView);
In most cases no need to add header, You can just add items in xml layout.
As case you can use http://viewpagerindicator.com/
and set getListView().setEmptyView(emptyView); // when your listView has no items
My solution was to manually toggle the empty view.
/**
* Custom empty view handling because we don't want the
* list to be hidden when the empty view is displayed,
* since the list must always display the header.
*/
private void toggleEmptyView(Adapter adapter)
{
final View emptyView = findViewById(R.id.empty_view);
adapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
emptyView.setVisibility(adapter.getCount() == 0 ? View.VISIBLE : View.GONE);
}
});
}
Irrespective of whether your ListView has elements or not, header will be visible always. If there are elements in ListView, header will scroll along with them else it will be on top always.
For how to add header view to ListView, you can refer other answers provided by users.

Synchronize two ListView positions when you scroll from both list views Android

I have two ListViews. Is there any way to synchronize the position of ListViews when I scroll both the Lists
Use the following method to get the scroll position of your first listView-
private void saveListScrollPosition()
{
// save index and top position
index = _listview1.getFirstVisiblePosition();
View view = _listview1.getChildAt(0);
top = (view == null) ? 0 : view.getTop();
}
And scroll the second listView to that position with-
// restore
_listview2.setSelectionFromTop(index, top);
You could use this in your second list view: smoothScrollToPosition(position)
And in your first ListView you could use a OnScrollListener and check the first visible item with getFirstVisiblePosition.
Best wishes,
Tim

Categories

Resources