Horizontal ListView in Android?

后端 未结 19 887
深忆病人
深忆病人 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:19

    I had to do the same for one of my projects and I ended up writing my own as well. I called it HorzListView is now part of my open source Aniqroid library.

    http://aniqroid.sileria.com/doc/api/ (Look for downloads at the bottom or use google code project to see more download options: http://code.google.com/p/aniqroid/downloads/list)

    The class documentation is here: http://aniqroid.sileria.com/doc/api/com/sileria/android/view/HorzListView.html

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

    I had used horizontal listview link in my project & I got good results. I had been used devsmart library initially but it gave me some issues. So best way to use horizontal listview link as it recovered my issues & also I recently launched my app on Google PlayStore using this library & got nice response from users. So I recommend you to use the same library which I mentioned above to show listview horizontally. Enjoy :)

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

    Since Google introduced Android Support Library v7 21.0.0, you can use RecyclerView to scroll items horizontally. The RecyclerView widget is a more advanced and flexible version of ListView.

    To use RecyclerView, just add dependency:

    com.android.support:recyclerview-v7:23.0.1
    

    Here is a sample:

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

    More info about RecyclerView:

    • https://developer.android.com/training/material/lists-cards.html
    • https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
    0 讨论(0)
  • 2020-11-22 04:20

    This isn't much of an answer, but how about using a Horizontal Scroll View?

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

    This is a little (very) late, but I'm posting this in case someone comes by this later.

    The Support Library as of the Android L preview has a RecyclerView that does exactly what you want.

    Right now, you can only get it through the L preview SDK and you need to set your minSdk to L. But you can copy all of the necessary files into your project and use them that way until L is officially out.

    You can download the preview docs here.

    Warning: The API for Recycler View may change and it may have bugs.

    Updated

    The source code for horizontal listview is:

    LinearLayoutManager layoutManager
        = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    
    RecyclerView myList = findViewById(R.id.my_recycler_view);
    myList.setLayoutManager(layoutManager);
    
    0 讨论(0)
  • 2020-11-22 04:26

    My solution is to simply use ViewPager widget. It isn't center-locked as Gallery and has a built-in features for recycling views (as ListView). You may see similar approach at Google Play app, whenever you deal with horizontally scrollable lists.

    You just need to extend PagerAdapter and perform a couple of tweaks there:

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

    As result, you'll have horizontally scrollable widget with adapter, like this: enter image description here

    0 讨论(0)
提交回复
热议问题