在Android中使用布局xml作为重复内容的模板

在Android中使用布局xml作为重复内容的模板,android,android-layout,android-xml,Android,Android Layout,Android Xml,我有一个模板布局,它代表我应用程序中ListView中的每一行。 这是photorow.xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/bodylay" android:layout_width="match_parent" android:layout_

我有一个模板布局,它代表我应用程序中ListView中的每一行。 这是photorow.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bodylay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/leftbigsquare"
    android:layout_width="0dp"
    android:layout_height="140dp"
    android:layout_weight="1"
    android:background="#1000b0"
    android:src="@drawable/test1"
    />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#d0b0b0"
        android:textSize="15sp" >

        <ImageView
            android:id="@+id/righttupperleft"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#b170b0"
            android:src="@drawable/test1" />

        <ImageView
            android:id="@+id/rightupperright"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:background="#b110b0"
            android:src="@drawable/test1" />
    </LinearLayout>

    <ImageView
        android:id="@+id/righthorizontal"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:src="@drawable/test1" />
</LinearLayout>
我想使用我的photorow.xml作为模板,只需更改其属性,例如每行中的source。 是否可以使用my photorow.xml作为行的模板


编辑 我的自定义适配器:

package com.example.test2;

import java.util.List;

import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PhotoAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<PhotoRow> rowList;
    Activity context = null;

    public PhotoAdapter(Activity activity, List<PhotoRow> rows) {

        mInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowList = rows;
        context = activity;
    }

    @Override
    public int getCount() {
        return rowList.size();
    }

    @Override
    public Object getItem(int position) {
        return rowList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView;


        rowView = mInflater.inflate(R.layout.photorow, null);



        //Here I get the ImageView of my photorow xml
        ImageView leftBigSquare = (ImageView) rowView
                .findViewById(R.id.leftbigsquare);


        // Here I get the proper URL from rowList  by using Picasso framework
        Picasso.with(context).load(rowList.get(position).getUrls().get(0))
                .resize(50, 50).centerCrop().into(leftBigSquare);

        //So how can I put the leftBigSquare ImageView to my photorow template with its proper attributes


        return rowView;
    }

}
package com.example.test2;
导入java.util.List;
导入com.squareup.picasso.picasso;
导入android.app.Activity;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类PhotoAdapter扩展了BaseAdapter{
私人停车场;
私有列表行列表;
活动上下文=null;
公共PhotoAdapter(活动,列表行){
mInflater=(LayoutInflater)活动
.getSystemService(上下文布局\充气机\服务);
行列表=行;
上下文=活动;
}
@凌驾
public int getCount(){
返回rowList.size();
}
@凌驾
公共对象getItem(int位置){
返回rowList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行视图;
rowView=mInflater.inflate(R.layout.photorow,空);
//这里我得到了PhotoRowXML的ImageView
ImageView leftBigSquare=(ImageView)行视图
.findviewbyd(R.id.leftbigsquare);
//在这里,我使用毕加索框架从行列表中获得正确的URL
Picasso.with(context).load(rowList.get(position.getURL().get(0))
.resize(50,50).centerCrop()到(leftBigSquare);
//那么,如何将leftBigSquare ImageView与其适当的属性放在photorow模板中呢
返回行视图;
}
}

是的,当然,您可以使用相同的rowview XML,但每一行中仍然有不同的数据

您必须使用自定义适配器,然后从该适配器充气行,并根据多种情况更改数据

粘贴您的自定义适配器代码,我将告诉您如何做

package com.example.test2;

import java.util.List;

import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PhotoAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<PhotoRow> rowList;
    Activity context = null;

    public PhotoAdapter(Activity activity, List<PhotoRow> rows) {

        mInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowList = rows;
        context = activity;
    }

    @Override
    public int getCount() {
        return rowList.size();
    }

    @Override
    public Object getItem(int position) {
        return rowList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView;


        rowView = mInflater.inflate(R.layout.photorow, null);



        //Here I get the ImageView of my photorow xml
        ImageView leftBigSquare = (ImageView) rowView
                .findViewById(R.id.leftbigsquare);


        // Here I get the proper URL from rowList  by using Picasso framework
        Picasso.with(context).load(rowList.get(position).getUrls().get(0))
                .resize(50, 50).centerCrop().into(leftBigSquare);

        //So how can I put the leftBigSquare ImageView to my photorow template with its proper attributes


        return rowView;
    }

}