0

我正在使用openaphid提供的其中一个类来进行翻转视图。我遇到的问题(正如我刚刚开始发现的那样)是,随着新视图的创建和旧视图被废弃(内部getView()),对我的变量的引用被更新,因此具有不同的 ID,这导致我无法更改它们。

本质上,我所拥有的是 2 TextViews。其中一个TextViews ( txtvwCharacter) 已在 中设置为可点击,XML而我想要做的是:当txtvwCharacter被点击/触摸时,另一个TextView( ) 在和(空字符串)txtvwComposition之间切换。"some text"""

下面是我的代码的简化版本,因为我只关注 getView() 方法:

public class TravelAdapter extends BaseAdapter 
{
    private LayoutInflater inflater;

    ArrayList<String> characterArray = new ArrayList<String>();
    ArrayList<String> compositionArray = new ArrayList<String>();

    TextView txtvwCharacter, txtvwComposition;

    boolean detailsOn = false;

    public TravelAdapter(Context context) 
    {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View layout = convertView;

        if (convertView == null) 
            layout = inflater.inflate(R.layout.complex1, null);

        txtvwCharacter = UI.<TextView>findViewById(layout, R.id.txtvw_character);
        txtvwComposition = UI.<TextView>findViewById(layout, R.id.txtvw_composition);

        txtvwCharacter.setText(characterArray.get(position));
        txtvwComposition.setText("");

        txtvwCharacter.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(detailsOn == false)
                {
                    txtvwComposition.setText(compositionArray.get(position));
                    detailsOn = true;
                }
                else
                {
                    txtvwComposition.setText("");
                    detailsOn = false;
                }
            }
        });

        return layout;
    }
}

目前,使用此代码,onClick确实已注册并已txtvwComposition设置,但显示的并不相同txtvwComposition,因此,从视觉上看,似乎什么也没发生。

我想在这里有某种持久性,当我单击时txtvwCharacter,它知道这是一个TextView对象,而不是在所有来回翻转之间实例化的新对象。(对不起,我不知道如何更好地表达。)

我看过thisthis,但我不明白它是做什么ViewHolder的,它如何知道“持有”什么,或者如何将它应用到我的特定代码中。

代码编辑:

public class TravelAdapter extends BaseAdapter 
{
    private LayoutInflater inflater;

    ArrayList<String> characterArray = new ArrayList<String>();
    ArrayList<String> compositionArray = new ArrayList<String>();

    TextView txtvwCharacter, txtvwComposition;

    boolean detailsOn = false;

    public TravelAdapter(Context context) 
    {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View layout = convertView;

        if (convertView == null) 
            layout = inflater.inflate(R.layout.complex1, null);

        txtvwCharacter = UI.<TextView>findViewById(layout, R.id.txtvw_character);
        txtvwComposition = UI.<TextView>findViewById(layout, R.id.txtvw_composition);

        if (detailsOn == false)
        {
            txtvwCharacter.setText(characterArray.get(position));
            txtvwComposition.setText("");
        }
        else
        {
            txtvwCharacter.setText(characterArray.get(position));
            txtvwComposition.setText(compositionArray.get(position));
        }

        txtvwCharacter.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                RelativeLayout root = (RelativeLayout) v.getParent();
                TextView txtvwComposition = (TextView) root.findViewById(R.id.txtvw_composition);

                if(detailsOn == false)
                {
                    txtvwComposition.setText(compositionArray.get(position));
                    detailsOn = true;
                }
                else
                {
                    txtvwComposition.setText("");
                    detailsOn = false;
                }
            }
        });

        return layout;
    }
}
4

0 回答 0