开发者

Fill parent not working on My list view

Here is my xml code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#E7F7FF" android:gravity="top">
    <!-- Header text here -->
    <TextView />
    <!-- Scrollable layout -->
    <ScrollView >
          <RelativeLayout android:layout_height="fill_parent" android:layout_marginTop="5dip" android:layout_width="fill_parent">       
            <LinearLayout >
                <ImageView />
            </LinearLayout>
            <LinearLayout >
                <TextView  />
                <TextView />
                <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
                    <ListView android:id="@+id/contact_number_list_view" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="fill_parent"                   
                        android:layout_marginTop="3dp" android:layout_marginBottom="1dp" android:layout_marginRight="5dip" android:clickable="true"
                        android:cacheColorHint="#00000000" android:divider="@android:color/transparent" android:dividerHeight="2dip" android:fastScrollEnabled="true" 
    开发者_StackOverflow社区                    android:fadeScrollbars="true" android:listSelector="@drawable/rounded_corner_item_selecter" android:layout_gravity="top"></ListView>
                    <TextView />    
                </FrameLayout>      
            </LinearLayout>

            <!-- Show tags details layout -->
            <LinearLayout  >
            <TableLayout > 

            </TableLayout>
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>       
    <!-- Now button layouts here -->  
    <LinearLayout >
        <Button />
        <Button/>
        <Button />
    </LinearLayout>
</RelativeLayout>

And I want to expand list view as what number of data in it. But It is not expand in size. Why?


As explained by the programmers that did the listView in this video from GoogleIo never put a ListView inside a scroll View. If your list should not scroll use a ViewGroup like a linear Layout and add all the items to this ViewGroup in a loop in your code. If you want a whole row to be clickable you have to use another ViewGroup as the root node for each row and add the OnClickListener to this View.

So just replace your ListView with LinearLayout as

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linear_layout_which_is_present_at_place_of_list_view"                                      
android:orientation="vertical"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"/>   

and Let you were using two TextView as custom row of list view. So custom row view will be row_view_layout.xml as

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">       
<TextView android:layout_width="wrap_content"
android:clickable="true"        
android:id="@+id/row_view_layout_text1"
android:layout_height="wrap_content" />
<TextView android:id="@+id/row_view_layout_text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
</LinearLayout>

Sample Code:

//Let you were using two TextView as custom row of list view.
LinearLayout list = (LinearLayout)findViewById(R.id.linear_layout_which_is_present_at_place_of_list_view);
LayoutInflater inflater = LayoutInflater.from(context);
list.removeAllViews(); //Remove previously created views
for(String row : getAllRow()) {
       View vi = inflater.inflate(R.layout.row_view_layout, null);
       textOne = (TextView) vi.findViewById(R.id.row_view_layout_text1);
       textTwo = (TextView) vi.findViewById(R.id.row_view_layout_text2);

       final String t1 = row.getFirstValue();
       final String t2 = row.getSecondValue();

       textOne.setText(t1);
       textTwo.setText(t2);

       //Set listener for both text views

       textOne.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 Toast.makeText(context, "You clicked on : " + t1  , Toast.LENGTH_SHORT).show();
             }
        });

       textTwo.setOnClickListener(new OnClickListener() {                
             public void onClick(View v) {
                 Toast.makeText(context, "You clicked on : "  + t2 , Toast.LENGTH_SHORT).show();
             }
       });

       //Add view          
       list.addView(vi);
}

Hope this will help.

Happy coding.


 <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
                    <ListView android:id="@+id/contact_number_list_view" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="fill_parent"                   
                        android:layout_marginTop="3dp" android:layout_marginBottom="1dp" android:layout_marginRight="5dip" android:clickable="true"
                        android:cacheColorHint="#00000000" android:divider="@android:color/transparent" android:dividerHeight="2dip" android:fastScrollEnabled="true" 
                        android:fadeScrollbars="true" android:listSelector="@drawable/rounded_corner_item_selecter" android:layout_gravity="top">

//HERE PUT THE ITEM VIEWS ELEMENTS OF THE LISTIVIEW

</ListView>

I cant see anything else that might be causing your problem...


Set width and height of ScrollView Something like this :

<ScrollView 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

     <!-- Contents -->

</ScrollView>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜