Horizontal ListView in Android?

后端 未结 19 944
深忆病人
深忆病人 2020-11-22 03:54

Is it possible to make the ListView horizontally? I have done this using a gallery view, but the selected item comes to the center of the screen automatically.

相关标签:
19条回答
  • 2020-11-22 04:31

    Paul doesn't bother to fix bugs of his library or accept users fixes. That's why I am suggesting another library which has similar functionality:

    https://github.com/sephiroth74/HorizontalVariableListView

    Update: on Jul 24, 2013 author (sephiroth74) released completely rewritten version based on code of android 4.2.2 ListView. I must say that it doesn't have all the errors which previous version had and works great!

    0 讨论(0)
  • 2020-11-22 04:36

    Its actually very simple: simply Rotate the list view to lay on its side

    mlistView.setRotation(-90);

    Then upon inflating the children, that should be inside the getView method. you rotate the children to stand up straight:

     mylistViewchild.setRotation(90);
    

    Edit: if your ListView doesnt fit properly after rotation, place the ListView inside this RotateLayout like this:

     <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>
    
    0 讨论(0)
  • 2020-11-22 04:37

    For my application, I use a HorizontalScrollView containing LinearLayout inside, which has orientation set to horizontal. In order to add images inside, I create ImageViews inside the activity and add them to my LinearLayout. For example:

    <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>
    

    An this works perfectly fine for me. In the activity all I have to do is something like the code below:

    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); 
    

    As I said that works for me, and I hope it helps somebody looking to achieve this as well.

    0 讨论(0)
  • 2020-11-22 04:40

    well you can always create your textviews etc dynamically and set your onclicklisteners like you would do with an adapter

    0 讨论(0)
  • 2020-11-22 04:40

    HorizontialListView can't work when the data in the adapter is involved in another thread. Everything runs 100% on UI thread.This is a big problem in multithread. I think using HorizontialListView is not the best solution for your problem.HorzListView is a better way.You just replace your previous Gallery with HorzListView.You neednot modify the code about the adapter.Then everything goes the way you hope.See https://stackoverflow.com/a/12339708/1525777 about HorzListView.

    0 讨论(0)
  • 2020-11-22 04:42

    @Paul answer links to a great solution, but the code doesn't allow to use onClickListeners on items children (the callback functions are never called). I've been struggling for a while to find a solution and I've decided to post here what you need to modify in that code (in case somebody need it).

    Instead of overriding dispatchTouchEvent override onTouchEvent. Use the same code of dispatchTouchEvent and delete the method (you can read the difference between the two here http://developer.android.com/guide/topics/ui/ui-events.html#EventHandlers )

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

    Then, add the following code which will decide to steal the event from the item children and give it to our onTouchEvent, or let it be handled by them.

    @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);
        }
    }
    

    Finally, don't forget to declare the variables in your class:

    private float mInitialX;
    private float mInitialY;
    
    0 讨论(0)
提交回复
热议问题