隐藏列表视图 header 的一部分(文本)

hide a part (text) of a header of a list view

我有自己的 Adapter 一个 listView 和一个 header。我想选择隐藏 listview 的哪些列;它适用于我的带有 textViewIdBaseDatos.setVisibility(View.GONE) 的适配器中的列表视图,但我不能在 header 中这样做。我可以在 header.xml 中使用 textView 中的 android:visibility="gone" 我想隐藏,但我想选择想要 show/hide 的列作为首选项。

您可以在屏幕中看到 header 和 listView


header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fondoAccent">

    <TextView
        android:id="@+id/editTextIdBaseDatos"
       
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:maxLength="2"
        android:text="@string/idbasedatos"
        android:textColor="@color/headlineList"
        android:textSize="@dimen/testSizeHeadList"
        android:background="@color/fondoAccent2"/>

    <TextView
        android:id="@+id/editTextPriority"
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:maxLength="1"
        android:text="@string/priority"
        android:textColor="@color/headlineList"
        android:textSize="@dimen/testSizeHeadList"

        android:background="@color/fondoAccent2"/>

    <TextView
        android:id="@+id/editTextTarea"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLength="7"
        android:text="@string/task"
        android:textSize="@dimen/testSizeHeadList"
        android:textColor="@color/headlineList"
        android:background="@color/fondoAccent2"/>

显示 header 和 listView 的片段的一部分:

    Adapter = new MyAdapter(getActivity(), R.id.listView, idBaseDatos, name, description, start, startime, finished, duration, priority); // enlazamos el adaptador que hemos creado en clase java myAdapter con nuestra vista
        listView.setAdapter(myAdapter);
        listView.addHeaderView(headerView); // añade la cabecera a la listView

myAdapter的一部分:

public class MyAdapter extends BaseAdapter {
    // 3 variables
    private Context context;
    private int layout;
    private List<String> priority;
    private List<String> nameTask;
    private List<String> description;
    private List<String> start;
    private List<String> startime;
    private List<String> idBasedatos;
    private List<Integer> finished;
    private List<String> duration;
    // private int colorFinished = 0xFF00FF00 ;
    private  int colorFinished;


    // constructor que recoge el contexto, el layout y los nombres
    public MyAdapter(Context context, int layout,List<String> idBasedatos, List<String> nameTask, List<String> description, List<String> start, List<String> startime, List<Integer> finished , List<String> duration, List<String> priority){
        this.context = context;
        this.priority = priority;
        this.layout = layout;
        this.nameTask = nameTask;
        this.description = description;
        this.start = start; // date
        this.startime = startime; // time in 24 H format
        this.duration = duration;
        this.idBasedatos = idBasedatos;
        this.finished = finished; // Integer que hay que pasar a booleano
        colorFinished = ContextCompat.getColor(context, R.color.colorPrimarySoft);

您必须首先使用 listView 作为 parent 膨胀 header 视图。 现在,您可以在将此 header 视图作为 header 添加到 listView 之前对其执行任何操作。例如:

ListView listView = findViewById(R.id.list);
View header = getLayoutInflater().inflate(R.layout.header, listView, false);
header.findViewById(R.id.viewToHide).setVisibility(View.GONE);
listView.addHeaderView(header);

你也可以参考其他类似的回答:https://whosebug.com/a/25867095/2621237