Как я могу получить экземпляр пользовательского представления для извлечения в цикле? - PullRequest
0 голосов
/ 20 июля 2011

Я хотел бы показать строки в соответствии с моими данными.Например, я буду динамически создавать 10 строк в цикле, и эта строка на самом деле является пользовательским представлением.Поэтому мне нужно сделать это в основной деятельности.Можете ли вы показать мне, как это сделать?

example.xml

<TableLayout 
android:id="@+id/tableLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
        <ImageView android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_width="wrap_content"></ImageView>

        <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="fill_parent">
        <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView1" android:text="titletitletitletitletitletitle"></TextView>
        <TextView android:text="DescriptionDescriptionDescription" android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
        </LinearLayout>

    </TableRow>   
</TableLayout>`

CustomView.java

public class CustomView extends LinearLayout
{
    public CustomView(Context context, AttributeSet attrs)
    {
        super(context,attrs);       
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.example,this);
    }

    private void init(Context context){

    }
}

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

    <ScrollView android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">  
    </ScrollView>  

</LinearLayout>

1 Ответ

0 голосов
/ 20 июля 2011

вы можете использовать список адаптеров и оболочку и каждый раз раздувать пользовательский вид. На самом деле, во время второго раунда, чтобы ваш код был более эффективным, вы должны «повторно» использовать завышенное представление, чтобы не раздувать каждый раз. Проверьте, например, этот урок: http://mylifewithandroid.blogspot.com/2008/04/custom-widget-adapters.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...