如何在Android中制作水平ListView?

Nei*_*aft 183 android listview gallery

可能重复:
Android中的水平ListView?

像Android中的许多东西一样,你不会认为这会是一个如此难的问题,但哦,哦,你会错的.而且,与Android中的许多内容一样,API甚至没有提供合理可扩展的起点.如果我要打开自己的ListView,我会被诅咒,当我想要的只是把它拿走并把它转向它.\咆哮

好吧,现在我已经完成了冒烟,让我们谈谈问题本身.我需要的基本上就像是Gallery,但没有中心锁定功能.我真的不需要ListViewlistSelector,但这是一个很好的选择.大多数情况下,我可以用LinearLayout内部的方式做我想做的事情ScrollView,但我需要来自a的子视图ListAdapter,我真的希望有一个视图回收器.我真的不想写任何布局代码.

我偷看了其中一些类的源代码......

图库:如果我覆盖大多数'onXyz'方法,复制所有源代码,但不要调用,看起来我可以使用Gallery scrollIntoSlots().但我敢肯定,如果我这样做,我会遇到一些无法访问的成员字段或其他一些无法预料的后果.

AbsSpinner:由于该mRecycler字段是包私有的,我怀疑我是否能够扩展这个类.

AbsListView:看起来这个类只适用于垂直滚动,所以没有帮助.

AdapterView:我从来没有直接扩展这个类.如果你告诉我它很容易做,并且很容易自己动手RecycleBin,我会非常怀疑,但我会试一试.

我想我可以复制这两个 AbsSpinnerGallery得到我想要的东西......希望那些类没有使用一些我无法访问的包私有变量.你们都认为这是一个好习惯吗?有没有人有任何教程或第三方解决方案可能会让我朝着正确的方向前进?

更新:
到目前为止我找到的唯一解决方案是自己完成所有工作.自从提出这个问题以来,我已经AdapterView从头开始重写并实现了自己的"Horizo​​ntalListView".真正覆盖Gallery的中心锁定功能的唯一方法是覆盖私有scrollIntoSlots方法,我认为这需要在运行时生成子类.如果你足够大胆地做到这一点,它可以说是最好的解决方案,但我不想依赖可能改变的无证方法.

下面的Swathi EP建议我给出Gallery一个OnTouchListener并覆盖滚动功能.如果你不关心你的列表中有一扔支持,或者如果它是没关系的意见,在一扔动画结束捕捉到中心,那么这为你工作!然而,最终仍然证明在不移除支撑的情况下移除中心锁定特征是不可能的.而且我问你,什么样的名单不扔?

所以,唉,这对我不起作用.:-(但如果你对这种方法感兴趣,请继续阅读......

我还必须对Swathi的代码添加一些内容以获得我想要的内容.在GestureListener.onTouch,除了委托给手势检测器,我还必须返回true ACTION_UPACTION_CANCEL事件.这成功禁用了中心锁定功能,但它也禁用了投掷功能.我能够通过将自己的GestureListener委托给Gallery的onFling方法来重新启用fling .如果您想尝试一下,请进入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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 48

看完这篇文章后,我实现了自己的横向ListView.你可以在这里找到它:http://dev-smart.com/horizo​​ntal-listview/如果有帮助请告诉我.

  • 此小部件不尊重wrap_content布局参数.如果你没有对它的高度进行硬编码,它会让你在一天的剩余时间里试图弄清楚为什么你的下面没有任何一个视图出现,因为它吞噬了所有的屏幕空间. (8认同)
  • 我在layout_height属性中添加了对"wrap_content"的支持.您可以在Horizo​​ntalViewList的分叉版本中找到此修复程序:https://github.com/inazaruk/android-dev-smart-lib (3认同)

Thi*_*ira 12

您是否考虑过使用Horizo​​ntalScrollView来包装列表项?这将允许您的每个列表项可以水平滚动(您放在那里的内容取决于您,并且可以使它们成为类似于ListView的动态项).如果您只在一行项目之后,这将很有效.

  • 我没有采用这种方法的两个原因:(1)它没有扩展`AdapterView`,所以我不能与我的`ListView`和`ListAdapter`代码互换使用它,以及(2)没有回收观点.也许有可能将这个功能添加到滚动视图中,但在我看来它会弄乱滚动行为,并且它几乎与简单地扩展`AdapterView`一样多. (25认同)