Android - Нет прокрутки на пользовательских элементов Spinner - PullRequest
0 голосов
/ 11 сентября 2018

Я создаю собственный спиннер в своем приложении. Элементы отображаются отлично, но проблема в том, что элементы списка не прокручиваются.

Пожалуйста, обратитесь к изображению ниже. На изображении ниже я использую множество стран, но не могу прокрутить список других стран.

Custom dropdown image link

Ниже приведен мой полный код счетчика:

main_activity.xml

        <android.support.percent.PercentRelativeLayout
            android:id="@+id/layoutSpinner"
            android:layout_centerHorizontal="true"
            android:background="@drawable/bg_spinner_countrycode"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            app:layout_widthPercent="25%">

            <Spinner
                android:id="@+id/spCountryCode"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </android.support.percent.PercentRelativeLayout>

MainActivity.java:

Spinner spCountryCode = (Spinner) findViewById(R.id.spCountryCode);

CountryCodeAdapter countryCodeAdapter = new CountryCodeAdapter(LoginScreenActivity.this, modelList);
spCountryCode.setAdapter(countryCodeAdapter);

CountryCodeAdapter.java:

public class CountryCodeAdapter extends ArrayAdapter<CountryCodeModel> {

    Activity activity;
    List<CountryCodeModel> itemList;
    LayoutInflater inflater;


    public CountryCodeAdapter(Activity activity, List<CountryCodeModel> itemList) {
        super(activity, R.layout.country_code_spinner_layout, itemList);
        this.activity = activity;
        this.itemList = itemList;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = inflater.inflate(R.layout.country_code_spinner_layout, parent, false);

        ImageView imgFlag = (ImageView) itemView.findViewById(R.id.imgFlag);
        TextView lblCountryName = (TextView) itemView.findViewById(R.id.lblCountryName);
        TextView lblCountryCode = (TextView) itemView.findViewById(R.id.lblCountryCode);

        Picasso.with(activity).load(itemList.get(position).getStrCountryFlagUrl()).into(imgFlag);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgFlag.getLayoutParams();
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        imgFlag.setLayoutParams(lp);

        //lblCountryName.setText(itemList.get(position).getStrCountryName());
        lblCountryName.setVisibility(View.GONE);
        //lblCountryCode.setText("(" + itemList.get(position).getStrCountryCode() + ")");
        lblCountryCode.setVisibility(View.GONE);


        return itemView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View itemView = inflater.inflate(R.layout.country_code_spinner_layout, parent, false);

        ImageView imgFlag = (ImageView) itemView.findViewById(R.id.imgFlag);
        TextView lblCountryName = (TextView) itemView.findViewById(R.id.lblCountryName);
        TextView lblCountryCode = (TextView) itemView.findViewById(R.id.lblCountryCode);

        Picasso.with(activity).load(itemList.get(position).getStrCountryFlagUrl()).into(imgFlag);
        lblCountryName.setText(itemList.get(position).getStrCountryName());
        lblCountryCode.setText(itemList.get(position).getStrCountryCode());

        return itemView;
    }
}

Кроме того, я также пытался, countryCodeAdapter.setDropDownViewResource(R.layout.country_code_spinner_layout);, прямо перед spCountryCode.setAdapter(countryCodeAdapter);, но все еще безуспешно.

Кроме того, если вы хотите увидеть мой собственный макет счетчика, вот оно.

country_code_spinner_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/countryCodeSpinnerDropDownHeight">

    <ImageView
        android:id="@+id/imgFlag"
        android:layout_width="@dimen/countryCodeSpinnerImageSize"
        android:layout_height="@dimen/countryCodeSpinnerImageSize"
        android:layout_marginLeft="@dimen/countryCodeSpinnerMarginLeft"
        android:layout_centerVertical="true"/>

    <com.base.silpre.Font_TextViewOpenSans_SemiBold
        android:id="@+id/lblCountryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imgFlag"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/countryCodeSpinnerMarginLeft"
        android:textSize="@dimen/countryCodeSpinnerTextsSize"
        android:textColor="@android:color/black"/>

    <com.base.silpre.Font_TextViewOpenSans_Italic
        android:id="@+id/lblCountryCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/lblCountryName"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/countryCodeSpinnerMarginLeft"
        android:textSize="@dimen/countryCodeSpinnerTextsSize"
        android:textColor="@android:color/black"/>

</RelativeLayout>

Пожалуйста, вернитесь назад, если у кого-то есть решение для прокрутки пользовательского счетчика.

1 Ответ

0 голосов
/ 11 сентября 2018

Попробуйте wrap_content для высоты

   `< Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>`
...