Android - setVisibility not working in ProgressBar

懵懂的女人 提交于 2019-12-10 11:58:59

问题


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);
    }
}

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 !!!
            }
        }
    }
}

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;
}

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 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 !


回答1:


This is ListView's parent's

↳   android.view.ViewGroup
       ↳    android.widget.AdapterView<android.widget.ListAdapter>
           ↳    android.widget.AbsListView

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 Views 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




回答2:


Have you tried putting the file retrieval code into a worker thread like this?

// set progressBar .VISIBLE first 

// then...    
new Thread(new Runnable() {
            public void run() {

                // file retrieval code

            }
        }).start();

Here is how it solved my own problem & here are the docs.



来源:https://stackoverflow.com/questions/30008728/android-setvisibility-not-working-in-progressbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!