[HorizontalScrollView] 水平滾動無螢寬限制>> Rename/New/Delete 行為
< 定義於.java >
1. 預設TableRow包含6個TextView
2. 在此想讓user按下畫面中的Button後, 能無限增加水平列的widget, 即使會超出螢寬
3. 增加onClick()與onFocusChange()行為
4. 重新命名/新增/刪除
5. 依TableRow不同的View載入GridView
./src/com.tsots.Hori_malloc/Hori_malloc_FC.java (只挑重點補充)
//Click事件的監聽, show出內容
array_tv[i].setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//紀錄view的位置
categoryPresent = ii+1;
//被Click的項目要調整背景色
setCategoryBackground(ii);
//載入對應的GridView內容
funCategoryContent(categoryPresent);
}
});
//LongClick事件的監聽, show出選單
array_tv[i].setOnLongClickListener(new OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
//紀錄view的位置
categoryPresent = ii+1;
//載入Dialog
funDialog(array_tv[ii]);
return false;
}
});
array_tv[i].setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//紀錄view的位置
categoryPresent = ii+1;
//被Click的項目要調整背景色
setCategoryBackground(ii);
//載入對應的GridView內容
funCategoryContent(categoryPresent);
}
});
//LongClick事件的監聽, show出選單
array_tv[i].setOnLongClickListener(new OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
//紀錄view的位置
categoryPresent = ii+1;
//載入Dialog
funDialog(array_tv[ii]);
return false;
}
});
public void setCategoryBackground(int categoryPosition)
{
//逐一筆對, 若為當前click的view, 則改變背景色
for(int i=0; i<array_tv.length; i++)
{
if(i == categoryPosition)
{
array_tv[i].setBackgroundResource(R.drawable.label_focus);
}
else
{
array_tv[i].setBackgroundResource(R.drawable.label_selected);
}
}
}
public void funCategoryContent(int nowCategory)
nowCategory == 1 |
//初始化ArrayList
category_pag = new ArrayList<String>();
category_title = new ArrayList<String>();
category_icon = new ArrayList<Bitmap>();
//取得系統中所有AP, 也包含不可Launch的
List<ApplicationInfo> allappInfo =
packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES);
//AP名稱按英文字母排序
Collections.sort(allappInfo, new ApplicationInfo.DisplayNameComparator(packageManager));
for(int i = 0 ; i <allappInfo.size() ; i ++)
{
nowCategory ==2 |
ApplicationInfo appAI = allappInfo.get(i);
//取得系統中可Launch的AP
Intent intent = packageManager.getLaunchIntentForPackage(appAI.packageName);
if (intent != null)
{
if(nowCategory == 1)
{
funLoadAP(appAI);
}
else if(nowCategory == 2)
{
//取得系統中可Launch的系統AP
if ((appAI.flags & appAI.FLAG_SYSTEM) > 0)
{
funLoadAP(appAI);
}
}
else if(nowCategory == 3)
nowCategory ==3 |
{
//取得系統中可Launch的手動安裝AP
if ((appAI.flags & appAI.FLAG_SYSTEM) <= 0)
{
funLoadAP(appAI);
}
}
}
}
//蒐集 AP NAME 和 AP ICON 給class CategoryAdapter
mCategoryAdapter = new CategoryAdapter(context, category_title, category_icon);
gv_category.setAdapter(mCategoryAdapter);
}
//提供"Rename", "New", "Delete"的選單
public void funDialog(final TextView textview)
{
//請參考上一篇~
}
public void funLoadAP(ApplicationInfo appAI)
{
String apPag = appAI.packageName; //com.android.launcher
String apName = appAI.loadLabel(packageManager).toString();//Launcher
Drawable apIcon = appAI.loadIcon(packageManager); //android.graphics.drawable.BitmapDrawable@2ae99f60
ApplicationInfo apInfo = appAI; //ApplicationInfo{2ad52d98 com.android.launcher}
category_pag.add(apPag);
category_title.add(apName);
//將Drawable轉為Bitmap
Bitmap bp = drawableToBitmap(apIcon);
//以Bitmap的型態來調整icon大小
category_icon.add(fixSize(bp));
title.setText("AP : "+String.valueOf(category_title.size()) + " piece");
}
static Bitmap drawableToBitmap(Drawable drawable)
{
Bitmap.Config c =drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), c);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
public Bitmap fixSize(Bitmap entry_icon)
{
int iconWidth = 0;
int iconHeight = 0;
int newIconWidth = 80;
int newIconHeight = 80;
iconWidth = entry_icon.getWidth();
iconHeight = entry_icon.getHeight();
float scaleIconWidth = ((float) newIconWidth) / iconWidth;
float scaleIconHeight = ((float) newIconHeight) / iconHeight;
Matrix iconMatrix = new Matrix();
iconMatrix.postScale(scaleIconWidth, scaleIconHeight);
iconBitmap = Bitmap.createBitmap(entry_icon, 0, 0,iconWidth, iconHeight, iconMatrix, true);
return iconBitmap;
}
--------------------------------------------------------------------------------------------------------------
class CategoryAdapter extends BaseAdapter
{
private Context mContext;
ArrayList<String> adapterTitle;// = new ArrayList<String>();
ArrayList<Bitmap> adapterIcon;// = new ArrayList<Bitmap>();
public CategoryAdapter(Context c, ArrayList<String> category_title, ArrayList<Bitmap> category_icon)
{
mContext = c;
adapterTitle = category_title;
adapterIcon = category_icon;
}
public int getCount()
{
return adapterTitle.size();
}
public Object getItem(int position)
{
return adapterTitle.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater factory = LayoutInflater.from(mContext);
View v = (View) factory.inflate(R.layout.all_app_item, null);
TextView textView = (TextView) v.findViewById(R.id.name);
textView.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(adapterIcon.get(position)), null, null);
textView.setText(adapterTitle.get(position).toString());
return v;
}
}
{
private Context mContext;
ArrayList<String> adapterTitle;// = new ArrayList<String>();
ArrayList<Bitmap> adapterIcon;// = new ArrayList<Bitmap>();
public CategoryAdapter(Context c, ArrayList<String> category_title, ArrayList<Bitmap> category_icon)
{
mContext = c;
adapterTitle = category_title;
adapterIcon = category_icon;
}
public int getCount()
{
return adapterTitle.size();
}
public Object getItem(int position)
{
return adapterTitle.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater factory = LayoutInflater.from(mContext);
View v = (View) factory.inflate(R.layout.all_app_item, null);
TextView textView = (TextView) v.findViewById(R.id.name);
textView.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(adapterIcon.get(position)), null, null);
textView.setText(adapterTitle.get(position).toString());
return v;
}
}
沒有留言:
張貼留言