Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Listview show only one item

I have a ListView, where i changed appearence of row, but listview have size of one row, instead of fullscreen.

list_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" >  <TextView     android:layout_width="fill_parent"     android:layout_height="wrap_content"/>  <LinearLayout     android:layout_width="fill_parent"     android:layout_height="wrap_content" >      <TextView         android:id="@+id/textView_news_time"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:gravity="center"         android:textColor="#0082A8" />      <TextView         android:id="@+id/textView_news_header"         android:layout_width="wrap_content"         android:layout_height="wrap_content" /> </LinearLayout>  <TextView     android:layout_width="fill_parent"     android:layout_height="wrap_content" />  </LinearLayout> 

tab_news.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" >  <ListView     android:id="@+id/list_news"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >   </ListView>    </LinearLayout> 

Making Adapter:

public class RSSAdapter extends ArrayAdapter<RSSitem> { Context context;  int layoutResourceId;     ArrayList<RSSitem> data = null; SimpleDateFormat sdf = new SimpleDateFormat("kk:mm "); public RSSAdapter(Context context, int layoutResourceId, ArrayList<RSSitem> data) {     super(context, layoutResourceId, data);     this.layoutResourceId = layoutResourceId;     this.context = context;     this.data = data; }  @Override public View getView(int position, View convertView, ViewGroup parent) {     View row = convertView;     NewsHolder holder = null;      if(row == null)     {         LayoutInflater inflater = ((Activity)context).getLayoutInflater();         row = inflater.inflate(layoutResourceId, parent, false);          holder = new NewsHolder();         holder.txtTime = (TextView)row.findViewById(R.id.textView_news_time);         holder.txtNews = (TextView)row.findViewById(R.id.textView_news_header);          row.setTag(holder);     }     else     {         holder = (NewsHolder)row.getTag();     }      RSSitem item = data.get(position);     holder.txtTime.setText(sdf.format(item.getPubDate()));     holder.txtNews.setText(item.getTitle());      return row; }  static class NewsHolder {     TextView txtTime;     TextView txtNews; } 

Items are scrolling.

Sorry for bad English. Thanks for help

Advice of @Varun didn't help

I found mistake. Thanks. Answer in "answers")

like image 336
Artem Avatar asked Aug 23 '13 20:08

Artem


2 Answers

I Found mistake. tab_news was in ScrollView of TabHost. So stupid mistake( Thanks for help.

Update:ListView must not be underScrollView in xml. If it is,the same problem occurs.

Update 2 :You may use NestedScrollView

like image 197
Artem Avatar answered Sep 21 '22 17:09

Artem


If you use ListView inside ScrollView, you have this issue. And my solution is following as:

1. Create a custom listview which is non scrollable

public class NonScrollListView extends ListView {  public NonScrollListView(Context context) {     super(context); } public NonScrollListView(Context context, AttributeSet attrs) {     super(context, attrs); } public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {     super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(                 Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);         super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);         ViewGroup.LayoutParams params = getLayoutParams();         params.height = getMeasuredHeight();     } 

}

2. Use above custom class for xml file

  <xx.xx.NonScrollListView         android:id="@+id/lv_nonscroll_list"         android:layout_width="match_parent"         android:layout_height="wrap_content" >     </xx.xx.NonScrollListView> 

It worked well on all OS-version for me. Hope best for you.

like image 44
Hai Rom Avatar answered Sep 20 '22 17:09

Hai Rom