gpt4 book ai didi

java - 如何在没有初始动画的情况下更新 ListView 内的 SpeedView?

转载 作者:行者123 更新时间:2023-12-01 17:50:24 26 4
gpt4 key购买 nike

所以我有一个带有三个速度 View 仪表的 fragment 。 Picture of fragment它们曾经直接位于 fragment 中并且可以正常工作,但是在我需要添加 TextView 之后,我无法让所有内容都适合屏幕,因此我决定创建一个自定义布局并使用 ListView 来显示它们。

过去更新速度表非常容易,一切都运行良好。

将所有内容添加到 ListView 后,我认为更新所有内容的唯一方法是在适配器上使用notifyDataSetChanged(),但是当我这样做时,仪表指示器会捕捉到0并上升到所需的数字那里。我尝试过使用支架来解决这个问题,但没有成功。

这是有关如何创建适配器的 fragment 代码

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
OEEsegmentList = (ListView) view.findViewById(R.id.OEEplusLV);

qualitySegment = new OEEListSegment("Quality", OEEqualityValueString, "Bad parts: "+OEEqualityBadPartsValueString, "Total parts: "+OEEqualityTotalPartsValueString);
availabilitySegment = new OEEListSegment("Availability", OEEavailabilityValueString, "Downtime: "+OEEavailabilityDowntimeValueString, "Next time: "+OEEavailabilityNextTimeValueString);
performanceSegment = new OEEListSegment("Performance", OEEperformanceValueString, "Act time: "+OEEperformanceActTimeValueString, "Takt time: "+OEEperformanceTaktTimeValueString);

OEEsegmentArrayList = new ArrayList<>();
OEEsegmentArrayList.add(qualitySegment);
OEEsegmentArrayList.add(availabilitySegment);
OEEsegmentArrayList.add(performanceSegment);

OEEadapter = new OEEListAdapter(getActivity(), R.layout.oee_list_layout, OEEsegmentArrayList);
OEEsegmentList.setAdapter(OEEadapter);

}

这是更新函数之一,它们基本上都是相同的代码,但名称不同。当我收到有关特定标记被更改的消息时,我会在主要 Activity 中调用它们。

public void updateOEEplusAvailability (String availability) {
OEEavailabilityValueString=availability;

availabilitySegment.setSegmentValueDial(OEEavailabilityValueString);

OEEadapter.notifyDataSetChanged();
}

这是我的整个列表适配器

public class OEEListAdapter extends ArrayAdapter<OEEListSegment> {
private Context mContext;
int mResource;
public ArrayList<OEEListSegment> mSegments ;

public OEEListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<OEEListSegment> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
mSegments=objects;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
holder = new ViewHolder();

String name = getItem(position).getSegmentName();
String valueDial = getItem(position).getSegmentValueDial();
String valueTV1 = getItem(position).getSegmentValueTV1();
String valueTV2 = getItem(position).getSegmentValueTV2();

final OEEListSegment Segment = new OEEListSegment(name, valueDial, valueTV1, valueTV2);

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

TextView textViewName = (TextView) convertView.findViewById(R.id.textViewSegmentName);

holder.speedView = (SpeedView) convertView.findViewById(R.id.segmentSpeedView);
holder.textViewFirst = (TextView) convertView.findViewById(R.id.textViewSegmentFirst);
holder.textViewSecond=(TextView) convertView.findViewById(R.id.textViewSegmentSecond);
convertView.setTag(holder);
holder.textViewFirst.setText(mSegments.get(position).getSegmentValueTV1());
holder.textViewSecond.setText(mSegments.get(position).getSegmentValueTV2());
holder.speedView.speedTo(Float.parseFloat(mSegments.get(position).getSegmentValueDial()));


holder.speedView.clearSections();
holder.speedView.addSections(
new Section(.3f, Color.RED)
, new Section(.7f, Color.YELLOW)
, new Section(1f, Color.GREEN));


textViewName.setText(name);
holder.textViewFirst.setText(valueTV1);
holder.textViewSecond.setText(valueTV2);
holder.speedView.speedTo(Float.parseFloat(valueDial));

return convertView;
}

@Override
public int getViewTypeCount() {
return getCount();
}

@Override
public int getItemViewType(int position) {

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

public Object getItemCustom(int position) {
return mSegments.get(position);
}

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

private class ViewHolder {

protected SpeedView speedView ;
private TextView textViewFirst ;
private TextView textViewSecond;

}
}

这是列表段代码

public class OEEListSegment implements  Parcelable {
private String name;
private String valueDial;
private String valueTV1;
private String valueTV2;

public OEEListSegment(String name, String valueDial, String valueTV1, String valueTV2) {
this.name=name;
this.valueDial=valueDial;
this.valueTV1=valueTV1;
this.valueTV2=valueTV2;
}

public String getSegmentName() {
return name;
}
public void setSegmentName(String name) {
this.name=name;
}

public String getSegmentValueDial() {
return valueDial;
}
public void setSegmentValueDial(String valueDial) {
this.valueDial=valueDial;
}

public String getSegmentValueTV1() {
return valueTV1;
}
public void setSegmentValueTV1(String valueTV1) {
this.valueTV1=valueTV1;
}

public String getSegmentValueTV2() {
return valueTV2;
}
public void setSegmentValueTV2(String valueTV2) {
this.valueTV2=valueTV2;
}


@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {this.name, this.valueDial, this.valueTV1, this.valueTV2});
}

private void readFromParcel(Parcel in) {
name = in.readString();
valueDial= in.readString();
valueTV1= in.readString();
valueTV2= in.readString();
}

public OEEListSegment(Parcel in){
String[] data = new String[4];

in.readStringArray(data);
// the order needs to be the same as in writeToParcel() method
this.name = data[0];
this.valueDial= data[1];
this.valueTV1= data[2];
this.valueTV2= data[3];
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public OEEListSegment createFromParcel(Parcel in) {
return new OEEListSegment(in);
}

public OEEListSegment[] newArray(int size) {
return new OEEListSegment[size];
}
};
}

任何解决方案将不胜感激。如何阻止初始动画(0 到所需的数字)在速度 View 中发生,或者如何更新 ListView 以使动画不会发生。

或者最好完全省略 ListView ,同时仍然获得所需的布局(只有 3 个段)。

最佳答案

我无法在列表段内完成我需要做的事情,但由于我知道我希望显示该段的确切次数,所以我决定改变我的方法。

我在主布局中添加了一个 scoll View ,然后在其中包含了 3x 段布局。然后我需要像这样通过 ID 访问所有组件来访问段(布局)

performanceLayout = view.findViewById(R.id.performanceSegment);

像这样访问我的段内的元素

speedViewPerformance= performanceLayout.findViewById(R.id.segmentSpeedView);

然后我可以用这样的功能更新 speeviews

 public void updateOEEplusPerformance (String performance) {
OEEperformanceValueString=performance;
speedViewPerformance.speedTo(Float.parseFloat(OEEperformanceValueString));
}

这比我之前尝试做的要简单得多,因为我不需要适配器或新类,所以我绝对不需要更改速度 View 动画,它的工作原理就像我需要的一样!

关于java - 如何在没有初始动画的情况下更新 ListView 内的 SpeedView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60806267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com