我想执行方案,
我正在使用自定义TouchyGridView
一切都很好。
但现在我想添加新功能。
我想在后台下载APK文件。因此,当下载开始时,我想在网格视图中显示新图标,并在新成绩项目上显示进度条。
*我知道如何从服务器下载。
ProgressBar
应使用progressbar.setProgress(progress);
我该怎么做?
我尝试了BroadCastReceiver
,但没有成功,因为我的广播在MainActivity中,View在适配器中,所以如何使用BroadCastReceiver
更新Adapter类的视图?
grid_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/grid_app_image"
android:layout_width="@dimen/icon_width"
android:layout_height="@dimen/icon_height"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="@dimen/icon_width"
android:layout_height="@dimen/icon_height"
android:progressDrawable="@drawable/circular" />
</RelativeLayout>
<TextView
android:id="@+id/grid_app_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5px"
android:gravity="center_horizontal"
android:text="app"
android:textColor="@color/content"
android:textSize="18sp" />
</LinearLayout>
HomeScreenAppGridAdapter.java
public class HomeScreenAppGridAdapter extends BaseAdapter implements
OnClickListener {
private Context mContext;
private List<KioskAppDetail> mAppList;
private PackageManager mPackageManager;
private String tag = "GridAdapter";
// Constructor to initialize values
public HomeScreenAppGridAdapter(Context context,
List<KioskAppDetail> appList) {
mContext = context;
mAppList = appList;
mPackageManager = mContext.getPackageManager();
}
@Override
public int getCount() {
// Number of times getView method call depends upon gridValues.length
return mAppList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
// Number of times getView method call depends upon gridValues.length
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
// if (convertView == null) {
gridView = inflater.inflate(R.layout.grid_item, null);
final TextView textView = (TextView) gridView
.findViewById(R.id.grid_app_label);
final ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_app_image);
ProgressBar progressBar = (ProgressBar) gridView
.findViewById(R.id.progressBar);
if (!mAppList.get(position).getAppLable().toString()
.equalsIgnoreCase("Peripheral Settings")) {
/*int color = Color.parseColor(Util.getStringDefaultPrefValue(
mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT));*/
progressBar.setVisibility(View.GONE);
String iconSizePer = Util.getStringDefaultPrefValue(mContext, Util.ICON_SIZE_DP, Util.DEF_ICON_SIZE_DP);
if(iconSizePer != null) {
if(!iconSizePer.trim().isEmpty()) {
int originalIconSize = 60; // This is 100% Icon Size
int iconSize = (Integer.parseInt(iconSizePer) * originalIconSize) / 100;
int pixel = dpToPx(iconSize);
imageView.getLayoutParams().height = pixel;
imageView.getLayoutParams().width = pixel;
imageView.requestLayout();
}
}
String textSizePer = Util.getStringDefaultPrefValue(mContext, Util.TEXT_SIZE_SP, Util.DEF_TEXT_SIZE_SP);
if(textSizePer != null) {
if(!textSizePer.trim().isEmpty()) {
int originalTextSize = 18; // This is 100% text Size
int textSize = (Integer.parseInt(textSizePer) * originalTextSize) / 100;
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
} else {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
}
}
int color;
/*= Color.parseColor(Util.getStringDefaultPrefValue(
mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT));
*/
String selectedColor = Util.getStringDefaultPrefValue(mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT);
if(selectedColor.equalsIgnoreCase("")) {
color = Color.parseColor(mContext.getString(R.string.black));
} else if (selectedColor.equalsIgnoreCase("Black")) {
color = Color.parseColor(mContext.getString(R.string.black));
} else if (selectedColor.equalsIgnoreCase("Blue")) {
color = Color.parseColor(mContext.getString(R.string.blue));
} else if (selectedColor.equalsIgnoreCase("Cyan")) {
color = Color.parseColor(mContext.getString(R.string.cyan));
} else if (selectedColor.equalsIgnoreCase("Dark Grey")) {
color = Color.parseColor(mContext.getString(R.string.dark_grey));
} else if (selectedColor.equalsIgnoreCase("Grey")) {
color = Color.parseColor(mContext.getString(R.string.grey));
} else if (selectedColor.equalsIgnoreCase("Light Grey")) {
color = Color.parseColor(mContext.getString(R.string.light_grey_text));
} else if (selectedColor.equalsIgnoreCase("Green")) {
color = Color.parseColor(mContext.getString(R.string.green));
} else if (selectedColor.equalsIgnoreCase("Magenta")) {
color = Color.parseColor(mContext.getString(R.string.magenta));
} else if (selectedColor.equalsIgnoreCase("Red")) {
color = Color.parseColor(mContext.getString(R.string.red));
} else if (selectedColor.equalsIgnoreCase("White")) {
color = Color.parseColor(mContext.getString(R.string.white));
} else if (selectedColor.equalsIgnoreCase("Yellow")) {
color = Color.parseColor(mContext.getString(R.string.yellow));
} else {
color = Color.parseColor(mContext.getString(R.string.black));
}
textView.setTextColor(color);
textView.setText(mAppList.get(position).getAppLable());
// Log.d(tag, mAppList.get(position).getAppLable() + "App lable"
// + mAppList.size() + "App list size" + position);
// if (TextUtils.isEmpty(mAppList.get(position).getAppIcon())) {
// no custom icon, use app's icon
imageView.setImageDrawable(mAppList.get(position).getDefaultAppIcon());
// }
final OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(tag, "app icon clicked ["
+ mAppList.get(position).getPackageName());
Intent i = mPackageManager
.getLaunchIntentForPackage(mAppList.get(position)
.getPackageName());
mContext.startActivity(i);
}
};
imageView.setOnClickListener(onClickListener);
} else {
Log.d(tag, "In If Home Screen Adapter");
int color;
/*= Color.parseColor(Util.getStringDefaultPrefValue(
mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT));
*/
String selectedColor = Util.getStringDefaultPrefValue(mContext, Util.TEXT_COLOR, Util.TEXT_COLOR_DEFAULT);
if(selectedColor.equalsIgnoreCase("")) {
color = Color.parseColor(mContext.getString(R.string.black));
} else if (selectedColor.equalsIgnoreCase("Black")) {
color = Color.parseColor(mContext.getString(R.string.black));
} else if (selectedColor.equalsIgnoreCase("Blue")) {
color = Color.parseColor(mContext.getString(R.string.blue));
} else if (selectedColor.equalsIgnoreCase("Cyan")) {
color = Color.parseColor(mContext.getString(R.string.cyan));
} else if (selectedColor.equalsIgnoreCase("Dark Grey")) {
color = Color.parseColor(mContext.getString(R.string.dark_grey));
} else if (selectedColor.equalsIgnoreCase("Grey")) {
color = Color.parseColor(mContext.getString(R.string.grey));
} else if (selectedColor.equalsIgnoreCase("Light Grey")) {
color = Color.parseColor(mContext.getString(R.string.light_grey_text));
} else if (selectedColor.equalsIgnoreCase("Green")) {
color = Color.parseColor(mContext.getString(R.string.green));
} else if (selectedColor.equalsIgnoreCase("Magenta")) {
color = Color.parseColor(mContext.getString(R.string.magenta));
} else if (selectedColor.equalsIgnoreCase("Red")) {
color = Color.parseColor(mContext.getString(R.string.red));
} else if (selectedColor.equalsIgnoreCase("White")) {
color = Color.parseColor(mContext.getString(R.string.white));
} else if (selectedColor.equalsIgnoreCase("Yellow")) {
color = Color.parseColor(mContext.getString(R.string.yellow));
} else {
color = Color.parseColor(mContext.getString(R.string.black));
}
textView.setTextColor(color);
textView.setText(mAppList.get(position).getAppLable());
// if
// (TextUtils.isEmpty(mAppList.get(position).getAppIcon()))
// {
// no custom icon, use app's icon
imageView.setImageResource(R.drawable.icon_settings);
// }
final OnClickListener onClickListenerr = new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(tag, "app icon clicked ["
+ mAppList.get(position).getPackageName());
// Intent i =
// mPackageManager.getLaunchIntentForPackage(mAppList
// .get(position).getPackageName());
// mContext.startActivity(i);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
boolean status_for_useage_access_on_app_click = Util
.systemSettingsOnUsage(mContext);
if (status_for_useage_access_on_app_click == true) {
// Intent i = mPackageManager
// .getLaunchIntentForPackage(mAppList
// .get(position)
// .getPackageName());
// mContext.startActivity(i);
Intent intent = new Intent(
mContext,
net.intricare.allowedsettings.PeripheralSettingsActivity.class);
mContext.startActivity(intent);
} else {
Util.launchUsageAccessDialog(mContext);
}
} else {
Intent intent = new Intent(
mContext,
net.intricare.allowedsettings.PeripheralSettingsActivity.class);
mContext.startActivity(intent);
}
}
};
// --------------------------------------------- //
imageView.setOnClickListener(onClickListenerr);
}
// } else {
// gridView = (View) convertView;
// }
/**
* duplicate code
*/
// --------------------------------------------- //
/*if (!mAppList.get(position).getAppLable().toString()
.equalsIgnoreCase("Peripheral Settings")) {
int color = Color.parseColor(Util.getStringDefaultPrefValue(
mContext, "text_color", "black"));
textView.setTextColor(color);
textView.setText(mAppList.get(position).getAppLable());
// Log.d(tag, mAppList.get(position).getAppLable() + "App lable"
// + mAppList.size() + "App list size duplicate code"
// + position);
// if (TextUtils.isEmpty(mAppList.get(position).getAppIcon())) {
// no custom icon, use app's icon
imageView.setImageDrawable(mAppList.get(position)
.getDefaultAppIcon());
// }
final OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(tag, "app icon clicked ["
+ mAppList.get(position).getPackageName());
try {
Intent i = mPackageManager
.getLaunchIntentForPackage(mAppList.get(position)
.getPackageName());
mContext.startActivity(i);
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
};
imageView.setOnClickListener(onClickListener);
} else {
TextView textVieww = (TextView) gridView
.findViewById(R.id.grid_app_label);
int color = Color.parseColor(Util.getStringDefaultPrefValue(
mContext, "text_color", "black"));
textVieww.setTextColor(color);
textVieww.setText(mAppList.get(position).getAppLable());
ImageView imageVieww = (ImageView) gridView
.findViewById(R.id.grid_app_image);
imageVieww.setImageResource(R.drawable.icon_settings);
final OnClickListener onClickListenerr = new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(tag, "app icon clicked ["
+ mAppList.get(position).getPackageName());
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
boolean status_for_useage_access_on_app_click = Util
.systemSettingsOnUsage(mContext);
if (status_for_useage_access_on_app_click == true) {
Intent intent = new Intent(
mContext,
net.intricare.allowedsettings.PeripheralSettingsActivity.class);
mContext.startActivity(intent);
} else {
Util.launchUsageAccessDialog(mContext);
}
} else {
Intent intent = new Intent(
mContext,
net.intricare.allowedsettings.PeripheralSettingsActivity.class);
mContext.startActivity(intent);
}
}
};
// --------------------------------------------- //
imageVieww.setOnClickListener(onClickListenerr);
}*/
return gridView;
}
public static int pxToDp(int px) {
return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}
public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
@Override
public void onClick(View v) {
Log.i(tag, "touch event");
}
}
HomeActivity.java
TouchyGridView mGridView;
mGridView = (TouchyGridView) findViewById(R.id.apps_grid);
LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MESSAGE_PROGRESS);
bManager.registerReceiver(broadcastReceiver, intentFilter);
mGridView.setAdapter(new HomeScreenAppGridAdapter(HomeActivity.this, mKioskAppsList));
mGridView.setOnNoItemClickListener(this);
mGridView.setOnItemClickListener(this);
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(MESSAGE_PROGRESS)){
//initHomeScreenUI(true);
/*if(download.getProgress() == 100){
mProgressText.setText("File Download Complete");
} else {
mProgressText.setText(String.format("Downloaded (%d/%d) MB",download.getCurrentFileSize(),download.getTotalFileSize()));
}*/
}
}
};
// When Download Start, I am sending broadcast using below code
Intent intent = new Intent(HomeActivity.MESSAGE_PROGRESS);
intent.putExtra("download",download);
LocalBroadcastManager.getInstance(DownloadService.this).sendBroadcast(intent);
答案 0 :(得分:1)
请按照以下步骤解决问题。
首先确保Adapter类和活动代码在同一个类中。