Clonning/Duplicating XML 线性布局,循环,动态数据 - Android Studio

Clonning/Duplicating XML Linear Layout, in a loop, with dynamic data - Android Studio

My Layout Screenshot 这是我的全部activity,你看到的list里的三个item,是三个不同的LinearLayouts,我想做的是通过一个循环复制一个Layout到multi,放上动态数据,比如: 1) Hafeez ul haq,男,朴 2) 阿里,男,印度 3) 雏,女,朴

抱歉这些愚蠢的例子,但我主要想学习如何复制 LinearLayout,您可以使用带有 for 循环的字符串数组,例如:for(user in users){ Blah...Blah... }

你应该使用 RecyclerView.

示例:

数据模型

public class Person {
    private String name;
    private int gender;
    private String location;

    public Person(String name, int gender, String location) {
        this.name = name;
        this.gender = gender;
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

RecyclerView 适配器

public final class PeopleAdapter extends RecyclerView.Adapter<PeopleAdapter.PersonHolder> {
    private Context context;
    private List<Person> people;

    public PeopleAdapter(Context context, List<Person> people) {
        this.context = context;
        this.people = people;
    }

    @Override
    public PeopleAdapter.PersonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PersonHolder(LayoutInflater.from(context).inflate(R.layout.item_person, parent, false));
    }

    @Override
    public void onBindViewHolder(PeopleAdapter.PersonHolder holder, int position) {
        Person person = people.get(position);
        holder.txtPersonName.setText(person.getName());
        if(person.getGender() == 0)
            holder.txtGender.setText("Male");
        else if(person.getGender() == 1)
            holder.txtGender.setText("Female");
        else
            holder.txtGender.setText("_____");
        holder.txtPersonLocation.setText(person.getLocation());
    }

    @Override
    public int getItemCount() {
        return people.size();
    }

    final class PersonHolder extends RecyclerView.ViewHolder {
        private AppCompatTextView txtPersonName;
        private AppCompatTextView txtGender;
        private AppCompatTextView txtPersonLocation;

        PersonHolder(View itemView) {
            super(itemView);

            txtPersonName = itemView.findViewById(R.id.txt_person_name);
            txtGender = itemView.findViewById(R.id.txt_gender);
            txtPersonLocation = itemView.findViewById(R.id.txt_person_location);
        }
    }
}

在你的activity/fragment

private RecyclerView rvPeople;
private PeopleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    rvPeople = findViewById(R.id.rv_people);
    List<Person> people = new ArrayList<>(3);
    ...
    adapter = new PeopleAdapter(this, people);
    ...
    rvPeople.setAdapter(adapter);
}

您的项目布局。我通常使用以下代码来决定你的任务。您可以修改边距、填充、图像大小和需要样式文本视图。

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/white"
    android:padding="8dp">

    <androidx.appcompat.widget.AppCompatImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@drawable/ic_launcher_background" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginStart="16dp">

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-medium"
            tools:text="Text 1" />

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Text 1" />

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Text 1" />

    </LinearLayout>

</LinearLayout>