Android的 - setVisibility的进度不工作 [英] Android - setVisibility not working in ProgressBar
问题描述
我有一个包含progressBars一个ListView,我要显示和隐藏一个进度条线程内,这里是我的code:
I have a ListView which contains progressBars, I want to show and hide the progressBar inside a Thread, here is my code :
public void onClick(View v) {
if (v.getId() == R.id.ib_add_file) {
Intent intent = new Intent(this, FilePickerActivity.class);
startActivityForResult(intent, REQUEST_PICK_FILE);
}
}
当我选择一个文件,它会被添加到ListView并开始上传
When I pick a file it will be added to the ListView and start uploading :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_PICK_FILE:
if (data.hasExtra(FilePickerActivity.EXTRA_FILE_PATH)) {
File file = new File(data.getStringExtra(FilePickerActivity.EXTRA_FILE_PATH));
Fichier fichier = new Fichier();
fichier.setFile(file);
ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) chat_list.getAdapter();
adapter.add(fichier);
int pos = adapter.getCount() - 1;
MessageHolder holder = (MessageHolder) getViewByPosition(pos, chat_list).getTag();
Log.i("Fichier", holder.fichier.getText().toString());
//the log is showing the right file name
holder.progress.setVisibility(View.VISIBLE);
//the progressBar isn't showing !
Log.i("ProgressBar", (holder.progress.getVisibility() == View.VISIBLE)? "Visible":"invisible");
//the log is showing Visible !!!
}
}
}
}
我使用这个功能,我从这个有<一个href=\"http://stackoverflow.com/questions/24811536/android-listview-get-item-view-by-position?answertab=active#tab-top\">answer从ListView中获取添加的行处理进度:
I'm using this function which i got from this answer to get the added row from the ListView to handle the ProgressBar:
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MessageHolder msgHolder = null;
Message msg = data.get(position);
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
msgHolder = new MessageHolder();
msgHolder.fichier = (TextView) row.findViewById(R.id.tv_fichier);
msgHolder.iconFichier = (ImageView) row.findViewById(R.id.iv_icon_fichier);
msgHolder.progress = (ProgressBar) row.findViewById(R.id.pb_fichier);
row.setTag(msgHolder);
//msgHolder.handler = new ProgressBarHandler(msgHolder.progress);
} else {
msgHolder = (MessageHolder) row.getTag();
}
Fichier fich = (Fichier) msg;
msgHolder.fichier.setText(fich.getNomFichier());
msgHolder.fichier.setPaintFlags(msgHolder.fichier.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
msgHolder.iconFichier.setImageResource(fichier.getIconFichier());
return row;
}
private static class MessageHolder {
ImageView iconFichier;
TextView fichier;
ProgressBar progress;
ProgressBarHandler handler;
}
这是我为ListView的行XML:
And this is my xml for the the rows of the listview :
<ImageView
android:id="@+id/iv_icon_fichier"
style="@style/AvatarImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/descThumbnail"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_icon_fichier"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_fichier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22sp"
android:text="Fichier" />
<ProgressBar
android:id="@+id/pb_fichier"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:minWidth="200dp"
android:progress="0"
android:visibility="gone"/>
</LinearLayout>
为什么我的进度没有显示?
Why my ProgressBar is not showing ?
code ++编辑:我意识到,我的问题并不相关者的处理程序,我试过直接显示进度条从我的onActivityResult梅索德,但它没有显示
Code Edited : I realized that my problem wasn't relater to the Handler, I tried showing the ProgressBar directly from my onActivityResult methode but it's not showing !
推荐答案
这是 ListView控件的
母公司
↳ android.view.ViewGroup
↳ android.widget.AdapterView<android.widget.ListAdapter>
↳ android.widget.AbsListView
看着我甚至可以说我会叫 mylistView.removeViewAt(3);
,这应该是因为的ViewGroup $ C工作$ C>但它不会因为
适配器视图
..
为什么我说这是这样的,我是不是天才,这个职位有44 upvotes,这意味着它的合法的,但我不认为这会去,如果你想挑出更新的方式查看
由适配器视图
因为 getView
方法将覆盖它的变化,即使支持发生的变化不会neccessarily您preffered发生查看
的查看
在位置0是什么在查看休息
开始从图纸。例如,如果你有一个初步的查看
一个可见的的TextView
和一个无形的的EditText
,那么所有的查看
旨意是这样,除非您更改一个查看
在 getView
返回(我没有使用过这种方法/方式前,我与支持的假设的说),所以不是把另一个变量在你的数据源,比如布尔needsupdate
,并在 getView
检查 needsupdate
并修改公开程度
那里。这将是合法的方式,我的智商 - (也许我可能是有点在这里下车)。但试试看
looking at that i might even say i will call mylistView.removeViewAt(3);
and that should work because of ViewGroup
but it doesn't because of AdapterView
..
Why i am saying this is that, i am no genius, that post has 44 upvotes, which means its legit, but i do not think that will be the way to go if you want to single out Update a View
backed by an AdapterView
because the getView
method will override it your changes,even though the changes occur it doesn't neccessarily occur on your preffered View
the View
at position 0 is what the rest of the View
start drawing from. eg, if you have an initial View
with a visible TextView
and an invisible EditText
then all the View
s will be like that unless you change one View
before the getView
returns(i have not used that method/way before and i am speaking with backed assumptions), so instead put another variable in your datasource like boolean needsupdate
and check for needsupdate
in your getView
and change the Visibilty
there. that will be the legit way to my intelligence -(maybe i might be a little off here). but give it a try
这篇关于Android的 - setVisibility的进度不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!