是否有可能使ListView水平?我使用图库视图完成了此操作,但所选项目会自动出现在屏幕的中心。我不希望选中的项目位于我点击的同一位置。我该如何纠正这个问题?我的想法是用水平滚动来设置ListView。分享你的想法?


这不是一个很好的答案,但是如何使用水平滚动视图呢?


你总是可以创建你的textviews等动态和设置你的onclicklistener,就像你会做一个适配器


为了找到解决这个问题的办法,我做了很多研究。简短的回答是,没有好的解决方案,除非重写私有方法之类的东西。我发现最好的方法是自己从头开始通过扩展AdapterView来实现它。这很悲惨。请看我关于水平listview的SO问题。


@Paul回答链接到一个伟大的解决方案,但代码不允许对项目的孩子使用onClickListeners(回调函数永远不会被调用)。我一直在努力寻找一个解决方案,我决定在这里张贴你需要修改的代码(以防有人需要它)。

而不是重写dispatchTouchEvent重写onTouchEvent。使用dispatchTouchEvent的相同代码并删除方法(您可以在这里阅读两者之间的区别http://developer.android.com/guide/topics/ui/ui-events.html#EventHandlers)

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean handled = mGesture.onTouchEvent(event);
    return handled;
}

然后,添加下面的代码,它将决定从子项中窃取事件,并将其交给我们的onTouchEvent,或让它由它们处理。

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch( ev.getActionMasked() ){
        case MotionEvent.ACTION_DOWN:
             mInitialX = ev.getX();
             mInitialY = ev.getY();             
             return false;
        case MotionEvent.ACTION_MOVE:
             float deltaX = Math.abs(ev.getX() - mInitialX);
             float deltaY = Math.abs(ev.getY() - mInitialY);
             return ( deltaX > 5 || deltaY > 5 );
        default:
             return super.onInterceptTouchEvent(ev);
    }
}

最后,不要忘记在你的类中声明变量:

private float mInitialX;
private float mInitialY;

我在自己的一个项目中也做了同样的事情,最后我也写了自己的项目。我称之为HorzListView现在是我的开源Aniqroid库的一部分。

http://aniqroid.sileria.com/doc/api/(在底部寻找下载或使用谷歌代码项目查看更多下载选项:http://code.google.com/p/aniqroid/downloads/list)

类文档在这里:http://aniqroid.sileria.com/doc/api/com/sileria/android/view/HorzListView.html


从这里下载jar文件

现在把它放到你的libs文件夹中,右键点击它并选择“添加为库”

现在在main.xml中放入这段代码

 <com.devsmart.android.ui.HorizontalListView
    android:id="@+id/hlistview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

现在在Activity类中,如果你想要带有图像的水平列表视图,那么就把这段代码放进去

  HorizontalListView hListView = (HorizontalListView) findViewById(R.id.hlistview);
    hListView.setAdapter(new HAdapter(this));


 private class HAdapter extends BaseAdapter {

    LayoutInflater inflater;

    public HAdapter(Context context) {
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Const.template.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listinflate, null);
            holder = new HViewHolder();
            convertView.setTag(holder);

        } else {
            holder = (HViewHolder) convertView.getTag();
        }
        holder.img = (ImageView) convertView.findViewById(R.id.image);
        holder.img.setImageResource(Const.template[position]);
        return convertView;
    }

}

class HViewHolder {
    ImageView img;
}

当适配器中的数据涉及到另一个线程时,HorizontialListView不能工作。一切都100%在UI线程上运行。这是多线程中的一个大问题。我认为使用HorizontialListView不是解决你的问题的最佳方案。HorzListView是一个更好的方法。你只需用HorzListView替换你以前的画廊。您不需要修改关于适配器的代码。然后一切都会如你所愿。查看https://stackoverflow.com/a/12339708/1525777关于HorzListView。


Paul没有费心去修复他的库中的bug或接受用户的修复。这就是为什么我建议另一个具有类似功能的库:

https://github.com/sephiroth74/HorizontalVariableListView

更新:2013年7月24日作者(sephiroth74)发布了基于android 4.2.2 ListView代码的完全重写版本。我必须说,它没有所有的错误,以前的版本有和伟大的工作!


其实很简单: 简单地旋转列表视图躺在它的一边

mlistView.setRotation(-90);

然后在膨胀子对象时,它应该在getView方法中。你让孩子们站直:

 mylistViewchild.setRotation(90);

编辑: 如果你的ListView在旋转后不合适,把ListView放在这个RotateLayout中,就像这样:

 <com.github.rongi.rotate_layout.layout.RotateLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:angle="90"> <!-- Specify rotate angle here -->

    <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    </ListView>
</com.github.rongi.rotate_layout.layout.RotateLayout>

你可以使用ViewFlipper来包含布局XML,并为每个布局XML添加图像、列表视图


我的解决方案是简单地使用ViewPager小部件。它不像Gallery那样中心锁定,并且有一个用于循环视图的内置功能(如ListView)。你可能会看到类似的方法在谷歌播放应用程序,每当你处理水平滚动列表。

你只需要扩展PagerAdapter并在那里执行一些调整:

public class MyPagerAdapter extends PagerAdapter {

    private Context mContext;

    public MyPagerAdapter(Context context) {
        this.mContext = context;
    }

    // As per docs, you may use views as key objects directly 
    // if they aren't too complex
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.item, null);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    // Important: page takes all available width by default,
    // so let's override this method to fit 5 pages within single screen
    @Override
    public float getPageWidth(int position) {
        return 0.2f;
    }
}

因此,你将拥有一个带有适配器的水平滚动小部件,如下所示:


我在我的项目中使用了水平列表视图链接,我得到了很好的结果。我最初使用devsmart库,但它给了我一些问题。所以使用水平列表视图链接的最佳方式,因为它恢复了我的问题,而且我最近在谷歌PlayStore上使用这个库推出了我的应用程序,并得到了用户的良好响应。所以我建议你使用上面提到的相同的库来水平显示listview。享受:)


您可以在支持库中使用RecyclerView。RecyclerView是ListView的一个通用版本,支持:

用于定位项目的布局管理器 默认动画 项目操作

Android循环查看文档


这有点(非常)晚了,但我把它贴出来,以防以后有人来看。

在Android L预览版的支持库中,有一个RecyclerView,它完全是你想要的。

现在,你只能通过L预览SDK获得它,你需要将你的minSdk设置为L。但是你可以将所有必要的文件复制到你的项目中,并以这种方式使用它们,直到L正式发布。

你可以在这里下载预览文档。

警告:回收器视图的API可能会改变,它可能有bug。

更新

水平列表视图的源代码是:

LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView myList = findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);

因为谷歌引入了Android支持库v7 21.0.0,你可以使用RecyclerView水平滚动项目。RecyclerView小部件是ListView的一个更高级、更灵活的版本。

要使用RecyclerView,只需添加依赖项:

com.android.support:recyclerview-v7:23.0.1

下面是一个例子:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);

        MyAdapter adapter = new MyAdapter(myDataset);
        recyclerView.setAdapter(adapter);
    }
}

关于RecyclerView的更多信息:

https://developer.android.com/training/material/lists-cards.html https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html


对于我的应用程序,我使用一个包含LinearLayout的HorizontalScrollView,它的方向设置为水平。为了在里面添加图像,我在活动中创建ImageViews,并将它们添加到我的LinearLayout中。例如:

<HorizontalScrollView 
        android:id="@+id/photo_scroll"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="horizontal"
        android:visibility="gone">

        <LinearLayout 
            android:id="@+id/imageview_holder"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:layout_height="match_parent">

        </LinearLayout>
    </HorizontalScrollView>

这对我来说很好。在活动中,我所要做的就是下面的代码:

LinearLayout imgViewHolder = findViewById(R.id.imageview_holder);
ImageView img1 = new ImageView(getApplicationContext());
//set bitmap
//set img1 layout params
imgViewHolder.add(img1);
ImageView img2 = new ImageView(getApplicationContext());
//set bitmap
//set img2 layout params
imgViewHolder.add(img2); 

就像我说的,这对我来说很有用,我希望它也能帮助那些希望实现这一目标的人。


根据Android文档RecyclerView是新的方式来组织项目在列表视图和水平显示

优点:

由于使用Recyclerview适配器,ViewHolder模式是 自动实现 动画很容易执行 更多特性

关于RecyclerView的更多信息:

grokkingandroid.com antonioleiva.com

示例:

survivingwithandroid.com

只需添加下面的块,使ListView水平从垂直

代码片段

LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);

注意:Android现在支持使用RecyclerView的水平列表视图,所以现在这个答案是不赞成的,关于RecyclerView的信息: https://developer.android.com/reference/android/support/v7/widget/RecyclerView

我已经开发了一个逻辑来做到这一点,而不使用任何外部水平滚动视图库,这是我实现的水平视图,我已经在这里发布了我的答案:https://stackoverflow.com/a/33301582/5479863

我的json响应是这样的:

{"searchInfo":{"status":"1","message":"Success","clist":[{"id":"1de57434-795e-49ac-0ca3-5614dacecbd4","name":"Theater","image_url":"http://52.25.198.71/miisecretory/category_images/movie.png"},{"id":"62fe1c92-2192-2ebb-7e92-5614dacad69b","name":"CNG","image_url":"http://52.25.198.71/miisecretory/category_images/cng.png"},{"id":"8060094c-df4f-5290-7983-5614dad31677","name":"Wine-shop","image_url":"http://52.25.198.71/miisecretory/category_images/beer.png"},{"id":"888a90c4-a6b0-c2e2-6b3c-561788e973f6","name":"Chemist","image_url":"http://52.25.198.71/miisecretory/category_images/chemist.png"},{"id":"a39b4ec1-943f-b800-a671-561789a57871","name":"Food","image_url":"http://52.25.198.71/miisecretory/category_images/food.png"},{"id":"c644cc53-2fce-8cbe-0715-5614da9c765f","name":"College","image_url":"http://52.25.198.71/miisecretory/category_images/college.png"},{"id":"c71e8757-072b-1bf4-5b25-5614d980ef15","name":"Hospital","image_url":"http://52.25.198.71/miisecretory/category_images/hospital.png"},{"id":"db835491-d1d2-5467-a1a1-5614d9963c94","name":"Petrol-Pumps","image_url":"http://52.25.198.71/miisecretory/category_images/petrol.png"},{"id":"f13100ca-4052-c0f4-863a-5614d9631afb","name":"ATM","image_url":"http://52.25.198.71/miisecretory/category_images/atm.png"}]}}

布局文件:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5">    
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4" />
    <HorizontalScrollView
        android:id="@+id/horizontalScroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">    
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

类文件:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
        for (int v = 0; v < collectionInfo.size(); v++) {
            /*---------------Creating frame layout----------------------*/

            FrameLayout frameLayout = new FrameLayout(ActivityMap.this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, getPixelsToDP(90));
            layoutParams.rightMargin = getPixelsToDP(10);
            frameLayout.setLayoutParams(layoutParams);

            /*--------------end of frame layout----------------------------*/

            /*---------------Creating image view----------------------*/
            final ImageView imgView = new ImageView(ActivityMap.this); //create imageview dynamically
            LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            imgView.setImageBitmap(collectionInfo.get(v).getCatImage());
            imgView.setLayoutParams(lpImage);
            // setting ID to retrieve at later time (same as its position)
            imgView.setId(v);
            imgView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    // getting id which is same as its position
                    Log.i(TAG, "Clicked on " + collectionInfo.get(v.getId()).getCatName());
                    // getting selected category's data list
                    new GetSelectedCategoryData().execute(collectionInfo.get(v.getId()).getCatID());
                }
            });
            /*--------------end of image view----------------------------*/

            /*---------------Creating Text view----------------------*/
            TextView textView = new TextView(ActivityMap.this);//create textview dynamically
            textView.setText(collectionInfo.get(v).getCatName());
            FrameLayout.LayoutParams lpText = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.CENTER);
            // Note: LinearLayout.LayoutParams 's gravity was not working so I putted Framelayout as 3 paramater is gravity itself
            textView.setTextColor(Color.parseColor("#43A047"));
            textView.setLayoutParams(lpText);
            /*--------------end of Text view----------------------------*/

            //Adding views at appropriate places
            frameLayout.addView(imgView);
            frameLayout.addView(textView);
            linearLayout.addView(frameLayout);

        }

 private int getPixelsToDP(int dp) {
        float scale = getResources().getDisplayMetrics().density;
        int pixels = (int) (dp * scale + 0.5f);
        return pixels;
    }

在这里工作的技巧是我分配给ImageView的id“imgView.setId(v)”,之后应用onClickListener,我再次获取视图的id ....为了便于理解,我还在代码中添加了注释, 我希望这对你很有用… 快乐的编码…:)


有一个很棒的库,叫做TwoWayView,它很容易实现,只需要将项目库包含到你的工作空间中,并将其作为库项目添加到你的原始项目中,然后遵循以下步骤,这是最初在这里提到的:

首先,让我们添加一个样式来指示ListView的方向 (水平或垂直)in (res/values/styles.xml):

<style name="TwoWayView">
    <item name="android:orientation">horizontal</item>
</style>

然后,

在你的布局XML中,使用下面的代码来添加TwoWayView:

<org.lucasr.twowayview.TwoWayView 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/lvItems"
     style="@style/TwoWayView"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:drawSelectorOnTop="false"
     tools:context=".MainActivity" />

最后,声明它并像任何常规的ListView一样处理它:

TwoWayView lvTest = (TwoWayView) findViewById(R.id.lvItems);

ListView的所有方法将在这里照常工作,但只有一个区别,我注意到,这是设置选择模式时,方法setChoiceMode不接受int值,而是从enum称为ChoiceMode的值,所以list_view.setChoiceMode(ListView. choice_mode_single);将lvTest.setChoiceMode(choicmode . single);//或多重或无。