我是Android的新手,我正在制作一个响铃调整下载器应用程序。在这个应用程序中,我有一个回收视图与振铃曲调列表。当用户点击下载按钮时,我想在每个单元格中显示进度条。我知道RecyclerView会回收任何旧视图,因此需要绕过至少进度条。
实现这一目标的理想方法是什么?
我的适配器类
public class TrackListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
//to reference the Activity
private final Activity context;
private final RecyclerViewClicks recyclerViewClicks;
private final ArrayList<Track> trackList;
public TrackListAdapter(Activity context, ArrayList<Track> trackList, RecyclerViewClicks recyclerViewClicks){
this.context = context;
this.recyclerViewClicks = recyclerViewClicks;
this.trackList = trackList;
}
public ArrayList<Track> getData() {
return this.trackList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.tracklist_row,null);
TrackListView rowView = new TrackListView(view);
return rowView;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Track currentTrack = this.trackList.get(position);
TrackListView trackListView = (TrackListView) holder;
trackListView.title.setText(currentTrack.getName());
trackListView.track.setText(currentTrack.getArtist());
Picasso.get().load(currentTrack.getArtUrl())
.resize(100,100)
.into(trackListView.imageView);
// if(currentTrack.isDownloaded())
// {
// trackListView.download.setVisibility(View.INVISIBLE);
// }
// else
// {
// trackListView.download.setVisibility(View.INVISIBLE);
// }
}
@Override
public int getItemCount() {
return trackList.size();
}
class TrackListView extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener
{
TextView title, track;
ImageView imageView;
Button download;
ProgressBar progressBar;
TextView progressText;
// DownloadProgress downloadProgress;
public ProgressBar getProgressBar() {
return progressBar;
}
public TrackListView(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.name);
track = itemView.findViewById(R.id.track);
imageView = itemView.findViewById(R.id.imageView);
download = itemView.findViewById(R.id.download);
progressBar = itemView.findViewById(R.id.progressBar);
progressText = itemView.findViewById(R.id.downloadProgress);
itemView.setOnClickListener(this);
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recyclerViewClicks.onButtonClicked(getAdapterPosition(),view,(Button) view);
}
});
}
@Override
public void onClick(View view) {
}
@Override
public boolean onLongClick(View view) {
return false;
}
}
}
主要活动
public class MainActivity extends AppCompatActivity {
ArrayList<Track> tracks = new ArrayList<Track>();
RecyclerView trackListView;
TrackListAdapter trackListAdapter;
private File direct;
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AndroidNetworking.initialize(getApplicationContext());
trackListAdapter = new TrackListAdapter(this, tracks, new RecyclerViewClicks() {
@Override
public void onItemSelected(int position, View v) {
}
@Override
public void onButtonClicked(int position, View cell, Button button) {
Track selectedTrack = tracks.get(position);
downloadMusic(selectedTrack,cell);
}
@Override
public void onLongClicked(int position) {
}
});
//
trackListView = (RecyclerView) findViewById(R.id.trackListView);
trackListView.setLayoutManager(new LinearLayoutManager(this));
trackListView.setAdapter(trackListAdapter);
......
......
我正在使用AndroidNetworking libs从服务器下载数据。 请帮忙
答案 0 :(得分:0)
这是我为RecyclerView构建的内容,其中的单元格包含内容布局,该内容布局在单击时会加载mp3音频广播。 第一种方法是最佳的,因为它可以将加载信息临时存储在内存中,而第二种方法更适合于存储需要长时间存储的信息,而不是UI数据。
在 RecyclerView 中选择内容后, ViewModel 会存储唯一项目ID的Array或HashMap。加载 RecyclerView 单元格的内容后,它将Array或HashMap中内容ID的视图状态更改为已加载。布局绑定到 ViewModel 并相应显示 ProgressBar 。
Content 模型包含一个属性,以便在Room SQLite数据库中更新加载状态。布局已经是绑定到 Content 模型的数据,并且将根据模型的属性显示加载栏。