android - 布局可见性更改+特定流程后微调器值消失

标签 android listview spinner

我有一个自定义 ListView ,其布局包含两个布局,称为上部和底部。

上层布局包含微调器、设置和删除按钮。 底部布局包含 text view 和后退按钮。

默认底部布局处于GONE状态,当用户点击set按钮时,upper布局为GONE 并且底部是 VISIBLE(在底部布局中单击后退按钮将返回上底部)。

我的问题是微调值在特定流程后消失:

流程 1:

  1. 添加两个项目
  2. 点击第一项上的SET按钮
  3. 删除第二项
  4. 点击第一个项目上的“后退”按钮

Spinner value is disappear

流程 2:

  1. 点击设置
  2. 旋转屏幕
  3. 点击返回

需要说明的是,微调器值是存在的,如果将其下拉,您将找到名称(我的 list 中有 android:configChanges="keyboardHidden|orientation|screenSize")。

那么,为什么 spinner 值在这些流之后消失了?

这是我的代码:

Names.java

public class Names
{
    private String name;
    private int nameIndex;
    private Boolean isNameOnTop;


    public Names()
    {
        name = "";
        isNameOnTop = true;
    }

    public String getName() {
        return name;
    }

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

    public int getNameIndex() {
        return nameIndex;
    }

    public void setNameIndex(int nameIndex) {
        this.nameIndex = nameIndex;
    }

    public Boolean getIsNameOnTop() {
        return isNameOnTop;
    }

    public void setIsNameOnTop(Boolean isNameOnTop) {
        this.isNameOnTop = isNameOnTop;
    }
}

MainActivity.Java

public class MainActivity extends Activity
{
    ArrayList<Names> namesArray = new ArrayList<>();
    ListView lvNames;
    ListviewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvNames = (ListView) findViewById(R.id.listView);
        adapter = new ListviewAdapter(this, namesArray);
        lvNames.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_add)
        {
            namesArray.add(new Names());
            adapter.notifyDataSetChanged();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

ListviewAdapter.java

public class ListviewAdapter extends BaseAdapter
{
    public Activity context;
    public LayoutInflater inflater;
    private ArrayList<Names> namesID;
    private boolean isDeleted;

    public ArrayList<Names> getNamesID() {
        return namesID;
    }

    public void setNamesID(ArrayList<Names> namesID) {
        this.namesID = namesID;
    }

    // Constructor
    public ListviewAdapter(Activity context, ArrayList<Names> names)
    {
        super();
        setNamesID(names);

        this.context = context;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Names getItem(int position) {
        return getNamesID().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
public class ViewHolder
    {
        RelativeLayout relativeLayout_Upper;
        RelativeLayout relativeLayout_Bottom;

        Spinner spNames;
        Button btn_set, btn_remove, btn_back;
        TextView tvChosen;

        int index;
    }

    @Override
    public View getView(final int i, View view, final ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listview_row, null);

            holder.relativeLayout_Upper = (RelativeLayout) view.findViewById(R.id.lvRow_upper_layout);
            holder.relativeLayout_Bottom = (RelativeLayout) view.findViewById(R.id.lvRow_bottom_layout);
            holder.spNames = (Spinner) view.findViewById(R.id.spNames);
            holder.btn_set = (Button) view.findViewById(R.id.btn_set);
            holder.btn_remove = (Button) view.findViewById(R.id.btn_remove);
            holder.btn_back = (Button) view.findViewById(R.id.btn_back);
            holder.tvChosen = (TextView) view.findViewById(R.id.tv_chosen);
            view.setTag(holder);
        }
        else
            holder = (ViewHolder) view.getTag();
        holder.index = i;

        if (isDeleted)
        {
            holder.spNames.setSelection(getItem(holder.index).getNameIndex());
            holder.tvChosen.setText("Chosen: " + getItem(holder.index).getName());

            if (getItem(holder.index).getIsNameOnTop())
            {
                holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
                holder.relativeLayout_Bottom.setVisibility(View.GONE);
            }
            else
            {
                holder.relativeLayout_Upper.setVisibility(View.GONE);
                holder.relativeLayout_Bottom.setVisibility(View.VISIBLE);
            }
        }

        // pop spinner names
        String[] names = new String[]{"Tom", "Ben", "Gil", "Adam", "Moshe", "Adi", "Michael", "Yasmin", "Jessica", "Caroline", "Avi", "Yael"};
        final ArrayAdapter<String> spNamesAdapter = new ArrayAdapter<String>
                (view.getContext(), android.R.layout.simple_spinner_dropdown_item, names);
        spNamesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        holder.spNames.setAdapter(spNamesAdapter);

        holder.spNames.setSelection(getItem(holder.index).getNameIndex());
        holder.spNames.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //holder.spNames.setTag(position);
                getItem(holder.index).setNameIndex(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        holder.btn_set.setTag(i);
        holder.btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getItem(holder.index).setName(holder.spNames.getSelectedItem().toString());
                int position = (Integer) v.getTag();
                holder.tvChosen.setText("Chosen: " + getItem(position).getName());
                holder.relativeLayout_Upper.setVisibility(View.GONE);
                holder.relativeLayout_Bottom.setVisibility(View.VISIBLE);
                getItem(holder.index).setIsNameOnTop(false);
            }
        });

        // remove
        holder.btn_remove.setTag(i);
        holder.btn_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (Integer) v.getTag();
                namesID.remove(position);
                notifyDataSetChanged();
                isDeleted = true;
            }
        });

        // back
        holder.btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
                holder.relativeLayout_Bottom.setVisibility(View.GONE);
                getItem(holder.index).setIsNameOnTop(true);
            }
        });

        return view;
    }
}

activity_main.xml:仅包含 ListView

upper_view.xml

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

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spNames" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SET"
        android:id="@+id/btn_set" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="REMOVE"
        android:id="@+id/btn_remove" />
</LinearLayout>

bottom_view.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Chosen:"
        android:id="@+id/tv_chosen"
        android:layout_marginRight="20dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BACK"
        android:id="@+id/btn_back" />
</LinearLayout>

listview_row.xml

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

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lvRow_upper_layout">

        <include
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            layout="@layout/upper_view"
            android:id="@+id/includeRow_register"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="0dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="0dp" />
    </RelativeLayout>

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lvRow_bottom_layout"
        android:visibility="gone">

        <include
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            layout="@layout/bottom_view"
            android:id="@+id/includeRow_showData"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="0dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="0dp" />
    </RelativeLayout>
</LinearLayout>

最佳答案

在您的 ListviewAdapter 中,更改:

    // back
    holder.btn_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
            holder.relativeLayout_Bottom.setVisibility(View.GONE);
            getItem(holder.index).setIsNameOnTop(true);
        }
    });

到:

    // back
    holder.btn_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
            holder.relativeLayout_Bottom.setVisibility(View.GONE);
            getItem(holder.index).setIsNameOnTop(true);
            notifyDataSetChanged();
            // OR you can use this
            // holder.spNames.requestLayout();
        }
    });

关于android - 布局可见性更改+特定流程后微调器值消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295378/

相关文章:

listview - 图像的无限 ListView 卡住应用程序或在布局期间抛出异常 [包括小应用程序]

android - 如何知道 Spinner 是否显示或不再显示

java - 微调器文本不显示默认值,也不显示所选值

java - Textview 根据微调器更改,并通过单击按钮编辑文本

java - Android Studio 无法从网站获取数据

Android Seekbar float 值

c++ - 使用 LVM_SORTITEMSEX 将所有选定项目移动到特定项目下方

android - 使用 gradle 为 Android 配置 Log4j

java - 应用 Math.random() 调用数组

Android:将新项目添加到列表后,ListView 中的 NullPointerException onItemClick