开发者

Android: Inflating Linear Views within ScrollView

So I have a class that gets items from a database and then places them into a layout and inflates. This loops through each item in the database inflating a new layout for each item. This eventually extends past the viewport and will need a scroll bar which I assume will be a ScrollView. I've tried many different ways and can't get it to scroll through the entire content. Please advise.

Java:

View myView = linflater.inflate(R.layout.myviews, null);

TextView tvQuote = (TextView) myView.findViewById(R.id.lblQuote);
//Set an id to know which quote is in use
tvQuote.setId(i);
TextView tvShared = (TextView) myView.findViewById(R.id.lblShared);
TextView tvSaid = (TextView) myView.findViewById(R.id.lblSaid);

//Change name dynamically
tvQuote.setText((strQuote).toString());
tvShared.setText(("Shared by: " + strFName + " " + strLInitial).toString());
tvSaid.setText(("Said by: " + strFNameSaid + " " + strLInitialSaid).toString());

main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        android:orientation="vertical" 
        android:id="@+id/myMainLayout" 
        android:layout_width="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_height="wrap_content">   
    <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:text="Quoter" 
        android:id="@+id/lblTitle" 
        android:textSize="16px" 
        android:padding="5px" 
        android:textStyle="bold" 
        android:gravity="center_horizontal">
    </TextView>
</LinearLayout>

myviews.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <TextView 
                android:id="@+id/lblQuote" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           开发者_如何学JAVA android:layout_weight="1">

            <TextView 
                android:id="@+id/lblShared" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <TextView 
                android:id="@+id/lblSaid" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_weight="1"/> 
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
          <TextView
            android:text=" "
            android:textSize="1pt"
            android:background="#6F7285"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>     
        </LinearLayout>
</LinearLayout>

What would be the best solution to scroll through all of these inflated layouts?


It sounds like you're doing too much work. If your data is in a SQLite database then you can use a SimpleCursorAdapter with your ListView which will do the heavy lifting for you:

public class MyListActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // The my_list_activity layout is the container for each
        // item in the list.
        setContentView(R.layout.my_list_activity);

        // Create a database cursor for the data you want to query.
        // Assumes your data is accessible via a ContentProvider.

        // Note: db.CONTENT_URI is whatever URI your ContentProvider uses,
        // and db.COL1 etc are constants that would name the columns you want.
        cursor = getContentResolver().query(db.CONTENT_URI,
            new String[] { db._ID, db.COL1, db.COL2, db.COL3 },
            null, null, null);

        // Bind the value in db.COL1 to the resource with the id text1, and so
        // on for the other two columns.
        //
        // Assumes that the view has TextViews with IDs text1, text2, and text3.
        String[] from= new String[] { db.COL1, db.COL2, db.COL3 };
        int[] to = new int[] { R.id.text1, R.id.text2, R.id.text3 };

        // Create the adapter, bound to the cursor, layout, and the mapping from
        // column name to TextView ID.
        //
        // Assumes a layout called my_list_activity_item exists, with at least
        // 3 views called text1, text2, and text3.
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
            R.layout.my_list_activity_item, cursor, from, to);

        setListAdapter(mAdapter);
    }
}

If your data is not in an SQLite database then look at the other classes that derive from android.widget.BaseAdapter to see if one of them suits you.


You are using too many linearlayouts in your myviews.xml. This should do the trick for you:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <TextView 
            android:id="@+id/lblQuote" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="text"
            />
        <TextView 
            android:id="@+id/lblShared" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@id/lblQuote" 
            android:text="text"
            />
        <TextView 
            android:id="@+id/lblSaid" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@id/lblQuote" 
            android:layout_alignParentRight="true"
            android:text="text"
            /> 
        <TextView
            android:text="text "
            android:background="#6F7285"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblShared" 
            />     
    </RelativeLayout>
</ScrollView>

I added some placeholder text so you can see it. Remove whatever you don't need.


I think the quick answer is that you need a ScrollView around all of those LinearLayouts so you can actually scroll (your first LinearLayout has height=wrap_content). The long version is that you're probably doing it wrong and you should be using a ListView and an Adapter instead.


I actually found that adding the ScrollView to the main.xml was the fix. Thanks for all your help though guys. I'd still like to know if I am doing this wrong though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜