2017-01-09 8 views
1

私のアプリでは、ニュースフィードのようなことがあります。このリストビューには、このような行レイアウトを持つカスタムアダプタがあります。リストビューの行の更新

enter image description here

あなたが見ることができるように、ユーザーのヒットは続きを読むたびだから、私は、黒い画面が透明有効にします。だから私はこのコードを

public class CustomAdapter extends ArrayAdapter<GEvent> { 

private Context mContext; 

private Uri eventImage; 

final Holder holder = new Holder(); 

private View rowView; 

public void setEventImage(Uri eventImage) { 
    this.eventImage = eventImage; 
} 

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

    this.mContext = context; 
} 

@NonNull 
@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); 


    rowView = customView; 


    holder.title = (TextView) customView.findViewById(R.id.geventTitle); 
    holder.profileName = (TextView) customView.findViewById(R.id.geventProfileName); 
    holder.readMore = (TextView) customView.findViewById(R.id.geventReadMoreButton); 
    holder.eventPic = (ImageView) customView.findViewById(R.id.geventEventPhoto); 

    View.OnClickListener view =new View.OnClickListener() { 

     View updatedView; 

     @Override 
     public void onClick(View v) { 
      if(gEvent.isWantsToReadMore()){      //wants to read less 
       Log.i("Entered the condition","Read less"); 
       customView.findViewById(R.id.geventEventPhoto).setAlpha(0.5f); 
      } 
      else{          //wants to read more 
       Log.i("Entered the condition","Read more"); 
       customView.findViewById(R.id.geventEventPhoto).setAlpha(1f); 
      } 
      gEvent.setWantsToReadMore(!gEvent.isWantsToReadMore()); 

      updatedView = customView; 

     } 



    }; 

    holder.readMore.setOnClickListener(view); 

    holder.title.setText(gEvent.getTitle()); 
    holder.profileName.setText(gEvent.getProfileUsername()); 

    if(!gEvent.isHasSetPhoto()) 
    { 
     Log.i("Entered the condition","Check"); 

     fetchPhotoURI(gEvent); 

     if(eventImage!=null){ 


      Picasso.with(mContext) 
        .load(eventImage) 
        .into(holder.eventPic); 

      gEvent.setHasSetPhoto(true); 

      Log.i("Set COndition","True"); 
     } 

    } 

    return customView; 

} 

public void fetchPhotoURI(GEvent gEvent){ 



    FirebaseStorage fbStorage = FirebaseStorage.getInstance(); 

    StorageReference storageReference = fbStorage.getReferenceFromUrl("gs://XXXXX"); 

    final StorageReference pathRef= storageReference.child("gEvent/"+gEvent.getUuid()+"/photo.jpg"); 



    try{ 

     pathRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { 
      @Override 
      public void onSuccess(Uri uri) { 
       setEventImage(uri); 
      } 
     }); 

    }catch (Exception e){ 
     e.printStackTrace(); 
    } 


} 

public class Holder{ 

    TextView profileName; 
    TextView title; 
    TextView description; 
    TextView details; 
    TextView readMore; 

    CircleImageView profilePic; 
    ImageView eventPic; 


} 

public void readMoreFunction(View view,boolean wantsToReadMore){ 


    notifyDataSetChanged(); 

} 

}

を試してみました今、私が直面する問題は、ユーザーがより多くのボタンを読んでタップするたび、ビューは変更ありませんが、私はその特定の行を更新することができませんということですリストビュー、つまりアルファ値の変更はリストビューに反映されていないので、アダプターによるgetView()で返されていたViewを更新していなかったためだと思っていましたが、事件である。

私はnotifyDataSetChanged()を使ってみましたが、それは私にとってはうまくいかないようです。 変更する必要はありますか?前もって感謝します。

+0

あなたの全体のアダプタのコードを投稿します。 – Noorul

答えて

1

私たちはすでにthis chatに解決策を見つけ出しましたが、他の人が参考にして回答しています。

ここでの問題は、アルファ値を変更した後にビューが更新されないということです。

ので、ビューを更新するために、我々はinvalidate()メソッドを使用することができ、最終的なコードは次のようなものになります。

.... 
    @Override 
    public void onClick(View v) { 
     if(gEvent.isWantsToReadMore()){      //wants to read less 
      Log.i("Entered the condition","Read less"); 
      customView.findViewById(R.id.geventEventPhoto).setAlpha(0.5f); 
      // To make the view redraw itself, use invalidate() 
      customView.findViewById(R.id.geventEventPhoto).invalidate(); 

     } 
     else{          //wants to read more 
      Log.i("Entered the condition","Read more"); 
      customView.findViewById(R.id.geventEventPhoto).setAlpha(1f); 
      // To make the view redraw itself, use invalidate() 
      customView.findViewById(R.id.geventEventPhoto).invalidate(); 
     } 
    ... 
関連する問題