我为android开发了锁屏应用程序。我必须自定义视图以在锁屏显示它。如果我使用活动,gridview显示数据正常,但是当我在自定义视图中使用它时,它显示页面为空。 Customview是从Service调用的,而不是activity。
我的代码
Customview
<suite name="Suite1" verbose="1" >
<test name="Sequential" parallel="false" >
<test-group ref="smoke-tests" />
</test>
<test name="ParallelGroup" parallel="classes" >
<test-group ref="regular-tests" />
</test>
</suite>
适配器
public class PhotoView extends LinearLayout {
private PhotoView.OnPhotoListener mOnPhotoListener;
private Context mContext;
private View mPhotoView;
private boolean isAttached = false;
private static final String TAG = "PhotoView";
private GridView gridGallery;
private Handler handler;
private PhotoAdapter adapter;
public interface OnPhotoListener {
void onClose();
}
public void setOnPhotoListener(OnPhotoListener mOnPhotoListener) {
this.mOnPhotoListener = mOnPhotoListener;
}
public PhotoView(Context context) {
super(context);
this.mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mPhotoView = inflater.inflate(R.layout.fragment_photo, this, true);
initView();
}
public PhotoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PhotoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initView() {
findViewById(R.id.imgBackPhoto).setOnClickListener(onClickListener);
handler = new Handler();
gridGallery = (GridView) mPhotoView.findViewById(R.id.gridGallery);
gridGallery.setFastScrollEnabled(true);
adapter = new PhotoAdapter(mContext);
PauseOnScrollListener listener = new PauseOnScrollListener(ImageLoader.getInstance(),
true, true);
gridGallery.setOnScrollListener(listener);
gridGallery.setOnItemClickListener(mItemSingleClickListener);
gridGallery.setAdapter(adapter);
new Thread() {
@Override
public void run() {
Looper.prepare();
handler.post(new Runnable() {
@Override
public void run() {
adapter.addAll(getRealPathList());
//checkImageStatus();
}
});
Looper.loop();
};
}.start();
}
AdapterView.OnItemClickListener mItemSingleClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
PhotoItemData item = adapter.getItem(position);
Intent data = new Intent().putExtra("single_path", item.getSdcardPath());
}
};
public ArrayList<PhotoItemData> getRealPathList() {
Uri contentUri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ArrayList<PhotoItemData> galleryList = new ArrayList<PhotoItemData>();
try {
String[] proj = {MediaStore.Images.Media.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
Cursor cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
PhotoItemData item = new PhotoItemData();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String res = cursor.getString(column_index);
item.setSdcardPath(res);
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
// show newest photo at beginning of the list
Collections.reverse(galleryList);
return galleryList;
}
public boolean isAttached() {
return isAttached;
}
@Override
protected void onAttachedToWindow() {
isAttached = true;
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
isAttached = false;
super.onDetachedFromWindow();
}
private View.OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgBackPhoto:
L.d(TAG, "close");
mOnPhotoListener.onClose();
break;
default:
break;
}
}
};
}