Listview行更新 [英] Listview Row Updation
问题描述
因此,在我的应用程序中,我希望有一个新闻提要类的东西.在此列表视图中,我有一个自定义适配器,其行布局是这样的.
So In my app , I want there to be a news feed kind of thing . In this listview, I have an Custom Adapter which has a row layout like this.
如您所见,每当用户点击阅读更多"时,我都希望黑屏变成透明的.所以我尝试了这段代码
So as you can see , whenever the user hits Read More, I want the black screen to turn transparent . So I tried this code
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();
}
}
现在我面临的问题是,每当用户单击阅读更多"按钮时,视图确实会更改,但是我无法更新列表视图中的特定行,即Alpha值的更改不会反映在listview,我首先认为是因为我没有更新由Adapter在getView()中返回的View,但现在似乎并非如此.
Now the problem that I face is that whenever , the user taps the read more button , the view does change but I am unable to update that specific row in the listview , i.e , the change in Alpha value doesn't reflect in the listview and I first thought it was because I wasn't updating the View which was getting returned in the getView() by the Adapter but now that doesn't seem to be the case.
我尝试使用notifyDataSetChanged(),但对我来说似乎不起作用. 我应该改变什么?预先感谢.
I have tried using notifyDataSetChanged() but that doesn't seem to work for me. What should I change ? Thanks in advance.
推荐答案
So, though we already worked out the solution here in this chat, I am answering here for others to have a reference.
这里的问题是,更改Alpha值后,视图不会自动更新.
The problem here is that the view is not updating itself after the alpha values are being changed.
因此,要更新视图,我们可以使用invalidate()
方法,最终代码将如下所示:
So, to update a view, we can use the invalidate()
method, and the final code will look something like this :
....
@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();
}
...
这篇关于Listview行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!