所以在我的应用程序中,我希望有一个新闻提要的东西。在这个列表视图中,我有一个自定义适配器,它具有这样的行布局。
正如您所看到的,每当用户点击“阅读更多”时,我希望黑屏变为透明。所以我尝试了这段代码
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();
}
}
现在我遇到的问题是,每当用户点击read more按钮时,视图确实会发生变化,但我无法更新listview中的特定行,即Alpha值的变化不会发生变化。在listview中反映出来,我首先想到的是因为我没有更新由适配器在getView()中返回的View,但现在似乎并非如此。
我尝试过使用notifyDataSetChanged(),但这对我来说似乎不起作用。 我应该改变什么?提前谢谢。
答案 0 :(得分:1)
所以,虽然我们已经在this chat中找到了解决方案,但我在这里回答其他人提供参考。
这里的问题是在更改alpha值后视图不会自动更新。
因此,要更新视图,我们可以使用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();
}
...