ListView正在奇怪地更新

时间:2017-01-07 15:59:35

标签: android listview

以下是我在listview中自定义行的布局。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:id="@+id/layoutTop"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Well Hello Motherfucker"
        android:id="@+id/geventProfileName"
        android:textSize="20sp"
        android:paddingLeft="50dp"/>

</RelativeLayout>
<FrameLayout
    android:id="@+id/layoutBottom"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:alpha="1"
    android:layout_below="@+id/layoutTop"
    android:layout_alignParentStart="true">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/no_picture"
        android:id="@+id/geventEventPhoto"
        android:layout_gravity="top|left"
        android:alpha="1"/>

    <TextView
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:text="asfda"
        android:layout_gravity="center|left"
        android:textSize="35sp"
        android:textColor="#FFF"
        android:textAllCaps="true"
        android:id="@+id/geventTitle"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Read More"
        android:textColor="@color/black"
        android:id="@+id/geventReadMoreButton"
        android:background="@drawable/readtab"
        android:paddingTop="10dp"
        android:paddingLeft="20dp"

        android:layout_gravity="bottom|right"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        android:id="@+id/geventDescription"
        android:text=""
        android:textColor="@color/black"
        android:layout_gravity="top|left"/>

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/geventDetails"
        android:text=""
        android:textSize="20sp"
        android:textColor="#FFF"
        android:layout_gravity="bottom|left"
        />



</FrameLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/layoutBottom"
    android:orientation="horizontal"
    android:background="#9E9E9E"
    android:padding="5dp"
    android:layout_marginTop="5dp"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100"
        android:id="@+id/numberOfLikesGEvent"
        android:textColor="@color/black"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/numberOfLikesGEvent"
        android:id="@+id/likeTextViewGEvent"
        android:layout_marginLeft="10dp"
        android:textColor="@color/black"
        android:text="Like"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/dislikeTextViewGEvent"
        android:layout_marginRight="10dp"
        android:id="@+id/numberOfDislikesGEvent"
        android:textColor="@color/black"
        android:text="200"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dislikeTextViewGEvent"

        android:text="Dislike"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="0dp"
        android:textColor="@color/black"/>

</RelativeLayout>


<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/photo"

    app:civ_border_width="2dp"
    app:civ_border_color="#FFF"/>

这是我的CustomAdapter

public class CustomAdapter extends ArrayAdapter<GEvent> {


boolean hasLiked;
boolean hasDisliked;
FirebaseUser fbUser;
ArrayList<String> uids;
ArrayList<String> dUids;
GEvent currentEvent;

private TextView likeTextView;
private TextView numberOfLikes;
private TextView dislikeTextView;
private TextView numberOfDislikes;
private TextView readMoreButton;
private TextView eventDetails;
private TextView titleGevent;
private ImageView eventPhoto;
private TextView profileName;
private TextView descriptionGevent;

private String description;
private String titleForGEvent;
private String detailsGEvent;
private boolean hasLoaded=true;

private de.hdodenhof.circleimageview.CircleImageView circleImageView;

private Context mContext;

private boolean wantsToReadMore=false;

String profilePhotoLink;


PopupWindow pw;

public CustomAdapter(Context context, GEvent[] resource) {
    super(context, R.layout.custom_row , resource);

    this.mContext = context;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(getContext());

    final  View customView = inflater.inflate(R.layout.example,parent,false);

    final GEvent gEvent = getItem(position);

    currentEvent = gEvent;

    firebaseAuth = FirebaseAuth.getInstance();

    uid = firebaseAuth.getCurrentUser().getUid();



     titleGevent = (TextView) customView.findViewById(R.id.geventTitle);
     descriptionGevent = (TextView) customView.findViewById(R.id.geventDescription);
     profileName = (TextView) customView.findViewById(R.id.geventProfileName);
     likeTextView = (TextView) customView.findViewById(R.id.likeTextViewGEvent);
     numberOfLikes = (TextView) customView.findViewById(R.id.numberOfLikesGEvent);
     numberOfDislikes =(TextView) customView.findViewById(R.id.numberOfDislikesGEvent);
     dislikeTextView = (TextView) customView.findViewById(R.id.dislikeTextViewGEvent);
    readMoreButton = (TextView) customView.findViewById(R.id.geventReadMoreButton);
    eventPhoto = (ImageView) customView.findViewById(R.id.geventEventPhoto);
    eventDetails = (TextView)customView.findViewById(R.id.geventDetails);
    circleImageView = (CircleImageView) customView.findViewById(R.id.profile_image);

    description = gEvent.getDescription();
    detailsGEvent = gEvent.getgEventDate()+"\n"+gEvent.getgEventTime()+"\n"+gEvent.getVenue1()+"\n"+gEvent.getVenue2()+"\n"+gEvent.getContactEmail()+"\n"+gEvent.getContactPhoneNumber();
    titleForGEvent = gEvent.getTitle();


    if(gEvent.isHasPhoto()){

        hasLoaded= false;
        readMoreButton.setText("Loading");

    }

    readMoreButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            if(hasLoaded) {
                Log.i("CA","Entered hasLoaded Function");
                readMoreFunction();
            }
            else
                Toast.makeText(mContext,"Loading....",Toast.LENGTH_LONG).show();
        }
    });





    titleGevent.setText(gEvent.getTitle());




    String uuid = gEvent.getUuid();
    Boolean hasPhoto = gEvent.isHasPhoto();



    profileName.setText(gEvent.getProfileUsername());


    if(hasPhoto) {


        final long ONE_MEGABYTE = 1024 * 1024 * 5;

        pathRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
            @Override
            public void onSuccess(byte[] bytes) {

                Bitmap picturel = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                ImageView imageView = (ImageView) customView.findViewById(R.id.geventEventPhoto);

                Drawable d = new BitmapDrawable(mContext.getResources(),picturel);

                imageView.setBackground(d);

                Log.i("CA","Entered hasRetrieved Photo and update hasLoaded");

                readMoreButton.setText("Read More");

                hasLoaded=true;

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle any errors
                Log.i("Error","Couldn't Download");
            }
        });

    }




    return customView;
}

public void readMoreFunction(){

    Log.i("CA","Entered readMoreFunction Function");

    if(wantsToReadMore){            //wants to read less now

        Log.i("CA","Entered Read Less Function");

        eventPhoto.setAlpha(1f);
        descriptionGevent.setText("");

        eventDetails.setText("");

        readMoreButton.setText("Read More");

        titleGevent.setText(titleForGEvent);

        wantsToReadMore=false;

    }
    else{                           //Wants to read more
        Log.i("CA","Entered Read More Function");
        eventPhoto.setAlpha(0.5f);
        descriptionGevent.setText(description);

          eventDetails.setText(detailsGEvent);

        titleGevent.setText("");

        readMoreButton.setText("Read Less");

        wantsToReadMore=true;

    }

所以我的问题是每当我调用这个CustomAdapter时,只有一个列表项在那里它完美地工作,但是每当有多个列表项时,适配器开始表现奇怪。例如,每当有2个项目时,如果我按下第一个项目的“阅读更多”按钮,它将触发第二个项目,即使在日志中它显示它正在工作的数据是第一个项目本身。

如果有两个以上的项目,则会观察到上述模式,即每当我点击给定项目的“阅读更多”按钮时,它就会触发列表视图中的下一个项目。有时它甚至在加载图像或“阅读更多”按钮甚至没有响应后仍然停留在“正在加载”。enter image description here In this the Button isn't working 过去两天我一直在敲头,寻找虫子。希望你们能帮助我。谢谢你。

PS: - “加载更多”按钮不合适,请原谅。

1 个答案:

答案 0 :(得分:1)

GEvent内部创建一个boolean字段

class GEvent {
    private boolean readMoreClicked;

    public boolean isReadMoreClicked(){
        return readMoreClicked;
    }

    public void setReadMoreClicked(boolean readMoreClicked){
        this.readMoreClicked = readMoreClicked;
    }
}

getView内,

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

    ....

    if(gEvent.isReadMoreClicked()) {
        Log.i("CA","Entered More Less Function");
        ...
    } else {
        Log.i("CA","Entered Less More Function");
        ...
    }

    readMoreButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(hasLoaded) {
                Log.i("CA","Entered hasLoaded Function");

                // user has clicked Read More. Save the state
                gEvent.setReadMoreClicked(!gEvent.isReadMoreClicked());
                notifyDataSetChanged();

            } else {
                Toast.makeText(mContext,"Loading....",Toast.LENGTH_LONG).show();
            }
        }
    });

    ...
}

可能需要进行一些编辑。但我希望你明白这个主意。 :)