当ListView行项目失去焦点或单击另一行时,返回原始状态

时间:2015-07-03 14:47:57

标签: android android-layout listview android-activity android-listview

我有这个活动显示五行输入[一,二,三......五]: enter image description here

单击每一行时,布局将更改为显示EditText和Save图标:

enter image description here

我的问题是,如果我点击多行,我可以更改许多布局,如下所示: enter image description here

我希望每次单击ListView中的不同行如果之前单击了某行(使用EditText + Save Icon显示),则将该行更改回其原始状态(TextView + Trash和Arrow grey)图标)即可。

这是每行的自定义布局:

<?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="60dp"
    android:orientation="horizontal"
    android:layout_margin="10dp"
    android:background="@drawable/listbackground"
    android:weightSum="10">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7.5"
        android:id="@+id/rowTextView"
        android:typeface="serif"
        android:gravity="center_vertical"
        android:textSize="15sp"
        android:textColor="#70000000"
        android:layout_marginLeft="30dp" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/swipeImageView"
        android:src="@mipmap/swipe2"
        android:alpha="0.5"
        android:padding="5dp" />


    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:visibility="gone"
        android:id="@+id/updateEditText"
        android:selectAllOnFocus="true"
        android:textColorHighlight="#1f020260"
        android:textColor="#70000000"
        android:typeface="serif"
        android:gravity="center_vertical"
        android:textSize="15sp"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:visibility="gone"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:clickable="true"
        android:onClick="onClick"
        android:id="@+id/saveImageView"
        android:src="@mipmap/save"
        android:alpha="0.5"
        android:padding="11dp" />

</LinearLayout>

这是我正在使用的onListItemClick()方法(没有重构,所以有一些重复的代码对不起):

@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
    super.onListItemClick(l, v, position, id);

    final TextView rowTextView = (TextView) v.findViewById(R.id.rowTextView);
    final ImageView swipeImageView = (ImageView) v.findViewById(R.id.swipeImageView);
    final EditText updateEditText = (EditText) v.findViewById(R.id.updateEditText);
    final ImageView saveImageView = (ImageView) v.findViewById(R.id.saveImageView);

    rowTextView.setVisibility(View.GONE);
    swipeImageView.setVisibility(View.GONE);
    updateEditText.setVisibility(View.VISIBLE);
    saveImageView.setVisibility(View.VISIBLE);

    updateEditText.setText(rowTextView.getText());
    updateEditText.setFocusable(true);
    updateEditText.requestFocus();

    inputMethodManager.showSoftInput(updateEditText, InputMethodManager.SHOW_IMPLICIT);

    saveImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String update = updateEditText.getText().toString();

            Product product = (Product) getListAdapter().getItem(position);

            productsDAO.updateProduct(product, update);

            rowTextView.setVisibility(View.VISIBLE);
            swipeImageView.setVisibility(View.VISIBLE);
            updateEditText.setVisibility(View.GONE);
            saveImageView.setVisibility(View.GONE);

            adapterUpdater.getItem(position).setName(update);
            adapterUpdater.notifyDataSetChanged();
        }
    });
}

谢谢!

1 个答案:

答案 0 :(得分:0)

你能不能这样做:

rowTextView.setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
           //hide the edit stuff
        }
    }
});