在使用手风琴样式布局时,我能够做到这一点,其中每个折叠项也包含列表的一个子集,因为这种布局不适用于 RecyclerView Viewholder 模式。我使用 Concurrent 作为 Asynctask 的替代品,然后在 doInBackground 部分所有 Glide 获取图像和 addView() 调用使用 new Handler(Looper.getMainLooper()).post(() -> {//Your Code}); 包装,因为 Glide 和添加视图需要它在 UI 线程上运行。每个布局的添加将在屏幕上一一显示,但好在 Choreographer 不再跳帧。
这是我的代码的样子
public class GenerateLayoutAsync extends BaseConCurrentTask<Object> {
private final WeakReference<FragmentActivity> activityReference;
private final LinearLayoutCompat linearLayoutCompat;
private final List<GroupMatch> groupMatchList;
public GenerateLayoutAsync(FragmentActivity context, LinearLayoutCompat linearLayoutCompat, List<GroupMatch> groupMatchList) {
this.activityReference = new WeakReference<>(context);
this.linearLayoutCompat = linearLayoutCompat;
this.groupMatchList = groupMatchList;
}
@Override
public void setUiForLoading() {
}
@Override
public Object call() {
for (int i = 0; i < groupMatchList.size(); i++) {
GroupMatch groupMatch = groupMatchList.get(i);
AppCompatTextView title;
LinearLayoutCompat container;
View itemView = LayoutInflater.from(this.activityReference.get()).inflate(R.layout.group_card, linearLayoutCompat, false);
title = itemView.findViewById(R.id.groupTitle);
container = itemView.findViewById(R.id.populateView);
title.setText(groupMatch.getTitle());
container.setVisibility(View.GONE);
title.setOnClickListener(v -> {
if (container.getVisibility() == View.VISIBLE)
container.setVisibility(View.GONE);
else
container.setVisibility(View.VISIBLE);
});
for (int j = 0; j < groupMatch.getModelList().size(); j++) {
MatchModel matchModel = groupMatch.getModelList().get(j);
AppCompatTextView home, away, middleText, topText, bottomText, betBtn;
AppCompatImageView shareBtn, homeFlag, awayFlag;
View view = LayoutInflater.from(this.activityReference.get()).inflate(R.layout.match_card, (ViewGroup) itemView, false);
home = view.findViewById(R.id.homeTeam);
away = view.findViewById(R.id.awayTeam);
topText = view.findViewById(R.id.topTextV);
middleText = view.findViewById(R.id.middleTextV);
bottomText = view.findViewById(R.id.bottomTextV);
betBtn = view.findViewById(R.id.betNowBtn);
shareBtn = view.findViewById(R.id.shareBtn);
homeFlag = view.findViewById(R.id.homeFlag);
awayFlag = view.findViewById(R.id.awayFlag);
if (CampaignModel.isIsTarget() && CampaignModel.isFetchAds()) {
betBtn.setVisibility(View.VISIBLE);
betBtn.setOnClickListener(v -> this.activityReference.get().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(CampaignModel.getREDIRECT()))));
}
else
betBtn.setVisibility(View.GONE);
home.setText(matchModel.getHome());
away.setText(matchModel.getAway());
home.setSelected(true);
away.setSelected(true);
LocalDateTime localDateTime;
if (matchModel.getHomeScore().isEmpty() && matchModel.getAwayScore().isEmpty()){
betBtn.setAlpha(1f);
betBtn.setEnabled(true);
localDateTime = LocalDateTime.parse(matchModel.getStartDate(), DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm"));
String date = localDateTime.format(DateTimeFormatter.ofPattern("MM/dd/yy"));
String time = localDateTime.format(DateTimeFormatter.ofPattern("HH:mm a"));
topText.setText(time);
bottomText.setText(date);
middleText.setText(null);
}
else{
betBtn.setAlpha(0.3f);
betBtn.setEnabled(false);
localDateTime = LocalDateTime.parse(matchModel.getEndDate(), DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm"));
String date = localDateTime.format(DateTimeFormatter.ofPattern("MM/dd/yy"));
topText.setText(matchModel.getHomeScore());
bottomText.setText(matchModel.getAwayScore());
middleText.setText(date);
}
new Handler(Looper.getMainLooper()).post(() -> {
Glide.with(this.activityReference.get())
.asDrawable()
.load(matchModel.getHomeFlag())
.error(R.drawable.ic_flag)
.into(homeFlag);
Glide.with(this.activityReference.get())
.load(matchModel.getAwayFlag())
.error(R.drawable.ic_flag)
.into(awayFlag);
});
shareBtn.setOnClickListener(v -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this.activityReference.get(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this.activityReference.get(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
ShareToSocial.saveAndShare(matchModel.getHome() + " vs " + matchModel.getAway(), itemView);
else
this.activityReference.get().requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
else
ShareToSocial.saveAndShare(matchModel.getHome() + " vs " + matchModel.getAway(), itemView);
});
new Handler(Looper.getMainLooper()).post(() -> container.addView(view));
}
new Handler(Looper.getMainLooper()).post(() -> linearLayoutCompat.addView(itemView));
}
return null;
}
@Override
public void setDataAfterLoading(Object result) {
}
}
如您所见,我在第二个 for 循环中为每个标题布局添加视图,然后在第一个 for 循环结束时将每个标题布局添加到主布局。
如果你还不熟悉 Java 的 Concurrent util,你也可以用同样的方法使用旧的 AsyncTask。