RecycleView中有多个TextView。怎么做?

时间:2019-01-23 15:30:09

标签: java android android-recyclerview

你好,我刚刚启动Android,但我不明白如何在RecyclerView中放置更多TextView 我已经看过这种解决方案,但我不明白:How to create RecyclerView with multiple view type?

我试图制作一个具有多个维度的表,但是它不起作用。

适配器:

public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {

    private String[]data;
    public DeviceRecyclerView(String[]data){
        this.data = data;

    }
    @Override
    public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.device_list_row, parent , false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String name = data[position];
        String rssi = data[position];
        String uuid = data[position];

        holder.DeviceNameTextView.setText(name);
        holder.DeviceUUIDTextViewValue.setText(rssi);
        holder.DeviceRSSIVTextViewValue.setText(uuid);


    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView DeviceNameTextView;
        TextView DeviceUUIDTextViewValue;
        TextView DeviceRSSIVTextViewValue;

        public ViewHolder (View itemView){
            super(itemView);
            DeviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
            DeviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
            DeviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
        }
    }

MainActivity:

RecyclerView deviceList = (RecyclerView)findViewById(R.id.recycler_view_NoPaired);
        deviceList.setLayoutManager(new LinearLayoutManager(this));
        String[]names = {"Samsung64656"};
        String[]rssi = {"ezez"};
        String[]uuid = {"08:90:e5:90"};
        deviceList.setAdapter(new DeviceRecyclerView(names));

我的XML。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:gravity="center_vertical"
    tools:layout_editor_absoluteX="30dp"
    tools:layout_editor_absoluteY="81dp">
    <View
        android:id="@+id/ColoredRect"
        android:layout_width="10dp"
        android:layout_height="160dp"
        android:layout_marginBottom="16dp"

        android:layout_marginTop="16dp"
        android:background="#E27F26"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/DeviceNameTextView"
        android:layout_width="133dp"
        android:layout_height="24dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="16dp"
        android:text="@string/device_name_label"
        android:textColor="@color/TextColor"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/DeviceUUIDTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="8dp"
        android:text="@string/deviceUUIDTextView"
        android:textColor="@color/TextColor"
        android:textSize="12sp"
        app:layout_constraintBottom_toTopOf="@+id/DeviceUUIDTextViewValue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeviceNameTextView" />

    <TextView
        android:id="@+id/DeviceUUIDTextViewValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="14sp"
        android:text=""
        android:textColor="@color/TextColor"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeviceUUIDTv" />

    <TextView
        android:id="@+id/rssiLabelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="16dp"
        android:text="@string/deviceRSSITextView"
        android:textColor="@color/TextColor"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeviceUUIDTextViewValue" />

    <TextView
        android:id="@+id/DeviceRSSIVTextViewValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="14sp"
        android:text=""
        android:textColor="@color/TextColor"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rssiLabelTextView" />


</android.support.constraint.ConstraintLayout>

请清楚地向我解释该怎么做。我很抱歉拼写错误。

1 个答案:

答案 0 :(得分:1)

您只需要在XML行(名为device_list_row.xml)中添加一些textView,然后在适配器中对其进行初始化,就像您已经对3 textview所做的那样。

但是使用Object比使用字符串数组存储数据更容易。例如,在您的情况下,使Device类对象

Device.java
public class Device {
        String name;
        String rssi;
        String uuid;
        public String getName() {
                return name;
        }

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

        public String getRssi() {
            return rssi;
        }

        public void setRssi(String rssi) {
            this.rssi = rssi;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }


}

和适配器代码->

public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {

private ArrayList<Device> deviceList;
public DeviceRecyclerView(Arraylist<Device> deviceList){
    this.deviceList= deviceList;

}
@Override
public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.device_list_row, parent , false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Device device = deviceList.get(position);

    holder.deviceNameTextView.setText(device.getName());
    holder.deviceUUIDTextViewValue.setText(device.getUuid());
    holder.deviceRSSIVTextViewValue.setText(device.getRssi());



}

@Override
public int getItemCount() {
    return deviceList.length;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView deviceNameTextView, deviceUUIDTextViewValue, deviceRSSIVTextViewValue;

    public ViewHolder(View itemView)  {

        super(itemView);
        itemView.setOnClickListener(this);
        deviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
        deviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
        deviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
    }

    @Override
    public void onClick(View v) {
        Log.i("DEBUG","Item RecyclerView Cliqué");
    }
}

这样,您可以使用设备对象列表来初始化适配器,该设备对象可以具有您想要的任何变量,而无需将更多的数组或数据传递给适配器:-)

然后制作新设备并在RV中使用

Device device = new Device();
device.setName("Device Name 01");
device.setUuid("uuid value");
device.SetRssi("rssi value");

ArrayList<Device> deviceList = new ArrayList<>();
deviceList.add(device);

//Init adapter and fill it
DeviceRecyclerView deviceRecyclerView = new DeviceRecyclerView(deviceList);


LinearLayoutManager llm = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(llm);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(deviceRecyclerView);