可能重复:
Horizontal ListView in Android?如何在Android中制作水平ListView?
像Android的很多东西,你就不会认为这将是这样一个困难的问题,但Phoebe:哦,天哪,你是错的。而且,就像Android中的许多事情一样,API甚至没有提供合理的可扩展起点。如果我要推出自己的ListView,那么我会被吓倒,当我想要的只是把它放在一边时。 \ rant
好吧,现在我已经完成了冒烟,让我们来谈谈问题本身。我需要的基本上与Gallery
完全相同,但没有中央锁定功能。我真的不需要ListView
的listSelector,但它是一件很棒的事情。大多数情况下,我可以在ScrollView
内部以LinearLayout
的方式做我想做的事情,但我需要孩子的意见来自ListAdapter
,我真的很想拥有一个视图回收站。而我真的不想写任何布局代码。
我偷看到一些这些类的源代码
画廊:它看起来像我可以使用库,如果我重写大部分的“onXyz”的方法,复制所有的源代码,但不要致电scrollIntoSlots()
。但我敢肯定,如果我这样做,我会遇到一些无法访问或其他无法预料的后果的成员字段。
AbsSpinner:由于mRecycler
字段是包私人的,我怀疑我可以扩展这个类。
AbsListView:它看起来像这个类只适用于垂直滚动,所以没有帮助。
AdapterView:我从来没有必须直接扩展这个类。如果你告诉我这很容易做到,并且很容易推出我自己的RecycleBin
,我会非常怀疑,但我会给它一个镜头。
我想我可能复制都AbsSpinner
和Gallery
得到我想要的东西......希望这些类不使用我无法访问某些包私有的变数。你们是否认为这是一个好习惯?有没有人有任何教程或第三方解决方案可能使我朝着正确的方向发展?
更新:
我发现迄今做这一切我自己唯一的解决办法。自从问这个问题以来,我已经重写了AdapterView
并从头开始实现了我自己的“HorizontalListView”。真正重写Gallery的中心锁定功能的唯一方法是重写私有的scrollIntoSlots
方法,我相信这需要在运行时生成一个子类。如果你足够大胆地这样做,这可以说是最好的解决方案,但我不想依赖可能改变的无证方法。
Swathi EP下面建议我给Gallery
一个OnTouchListener
并覆盖滚动功能。如果你不在意在列表中支持fling,或者在视频动画结束时可以将视图捕捉到中心,那么这个将为工作!然而,最终仍然无法取消中央锁定功能而无需移除支架。我问你,什么样的名单不会扔?
所以,唉,这不适合我。 :-(但是如果你对这种方法感兴趣,请继续阅读...
我还必须对Swathi的代码做一些补充以获得我想要的内容GestureListener.onTouch
除了委托给手势检测器之外,我还必须为ACTION_UP
和ACTION_CANCEL
事件返回true。这成功禁用中央锁功能,但它也被禁止甩。我能够有自己GestureListener委托给画廊的onFling
方法来重新启用一扔,如果你想试一下,进入你的ApiDemos示例代码,并用下面的代码替换Gallery1.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);
}
}
}
我试过这个帖子,为我工作... http://whats-online.info/science-and-tutorials/87/Android-tutorial-Horizontal-RecyclerView-with-images-and-text-example/ – 2017-01-24 09:33:50