Possible Duplicate:
Horizontal ListView in Android?
和安乐斯的许多事情一样,你不认为这是一个如此棘手的问题,但用刚果语说,你会错。 如同安的很多事情一样,安普森甚至提供了合理的切入点。 如果我能亲自提出我自己的名单的话,那么我就会大声疾呼,而我却要把这.倒。 ant
奥凯,现在我做了大量的工作,让我谈谈问题本身。 我需要的基本上是像<条码>Gallery那样的,但没有中心锁定特征。 我不需要<代码>。 ListView s listSelector but it s a nice-to-have. 多数情况下,我可以采用<代码>LinearLayout。 观点条码>,但我需要儿童的意见来自<条码>。 页: 1 不想书写任何排外法。
I peeked into the source Code for some of these levels...
我可以申请复制AbsSpinner
和Gallery
,以获得我所希望的东西。 所有人是否认为这是一种良好做法? 是否有任何辅导或第三方解决办法使我走上正确的方向?
Update:
The only solution I ve found so far is to do it all myself. Since asking this question, I have overridden AdapterView
and implemented my own "HorizontalListView" from scratch. The only way to truly override the Gallery s center-locking feature is to override the private scrollIntoSlots
method, which I believe would require generating a subclass at runtime. If you re bold enough to do that, it s arguably the best solution, but I don t want to rely on undocumented methods that could change.
下面的SwathiEPA建议,我提供<代码>Gallery的
因此,这不是为了我工作: 但是,如果你重新对这种做法感兴趣,就读到......。
我还必须对Swathi的法典作一些补充,以了解我想要的东西。 在<代码>GestureListener.on Touch中,除了被授权到姿态探测器之外,我还必须返回<代码>ACTION_UP和ACTION_CompL
活动。 这成功打破了中心锁定的特征,但也造成了残疾。 我通过让我自己的GestureListener代表到美术厅onFling
,得以重现。 如果你想尝试,就进入你ApiDemos的样本代码,用以下代码取代美术1.java的类别:
import com.example.android.apis.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
public class Gallery1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_1);
// Reference the Gallery view
final Gallery g = (Gallery) findViewById(R.id.gallery);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));
// Set a item click listener, and just Toast the clicked position
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
// Gesture detection
final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector(g));
OnTouchListener gestureListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean retVal = gestureDetector.onTouchEvent(event);
int action = event.getAction();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
retVal = true;
onUp();
}
return retVal;
}
public void onUp() {
// Here I am merely copying the Gallery s onUp() method.
for (int i = g.getChildCount() - 1; i >= 0; i--) {
g.getChildAt(i).setPressed(false);
}
g.setPressed(false);
}
};
g.setOnTouchListener(gestureListener);
// We also want to show context menu for longpressed items in the gallery
registerForContextMenu(g);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(R.string.gallery_2_text);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
return true;
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
// See res/values/attrs.xml for the <declare-styleable> that defines
// Gallery1.
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
// The preferred Gallery item background
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
private Context mContext;
private Integer[] mImageIds = {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8
};
}
public class MyGestureDetector extends SimpleOnGestureListener {
private Gallery gallery;
public MyGestureDetector(Gallery gallery) {
this.gallery = gallery;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return gallery.onFling(e1, e2, velocityX, velocityY);
}
}
}