【问题标题】:Why does an Spinner disappear and reappear again on touch?为什么 Spinner 在触摸时会消失并再次出现?
【发布时间】:2016-06-27 15:02:45
【问题描述】:

在 Android 应用程序中使用微调器。我设置了一个适配器并用数据库中的数据填充它。

问题在于,每次我触摸微调器外部时(比如在EditText 上)- 微调器的项目布局会消失一秒钟,然后才会再次出现。
似乎微调器每次都会刷新。

我在 Activity 的 onCreate() 方法中只填充一次微调器:

 List<Deal> listDeals = mDealDao.getLastActiveDeals();
    if (listDeals != null) {
        mAdapter = new SpinnerDealsAdapter(this, listDeals);
        mSpinnerDeal.setAdapter(mAdapter);
        mSpinnerDeal.setOnItemSelectedListener(this);
    }

这是适配器:

public class SpinnerDealsAdapter extends BaseAdapter {

    public static final String TAG = "SpinnerDealsAdapter";

    private List<Deal> mItems;
    private LayoutInflater mInflater;

    public SpinnerDealsAdapter(Context context, List<Deal> listCompanies) {
        this.setItems(listCompanies);
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return (getItems() != null && !getItems().isEmpty()) ? getItems().size() : 0 ;
    }

    @Override
    public Deal getItem(int position) {
        return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position) : null ;
    }

    @Override
    public long getItemId(int position) {
        return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position).getId() : position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if(v == null) {
            v = mInflater.inflate(R.layout.spinner_item_deal, parent, false);
            holder = new ViewHolder();
            holder.txtDealNumber = (TextView) v.findViewById(R.id.tv_deal_number);
            holder.txtDealName = (TextView) v.findViewById(R.id.txt_client_name);
            holder.txtProduct = (TextView) v.findViewById(R.id.txt_product);
            v.setTag(holder);
        }
        else {
            holder = (ViewHolder) v.getTag();
        }

        // fill row data
        Deal currentItem = getItem(position);
        if(currentItem != null) {
            holder.txtDealNumber.setText(String.valueOf(currentItem.getId()));
            holder.txtDealName.setText(currentItem.getClientName());
            holder.txtProduct.setText(currentItem.getProduct());
        }

        return v;
    }

    public List<Deal> getItems() {
        return mItems;
    }

    public void setItems(List<Deal> mItems) {
        this.mItems = mItems;
    }

    class ViewHolder {
        TextView txtDealNumber;
        TextView txtDealName;
        TextView txtProduct;
    }
}

更新: 以下是我从数据库中获取数据的方式:

public List<Deal> getLastActiveDeals() {
        List<Deal> listDeals = new ArrayList<Deal>();

        Cursor cursor = mDatabase.query(DBHelper.TABLE_DEALS, mAllColumns,
                DBHelper.COLUMN_DEAL_IS_COMPLETED_FLAG + " = ?",
                new String[] { "0" }, null, null, DBHelper.COLUMN_DEAL_ID + " DESC");
        if (cursor != null) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                Deal deal = cursorToDeal(cursor);
                listDeals.add(deal);
                cursor.moveToNext();
            }

            // closing the cursor
            cursor.close();
        }
        return listDeals;

活动布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aandroid="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin">


        <android.support.design.widget.TextInputLayout
            android:id="@+id/txt_contactor_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:textColorHint="@color/text_hint_color">


            <AutoCompleteTextView
                android:id="@+id/txt_contactor"
                style="@style/EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="6dp"
                android:hint="@string/contractor"
                android:inputType="text" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/txt_expense_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt_contactor_layout"
            android:textColorHint="@color/text_hint_color">


            <AutoCompleteTextView
                android:id="@+id/txt_expense"
                style="@style/EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="6dp"
                android:hint="@string/expense"
                android:inputType="numberDecimal" />


        </android.support.design.widget.TextInputLayout>


        <TextView
            android:id="@+id/tv_active_deals_spinner_description"
            style="@style/DescriptionTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt_expense_layout"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="@dimen/descriptions_vertical_margin"
            android:text="@string/active_deals_spinner_description" />

        <LinearLayout
            android:id="@+id/ll_spinner_deals"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_active_deals_spinner_description"
            android:background="@drawable/bg_white_accent_stroke_round">


        <Spinner
            android:id="@+id/spinner_deals"
            style="@style/EditTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:animateLayoutChanges="true"
            android:prompt="@string/select_a_deal"
            android:spinnerMode="dropdown" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_add_quick_expense"
            style="@style/ButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="@dimen/button_height"
            android:layout_below="@+id/ll_spinner_deals"
            android:layout_marginTop="12dp"
            android:gravity="center"
            android:onClick="onClick"
            android:paddingBottom="2dp"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:paddingTop="2dp"
            android:text="@string/add_new_expense" />

        <TextView
            android:id="@+id/tv_quick_add_expense_no_active_deals"
            style="@style/DescriptionTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt_expense_layout"
            android:gravity="center"
            android:text="@string/no_active_deals"
            android:textColor="@color/accent"
            android:visibility="gone" />

        <Button
            android:id="@+id/btn_add_quick_expense_add_deal"
            style="@style/ButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="@dimen/button_height"
            android:layout_below="@+id/tv_quick_add_expense_no_active_deals"
            android:layout_marginTop="15dp"
            android:gravity="center"
            android:onClick="onClick"
            android:paddingBottom="2dp"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:paddingTop="2dp"
            android:text="@string/add_new_deal"
            android:visibility="gone" />
    </RelativeLayout>

</ScrollView>

怎么了?

【问题讨论】:

  • 列表 listDeals = mDealDao.getLastActiveDeals();你能告诉我你的 getLastActiveDeals() 吗?

标签: android android-spinner


【解决方案1】:

您每次都在创建一个新的适配器。

代替:

 List<Deal> listDeals = mDealDao.getLastActiveDeals();
    if (listDeals != null) {
        mAdapter = new SpinnerDealsAdapter(this, listDeals);
        mSpinnerDeal.setAdapter(mAdapter);
        mSpinnerDeal.setOnItemSelectedListener(this);
    }

这样做:

//Only do this once in onCreate or something
List<Deal> listDeals = mDealDao.getLastActiveDeals();

SpinnerDealsAdapter mAdapter;
//mAdapter should be a field in the activity

if (mAdapter == null) {
    mAdapter = new SpinnerDealsAdapter(this, listDeals);
    mSpinnerDeal.setAdapter(mAdapter);
    mSpinnerDeal.setOnItemSelectedListener(this);
}
else 
    mSpinnerDeal.setAdapter(mAdapter);

【讨论】:

  • 好像没有效果。最初我只在 onCreate 中设置了一次适配器。
  • ...如果我在微调器适配器的 getView 内设置断点并进行调试 - 每次选择不同的 EditText 时我都会到达那里。所以就像微调器每次都会刷新。我不明白为什么
  • 你应该每次都到那里......我正在再次调查它,我认为你的回收也有问题。让我看看。
  • 哦,尝试创建 ViewHolder。代替 ViewHolder 持有者;做 ViewHolder holder = new ViewHolder();
  • rogcg.com/blog/2013/03/12/recycling-views-listview 我正在将您的代码与此链接进行比较,除了 ViewHolder 之外,它似乎完全相同。
【解决方案2】:

终于找到了解决办法。
问题出在活动的布局中。
我刚刚从微调器中删除了android:animateLayoutChanges="true" 并且滞后消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    相关资源
    最近更新 更多