改变android中textView的可见性

时间:2017-01-13 08:59:21

标签: android

我使用带有2个textViews的列表视图。 1 textView的可见性在XML中设置为GONE。单击列表项时,我希望其textview是可见的。当它再次被点击时,我希望它是GONE。更进一步在listView中单击任何其他项时,只有它的textView应该是可见的。我尝试使用下面的代码,但是当再次选择相同的列表项时,Textview不会变为GONE。

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//to change the visibility of Prevoiusly selected textview to GONE
            if (pwdView != null)
                pwdView.setVisibility(View.GONE);
            pwdView = (TextView) view.findViewById(R.id.saved_pwd);
            if (pwdView.getVisibility()==View.VISIBLE)
                pwdView.setVisibility(View.GONE);
            else
                pwdView.setVisibility(View.VISIBLE);
        }
    });

适配器代码:

public class PwdCursorAdaptor extends CursorAdapter {
public PwdCursorAdaptor(Context context, Cursor c) {
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_items, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView saved_id = (TextView) view.findViewById(R.id.saved_id);
    TextView saved_password_data=(TextView)view.findViewById(R.id.saved_pwd);
    int pass_index=cursor.getColumnIndex(PwdContract.PasswordEntry.COL_PASSSWORD);
    int id_index = cursor.getColumnIndex(PwdContract.PasswordEntry.COL_LOGIN_ID);
    saved_id.setText(cursor.getString(id_index));
    saved_password_data.setText(cursor.getString(pass_index));
}

}

4 个答案:

答案 0 :(得分:0)

这是因为点击监听器是asynchrounus调用。因此,您必须通知适配器,即数据项中发生了更改。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (pwdView != null)
            pwdView.setVisibility(View.GONE);
        pwdView = (TextView) view.findViewById(R.id.saved_pwd);
        if (pwdView.getVisibility()==View.VISIBLE)
            pwdView.setVisibility(View.GONE);
        else
            pwdView.setVisibility(View.VISIBLE);


         yourAdapter.notifyDataSetChanged();
    }
});`

答案 1 :(得分:0)

   lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
   {
       TextView  pwdView = (TextView) view.findViewById(R.id.saved_pwd);
        if (pwdView !=null)
       {
            pwdView.setVisibility(pwdView.getVisibility()==View.VISIBLE ? View.GONE : View.VISIBLE);
        }
         notifyDatasetChanged();
    }
   });`

答案 2 :(得分:0)

就像在适配器类中一样

@Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView saved_id = (TextView) view.findViewById(R.id.saved_id);
        final TextView saved_password_data=(TextView)view.findViewById(R.id.saved_pwd);
        int pass_index=cursor.getColumnIndex(PwdContract.PasswordEntry.COL_PASSSWORD);
        int id_index = cursor.getColumnIndex(PwdContract.PasswordEntry.COL_LOGIN_ID);
        saved_id.setText(cursor.getString(id_index));
        saved_password_data.setText(cursor.getString(pass_index));

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (saved_password_data.getVisibility()==View.VISIBLE) {
                    saved_password_data.setVisibility(View.GONE);
                }else {
                    saved_password_data.setVisibility(View.VISIBLE);
                }
                notifyDatasetChanged();
            }
        });
    }

如果你想第一次看到saved_pa​​ssword_data应该可见或消失,而不是根据需要将可见性放​​在XML文件中

答案 3 :(得分:0)

在onItemClick中添加它,这里lBooking.size()是一个列表大小。

 @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            for(int j=0;j<lBooking.size();j++){
                if(i == j){
                    view.findViewById(R.id.saved_pwd).setVisibility(View.VISIBLE);
                }else {
                    lv.getChildAt(j).findViewById(R.id.saved_pwd).setVisibility(View.GONE);
                }
            }
        }