ListView严重加载了第一项

时间:2019-02-18 19:56:39

标签: java android listview adapter

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>

0 个答案:

没有答案