有没有办法在snackBar上显示进度对话框,同时从服务器获取AsyncTask中的分页数据。如果可能,那么如何做到这一点。请给出建议。
答案 0 :(得分:9)
对于新设计库:
如果你想在左侧显示progressBar:
Snackbar bar = Snackbar.make(root, R.string.data_updating, Snackbar.LENGTH_INDEFINITE);
ViewGroup contentLay = (ViewGroup) bar.getView().findViewById(android.support.design.R.id.snackbar_text).getParent();
ProgressBar item = new ProgressBar(context);
contentLay.addView(item,0);
bar.show();
或右侧:
Snackbar bar = Snackbar.make(root, R.string.data_updating, Snackbar.LENGTH_INDEFINITE);
ViewGroup contentLay = (ViewGroup) bar.getView().findViewById(android.support.design.R.id.snackbar_text).getParent();
ProgressBar item = new ProgressBar(context);
contentLay.addView(item);
bar.show();
答案 1 :(得分:8)
最简单,最好的方法是:
Snackbar bar = Snackbar.make(layout, "Somthing", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout snack_view = bar.getView();
snack_view.addView(new ProgressBar(context));
bar.show();
SnackbarLayout extends
LinearLayout,因此您可以将视图添加为子视图或以您希望的方式对其进行自定义。
例如,要更改SnackBar中TextView的样式,您可以这样做:
TextView tv = (TextView) bar.getView().findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
tv.setTextSize(0, dimen[1] / 40);
tv.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/font.otf"));
答案 2 :(得分:3)
我已经写了一个可以做到这一点的图书馆。它还包括队列系统。试一试https://github.com/tingyik90/snackprogressbar
答案 3 :(得分:1)
我认为您可以将自定义进度对话框设置为类似Snackbar,因为在加载时,snackbar不会阻止用户与GUI交互。
更新
试试这个:
创建anim res文件名为slide_in.xml并粘贴它:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="50%p" android:toYDelta="0%p"
android:duration="@android:integer/config_longAnimTime"
/>
创建另一个动画res文件名为slide_out.xml:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="0%p"
android:toYDelta="100%p" />
现在在样式res中放置进程对话框样式:
<style name="CustomDialog" parent="@android:style/Theme.Holo.Dialog">
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
<item name="android:background">#323232</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:alertDialogStyle">@style/customDialogStyle</item>
<item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>
<style name="customDialogStyle">
<item name="android:bottomBright">@android:color/transparent</item>
<item name="android:bottomDark">@android:color/transparent</item>
<item name="android:bottomMedium">@android:color/transparent</item>
<item name="android:centerBright">@android:color/transparent</item>
<item name="android:centerDark">@android:color/transparent</item>
<item name="android:centerMedium">@android:color/transparent</item>
<item name="android:fullBright">@android:color/transparent</item>
<item name="android:fullDark">@android:color/transparent</item>
<item name="android:topBright">@android:color/transparent</item>
<item name="android:topDark">@android:color/transparent</item>
将其放置为动画:
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">@anim/slide_in</item>
<item name="android:windowExitAnimation">@anim/slide_out</item>
</style>
现在添加方法:
public static ProgressDialog processSnackbar(Context context,String s){
ProgressDialog pSnackbar = new ProgressDialog(context, R.style.CustomDialog) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressBar progress = (ProgressBar) findViewById(android.R.id.progress);
LinearLayout bodyLayout = (LinearLayout) progress.getParent();
TextView messageView = (TextView) bodyLayout.getChildAt(1);
messageView.setPadding(20,0,0,0);
LinearLayout.LayoutParams llp =
(LinearLayout.LayoutParams) messageView.getLayoutParams();
llp.width = 0;
llp.weight = 1;
bodyLayout.removeAllViews();
bodyLayout.addView(messageView, llp);
bodyLayout.addView(progress);
}
};
pSnackbar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pSnackbar.setMessage(s);
pSnackbar.getWindow().setGravity(Gravity.BOTTOM);
pSnackbar.setCancelable(false);
return pSnackbar;
}
Method Credit返回@ mike-m reference Is it possible to alter progress dialog's message or spinner position?
我在我的应用程序中也使用它看看:
答案 4 :(得分:0)
fun showLoadingSnackbar(){
val snackbar: Snackbar = Snackbar.make(findViewById(R.id.rootlayoutid), "TEXT or RES", Snackbar.LENGTH_INDEFINITE)
val viewGroup = snackbar.view.findViewById<View>(com.google.android.material.R.id.snackbar_text).parent as ViewGroup
viewGroup.addView(ProgressBar(this))// Or Context if not in Activity
snackbar.show()
}
如果你想要它在左边,只需调用 addView(ProgressBar(this),0)
而不是在另一个答案中提到
编辑:如果文本太长,答案会在顶部显示进度条,因此要使其居中,您可以这样做:
val snackbar: Snackbar = Snackbar.make(this, text, duration)
val viewGroup = snackbar.view.findViewById<View>(com.google.android.material.R.id.snackbar_text).parent as ViewGroup
viewGroup.addView(ProgressBar(context).also {
it.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
it.foregroundGravity = Gravity.CENTER_VERTICAL
})
snackbar.show()