gpt4 book ai didi

android - android中方向变化进度条的更新

转载 作者:行者123 更新时间:2023-11-29 00:27:55 26 4
gpt4 key购买 nike

我有一个 fragment ,我正在 fragment 上设置 ListView 。

代码如下:

public class AttachmentsFragment extends Fragment {

ListView lstView = null;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//retains fragment instance across Activity re-creation
setRetainInstance(true);
objects = new ArrayList<AttachModel>();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = null;

view = inflater.inflate(R.layout.tab_attachment, container, false);

lstView = (ListView) view.findViewById(R.id.listViewAttachment);
adapter = new AttachAdapter(getActivity(), 0, 0, objects);
lstView.setAdapter(adapter);

return view;
}

在适配器中我有一个进度条和一个显示进度条进度的textview。启动和停止进度条的按钮

public class AttachAdapter extends ArrayAdapter<AttachModel> implements OnClickListener {

Context context;
ArrayList<AttachModel> objects = new ArrayList<AttachModel>();

AttachModel info;

//Activity act;

AttachModel model;

public AttachmentsAdapter(Context context, int resource,
int textViewResourceId, ArrayList<AttachmentsModel> objects) {
super(context, textViewResourceId, textViewResourceId, objects);

this.context = context;
this.objects = objects;

}

// no. of attachments available
@Override
public int getCount() {

return objects.size();
}

@Override
public AttachmentsModel getItem(int position) {

return objects.get(position);
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public View getView(int position, View convertView, final ViewGroup parent) {

View row = convertView;
ViewHolder holder = null;

if(null == row) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.attachment_list_item, parent, false);


//textview for showing progress
holder.textViewProgress = (TextView) row.findViewById(R.id.txtViewPg);

//progress bar to show the progress
holder.progressBar = (ProgressBar) row.findViewById(R.id.pgBar);
holder.progressBar.setTag(position);

holder.textViewProgress.setVisibility(TextView.VISIBLE);
holder.img_view_fileIcon.setVisibility(ImageView.VISIBLE);

holder.progressBar.setVisibility(ProgressBar.VISIBLE);

//to start stop the progress bar
holder.button = (Button)row.findViewById(R.id.img_btn_download);
holder.button.setVisibility(Button.VISIBLE);

holder.button.setTag(position);
holder.button.setOnClickListener(this);

row.setTag(holder);


} else {
holder = (ViewHolder) row.getTag();

}


return row;
}

private class ViewHolder {
TextView textViewProgress;
ProgressBar progressBar;
Button button;

boolean downloadFlag = false;

}

@Override
public void onClick(View v) {

View vParent=(View) v.getParent();

ViewHolder tempHolder = null;

tempHolder=(ViewHolder) vParent.getTag();

//toggle button like functionality
if(!tempHolder.downloadFlag) {

tempHolder.downloadFlag = true;
tempHolder.progressBarStatus = 0;

async = new AsyncTaskAttachments(tempHolder, objects.get(Integer.parseInt(v.getTag().toString())).getFilePath());

tempHolder.async.execute();

objects.get((Integer)tempHolder.progressBar.getTag()).setAsyncTask(tempHolder.async);

}else {

tempHolder.downloadFlag = false;
tempHolder.progressBar.setProgress(0);

tempHolder.textViewProgress.setVisibility(TextView.GONE);
tempHolder.textViewProgress.setText("");
tempHolder.progressBarStatus = 0;

tempHolder.async.cancel(true);



}

}

public class AsyncTaskAttachments extends AsyncTask<Void, Void, Void> {

private ViewHolder holder;

public AsyncTaskAttachments(ViewHolder holder, String filePath) {
this.holder = holder;
this.filePath = filePath;

}

public void attach(Activity act) {
this.act = act;
}

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub


holder.progressBarStatus = 0;

for(int i=0; i<=10; i++) {
try {
Thread.sleep(1000);
holder.progressBarStatus = i*10;


if(isCancelled()) {
break;
}


publishProgress();
}catch (Exception e) {
// TODO: handle exception
}

}

return null;
}

/* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

}

/* (non-Javadoc)
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();

holder.progressBarStatus = 0;

holder.textViewProgress.setText("" + holder.progressBarStatus + "%");

holder.progressBar.setProgress(holder.progressBarStatus);


}

/* (non-Javadoc)
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);

// TODO Auto-generated method stub
holder.progressBar.setProgress(holder.progressBarStatus);
holder.textViewProgress.setText("" + holder.progressBarStatus + "%");



}


}

}

注意:这只是为了展示我的做法。这只是一瞥。我需要知道我是否在正确的轨道上。

在方向改变时,进度条无法保留并从方向改变的地方开始进度。

提前致谢

最佳答案

当方向改变时,您的 Activity (和包含的 fragment )被重新创建。因此,您再次调用 asyncTask 并将进度设置为零:

holder.progressBarStatus = 0;

解决方案:在fragment的onSaveInstanceState()中保存进度,在onCreateView()中恢复并让asyncTask使用这个值进行初始进度设置。

关于android - android中方向变化进度条的更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009408/

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