[HorizontalScrollView] 水平滾動無螢寬限制>> 載入GridView
< 定義於.java >
1. 預設TableRow包含6個TextView
2. 在此想讓user按下畫面中的Button後, 能無限增加水平列的widget, 即使會超出螢寬
3. 增加onClick()與onFocusChange()行為
4. 重新命名/新增/刪除
5. 依TableRow不同的View載入對應的GridView
6. SQLite記載TableRow中view的資訊[宣告]
String tables[] = { "t_allApp_category"};
String fieldNames[][] =
{
{ "f_category_id", "f_category" }
};
String fieldTypes[][] =
{
{ "INTEGER PRIMARY KEY AUTOINCREMENT", "text" }
};
int version = 1;
private MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper
(
this,
"SQLite_Hori_malloc.db",
null,
version, /*version*/
tables,
fieldNames,
fieldTypes
);
String fieldNames[][] =
{
{ "f_category_id", "f_category" }
};
String fieldTypes[][] =
{
{ "INTEGER PRIMARY KEY AUTOINCREMENT", "text" }
};
int version = 1;
private MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper
(
this,
"SQLite_Hori_malloc.db",
null,
version, /*version*/
tables,
fieldNames,
fieldTypes
);
[新增]
Cursor c_category = null;
c_category = dbHelper.select(tables[0], null, null, null, null, null, null);
if(c_category.getCount() == 0)
{
//initial table[0] data
String[] updateCategory = {"f_category"};
for(int i=0 ; i<str_array_tv.length ; i++)
{
String mCategoryValues[] = {str_array_tv[i]};
dbHelper.insert(tables[0], updateCategory, mCategoryValues);
}
}
[更新]
String[] updateCategory = {"f_category"};
String[] updateValues = {et_category_rename.getText().toString()};
String where = "f_category_id=?";
String[] whereValue = {String.valueOf(categoryPresent)};
dbHelper.update(tables[0], updateCategory, updateValues, where, whereValue);
[刪除]
String where = "f_category_id=?";
String[] whereValue = {categoryPresent};
dbHelper.delete(tables[0], where, whereValue);
%%%%%%%%%%%%%%%./src/com.tsots.Hori_malloc/Activity_Hori_malloc.java%%%%%%%%%%%%%%%
package com.tsots.Hori_malloc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class Activity_Hori_malloc extends Activity
{
String tables[] = { "t_allApp_category"};
String fieldNames[][] =
{
{ "f_category_id", "f_category" }
};
String fieldTypes[][] =
{
{ "INTEGER PRIMARY KEY AUTOINCREMENT", "text" }
};
int version = 1;
private MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper (
this,
"SQLite_Hori_malloc.db",
null,
version, /*version*/
tables,
fieldNames,
fieldTypes
);
Context context = Activity_Hori_malloc.this;
private static final String TAG="Activity_Hori_malloc.java";
TextView title;
HorizontalScrollView scrollView;
TableRow row;
GridView gv_category;
TextView tv1, tv2, tv3, tv4, tv5, tv6;
TextView[] array_tv = {tv1, tv2, tv3, tv4, tv5, tv6};
String[] str_array_tv = {"All AP", "System AP", "Installed AP", "Entertainment", "Game", "..."};
String categoryPresent = "1";
int rowChildCount = 0;
int newrowChild = 0;
final int ID_RENAME = 0;
final int ID_NEW = 1;
final int ID_DELETE = 2;
AlertDialog alert;
CategoryAdapter mCategoryAdapter;
ArrayList<String> category_pag;
ArrayList<String> category_title;
ArrayList<Bitmap> category_icon;
Bitmap iconBitmap;
PackageManager packageManager;
TextView nowtextview;
TextView[] mTV;
Cursor c_category = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
title = (TextView) findViewById (R.id.title);
scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
row = (TableRow) scrollView.findViewById(R.id.row);
gv_category = (GridView) findViewById (R.id.gv_category);
packageManager = context.getPackageManager();
c_category = dbHelper.select(tables[0], null, null, null, null, null, null);
// 一開啟AP需要看到6個TextView, 即是做初始化設定
default_sqlite_data();
funInitTableItem();
}
public void default_sqlite_data()
{
Cursor c_category = null;
try
{
c_category = dbHelper.select(tables[0], null, null, null, null, null, null);
rowChildCount = c_category.getCount();
if(c_category.getCount() == 0)
{
//initial table[0] data
String[] updateCategory = {"f_category"};
for(int i=0 ; i<str_array_tv.length ; i++)
{
String mCategoryValues[] = {str_array_tv[i]};
dbHelper.insert(tables[0], updateCategory, mCategoryValues);
}
}
}
catch(Exception e)
{
Log.e(TAG, e.toString());
}
finally
{
c_category.close();
}
}
//[初始化]設定TextView的格式
public void funInitTableItem()
{
//先移除所有view, 以免累加
row.removeAllViews();
c_category = dbHelper.select(tables[0], null, null, null, null, null, null);
mTV = new TextView[c_category.getCount()];
rowChildCount = c_category.getCount();
for(int i=0 ; i<rowChildCount ; i++)
{
mTV[i]= new TextView(context);
mTV[i].setHeight(30);
mTV[i].setWidth(120);
mTV[i].setGravity(Gravity.CENTER);
mTV[i].setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.apple), null, null, null);
c_category.moveToPosition(i);
mTV[i].setText(c_category.getString(1));
mTV[i].setTextSize(18);
mTV[i].setTextColor(R.drawable.selector_category_textcolor);
mTV[i].setSingleLine();
mTV[i].setEllipsize(TruncateAt.MARQUEE);
mTV[i].setMarqueeRepeatLimit(6);
//array_tv[i].setFocusable(true);
//array_tv[i].setFocusableInTouchMode(true);
mTV[i].setBackgroundResource(R.drawable.selector_category);
//array_tv[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.label_selected));
//array_tv[i].setBackgroundResource(R.drawable.label_selected);
row.addView(mTV[i]);
}
funInitFocusAction();
}
//[初始化]設定TextView的Focus及Click行為
public void funInitFocusAction()
{
for(int i=0 ; i<rowChildCount ; i++)
{
final int ii = i;
mTV[i].setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus == true)
{
c_category.moveToPosition(ii);
categoryPresent = c_category.getString(0);
Toast.makeText(context, "focus "+array_tv[ii].getText().toString(), Toast.LENGTH_SHORT).show();
funCategoryContent(categoryPresent);
}
}
});
//Click事件的監聽, show出內容
mTV[i].setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
c_category.moveToPosition(ii);
categoryPresent = c_category.getString(0);
nowtextview = array_tv[ii];
//被Click的項目要調整背景色
setCategoryBackground(ii);
//載入對應的GridView內容
funCategoryContent(categoryPresent);
}
});
//LongClick事件的監聽, show出選單
mTV[i].setOnLongClickListener(new OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
//categoryPresent = ii+1;
c_category.moveToPosition(ii);
categoryPresent = c_category.getString(0);
//載入Dialog
funDialog(mTV[ii]);
return false;
}
});
}
}
//[新項目]設定TextView的格式
public void funNewTableItem()
{
Cursor c_category = null;
try
{
c_category = dbHelper.select(tables[0], null, null, null, null, null, null);
String[] updateCategory = {"f_category"};
String mCategoryValues[] = {"Newtext"};
dbHelper.insert(tables[0], updateCategory, mCategoryValues);
rowChildCount = c_category.getCount();
//更新畫面
funInitTableItem();
}
catch(Exception e)
{
Log.e(TAG, e.toString());
}
finally
{
c_category.close();
}
}
//提供"Rename", "New", "Delete"的選單
public void funDialog(final TextView textview)
{
AlertDialog.Builder AB = new AlertDialog.Builder(context);
DialogInterface.OnClickListener DIO = new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
switch(which)
{
case ID_RENAME: //int ID_RENAME = 0;
AlertDialog.Builder dialog_category = new AlertDialog.Builder(context);
LayoutInflater factory = LayoutInflater.from(context);
View view = factory.inflate(R.layout.layout_category_options, null);
final EditText et_category_rename = (EditText) view.findViewById(R.id.et_category_rename);
Button bt_category_rename_ok = (Button) view.findViewById(R.id.bt_category_rename_ok);
Button bt_category_rename_no = (Button) view.findViewById(R.id.bt_category_rename_no);
dialog_category.setView(view);
alert = dialog_category.create();
et_category_rename.setText(textview.getText().toString());
alert.show();
bt_category_rename_ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
textview.setText(et_category_rename.getText().toString());
String[] updateCategory = {"f_category"};
String[] updateValues = {et_category_rename.getText().toString()};
String where = "f_category_id=?";
String[] whereValue = {String.valueOf(categoryPresent)};
dbHelper.update(tables[0], updateCategory, updateValues, where, whereValue);
alert.dismiss();
}
});
bt_category_rename_no.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
alert.dismiss();
}
});
break;
case ID_NEW: //int ID_NEW = 1;
funNewTableItem();
break;
case ID_DELETE: //int ID_DELETE = 2;
String where = "f_category_id=?";
String[] whereValue = {categoryPresent};
dbHelper.delete(tables[0], where, whereValue);
funInitTableItem();
break;
}
}
};
String[] items = {"Rename", "New", "Delete"};
AB.setItems(items, DIO);
AB.show();
}
public void funDeleteTableItem(int index)
{
Cursor c_c = null;
try
{
c_c = dbHelper.select(tables[0], null, null, null, null, null, null);
c_c.moveToFirst();
String[] str = new String[c_c.getCount()];
for (int j=0;j<c_c.getCount();j++)
{
str[j] = c_c.getString(1);
}
}
catch(Exception e)
{
Log.e(TAG, e.toString());
}
finally
{
c_c.close();
}
// index = row.indexOfChild(textview)
row.removeViewAt(index);
//表示TableRow內含的widget數量-1
rowChildCount--;
}
//載入對應的GridView內容
public void funCategoryContent(String nowCategory)
{
//初始化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 ++)
{
ApplicationInfo appAI = allappInfo.get(i);
//取得系統中可Launch的AP
Intent intent = packageManager.getLaunchIntentForPackage(appAI.packageName);
if (intent != null)
{
if(nowCategory.equals("1"))
{
funLoadAP(appAI);
}
else if(nowCategory.equals("2"))
{
//取得系統中可Launch的系統AP
if ((appAI.flags & appAI.FLAG_SYSTEM) > 0)
{
funLoadAP(appAI);
}
}
else if(nowCategory.equals("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);
gv_category.requestFocus();
}
//取得AP的資訊: package, name, icon, info
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");
}
public void setCategoryBackground(int categoryPosition)
{
for(int i=0; i<mTV.length; i++)
{
if(i == categoryPosition)
{
mTV[i].setBackgroundResource(R.drawable.label_focus);
} else {
mTV[i].setBackgroundResource(R.drawable.label_selected);
}
}
}
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;
}
}
}
沒有留言:
張貼留言