Android:滚动后ListView无法正常工作

时间:2016-01-11 11:27:46

标签: android listview scroll

您好我正在尝试在列表视图中显示文字,图片。要将内容加载到我的列表视图中,我有三种情况。

1。有时候我只从json那里得到文字。所以,在这种情况下,我必须隐藏ImageView

2。有些情况下,我只从json获取图片网址。所以,在这种情况下,我必须隐藏TextView

3. 在其他情况下,我从json获取图片网址和文字,因此,在这种情况下,我不必隐藏任何内容

通过遵循这些场景,我正在首次正确加载所有内容,但是当我从上到下滚动,从下到上滚动时。所有图像视图都填充了图像(但该特定位置没有图像URL)

我的适配器代码是

package com.app.listviewitemid.adapter;

import java.util.ArrayList;

import com.app.listviewitemid.R;
import com.app.listviewitemid.model.Items;
import com.bumptech.glide.Glide;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemsAdapter extends ArrayAdapter<Items>{

    ArrayList<Items> itemsArrayList;
    int Resource; 
    LayoutInflater inflater;
    Context context;   
    ViewHolder holder;

    Button btnCheck;
    Items globPosition;

    public ItemsAdapter(Context context, int resource, ArrayList<Items> objects){
        super(context, resource, objects);
        this.context = context;
        this.Resource = resource;
        this.itemsArrayList = objects;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    }

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

        View v = convertView;
        if(v == null){
            v = inflater.inflate(Resource, null);
            holder = new ViewHolder();

            holder.tvPostID = (TextView) v.findViewById(R.id.tv_post_id);
            holder.tvPostText = (TextView) v.findViewById(R.id.tv_post_text);
            holder.ivPostImage = (ImageView) v.findViewById(R.id.iv_post_img);

            btnCheck = (Button) v.findViewById(R.id.btn_check);

            v.setTag(holder);

        }else{

            holder = (ViewHolder) v.getTag();
        }

        globPosition = itemsArrayList.get(position);

        holder.tvPostID.setText(itemsArrayList.get(position).getPostID());


        if(globPosition.getPostText().equals(null) || globPosition.getPostText().equals("")){

        }else {
            holder.tvPostText.setText(globPosition.getPostText());
        }



        if(globPosition.getPostImage().equals(null) || globPosition.getPostImage().equals("")){

        }else {
            Glide.with(context).load(globPosition.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(holder.ivPostImage);
        }




        btnCheck.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Log.e("PostID", globPosition.getPostID());

            }
        });

        return v;
    }

    static class ViewHolder{

        TextView tvPostID;
        TextView tvPostText;
        ImageView ivPostImage;

    }

}

适配器布局

<?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="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_post_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/post_id"
        android:textColor="#000000"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/tv_post_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="12sp" />

    <ImageView
        android:id="@+id/iv_post_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/empty" />

    <Button
        android:id="@+id/btn_check"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="#000000"
        android:text="@string/check"
        android:textColor="#FFFFFF" />

</LinearLayout>

如何摆脱这个?

2 个答案:

答案 0 :(得分:1)

if(globPosition.getPostText().equals(null) || globPosition.getPostText().equals("")){
            holder.tvPostText.setVisibility(View.GONE);
        }else {
            holder.ivPostImage.setVisibility(View.GONE);
            holder.tvPostText.setVisibility(View.VISIBLE);
            holder.tvPostText.setText(globPosition.getPostText());
        }



        if(globPosition.getPostImage().equals(null) || globPosition.getPostImage().equals("")){
            holder.ivPostImage.setVisibility(View.GONE);
        }else {
            holder.ivPostImage.setVisibility(View.GONE);
            holder.tvPostText.setVisibility(View.VISIBLE);
            Glide.with(context).load(globPosition.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(holder.ivPostImage);
        }

答案 1 :(得分:0)

我从ankit aggarwal的答案得到了这个想法(他的代码不能正常工作,但他给了我基本的想法)。还有一件事,以前我没有得到正确的位置,所以我稍微更改了适配器代码

<强>代码

 package com.app.listviewitemid.adapter;

import java.util.List;

import com.app.listviewitemid.R;
import com.app.listviewitemid.model.Items;
import com.bumptech.glide.Glide;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemsAdapter extends BaseAdapter{

    List<Items> itemsArrayList;
    int Resource; 
    LayoutInflater inflater;
    Context context;

    Button btnCheck;

    public ItemsAdapter(Context context, List<Items> items) {
        this.context = context;
        this.itemsArrayList = items;
    }

    @Override
    public int getCount() {
        return itemsArrayList.size();
    }

    @Override
    public Object getItem(int location) {
        return itemsArrayList.get(location);
    }

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

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

        if (inflater == null)
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.adapter_layout, null);

            TextView tvPostID = (TextView) convertView.findViewById(R.id.tv_post_id);
            TextView tvPostText = (TextView) convertView.findViewById(R.id.tv_post_text);
            ImageView ivPostImage = (ImageView) convertView.findViewById(R.id.iv_post_img);

            btnCheck = (Button) convertView.findViewById(R.id.btn_check);

            final Items items = itemsArrayList.get(position);


            tvPostID.setText(items.getPostID());


            if(items.getPostText().equals(null) || items.getPostText().equals("")){
                tvPostText.setVisibility(View.GONE);
            }else {
                //ivPostImage.setVisibility(View.GONE);

                if(items.getPostImage().equals(null) || items.getPostImage().equals("")){
                    ivPostImage.setVisibility(View.GONE);
                }else {
                    ivPostImage.setVisibility(View.VISIBLE);
                    tvPostText.setVisibility(View.GONE);
                    Glide.with(context).load(items.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(ivPostImage);
                }

                tvPostText.setVisibility(View.VISIBLE);
                tvPostText.setText(items.getPostText());
            }



            if(items.getPostImage().equals(null) || items.getPostImage().equals("")){
                ivPostImage.setVisibility(View.GONE);
            }else {
                ivPostImage.setVisibility(View.VISIBLE);
                //tvPostText.setVisibility(View.GONE);

                if(items.getPostText().equals(null) || items.getPostText().equals("")){
                    tvPostText.setVisibility(View.GONE);
                }else {
                     tvPostText.setVisibility(View.VISIBLE);
                        tvPostText.setText(items.getPostText());
                }
                Glide.with(context).load(items.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(ivPostImage);
            }


            btnCheck.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    Log.e("PostID", items.getPostID());

                }
            });

        return convertView;
    }

}

感谢ankit aggarwal