ListView плохо загружает первый элемент - PullRequest
0 голосов
/ 18 февраля 2019

ListViev, как это странно загружает первый элемент, раньше все было хорошо, но теперь что-то странное.

Элемент содержит два невидимых значения, url и position.Если вы используете gettext (), то это дает значение, но если вы щелкнете по нему, то ничего не произойдет.Кроме того, если вы измените положение для него, например, переместите его на второй, то это будет работать на его внимание.Но тот, который будет на первой позиции, будет с проблемами.

enter image description here

Вот адаптер:

public class ListViewAdapter extends BaseAdapter {

private Context context;
private String[] img, name, url;
private boolean isCountry = false;

ListViewAdapter(Context context, String[] img, String[] name, String[] url, boolean isCountry){
    this.context = context;
    this.img = img;
    this.name = name;
    this.url = url;
    this.isCountry = isCountry;
}

@Override
public int getCount() {
    return url.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@SuppressLint({"InflateParams", "SetTextI18n"})
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    OneItem oneItem;
    if(convertView == null){
        oneItem = new OneItem();
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(isCountry){
            convertView = layoutInflater.inflate(ThemeWork.getCountryItem(), null);
        } else {
            convertView = layoutInflater.inflate(ThemeWork.getChannelItem(), null);
        }

        oneItem.name = convertView.findViewById(R.id.itemName);
        oneItem.url = convertView.findViewById(R.id.itemUrl);
        oneItem.pos = convertView.findViewById(R.id.itemPosition);
        oneItem.img = convertView.findViewById(R.id.itemImage);
        oneItem.pref = convertView.findViewById(R.id.itemPref);

        convertView.setTag(oneItem);

    } else {
        oneItem = (OneItem) convertView.getTag();
    }

    oneItem.name.setText(name[position]);
    oneItem.url.setText(url[position]);
    oneItem.pos.setText(Integer.toString(position));

    oneItem.pref.setImageResource(ThemeWork.getImagePref());

    if(!menu.isListPref) {
        for (String n : menu.izbranoe) {
            if (n.equals(menu.currentCountryName + "_" + name[position])) {
                oneItem.pref.setImageResource(R.mipmap.ic_favorites);
            }
        }
    }

    if(!img[position].equals("")){
        Picasso.get().load(img[position]).into(oneItem.img);
    } else {
        oneItem.img.setImageResource(R.mipmap.ic_launcher);
    }

    return convertView;
}

class OneItem {
    TextView  name, url, pos;
    ImageView img, pref;
}

Вот макет:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="UselessParent">

    <ListView
        android:id="@+id/listView"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="InefficientWeight" />

    <LinearLayout
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <ImageButton
            android:onClick="LoadCountryItem"
            android:src="@mipmap/ic_country"
            android:scaleType="centerCrop"
            android:padding="7dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:background="@android:color/transparent"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:layout_width="50dp"
            android:layout_height="50dp"
            tools:ignore="ContentDescription" />

        <ImageButton
            android:onClick="ShowPref"
            android:src="@mipmap/ic_favorites"
            android:scaleType="centerCrop"
            android:padding="7dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:background="@android:color/transparent"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:layout_width="50dp"
            android:layout_height="50dp"
            tools:ignore="ContentDescription" />

        <ImageButton
            android:onClick="ClickSearch"
            android:src="@mipmap/ic_search"
            android:scaleType="centerCrop"
            android:padding="7dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:background="@android:color/transparent"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:layout_width="50dp"
            android:layout_height="50dp"
            tools:ignore="ContentDescription" />
    </LinearLayout>
</LinearLayout>

Вот активность:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".menu">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-*">
    </com.google.android.gms.ads.AdView>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <ImageView
            android:id="@+id/progressImage"
            android:layout_centerInParent="true"
            android:src="@mipmap/ic_loading"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:visibility="gone"
            tools:ignore="ContentDescription" />

    </RelativeLayout>
</LinearLayout>

...