Horizontal scrolling in android gridview

后端 未结 6 1337
闹比i
闹比i 2021-01-04 22:40

I have a grid view in my application and i need to scroll it horizontally.I have tried changing the gridview to gallery.But then only one row is available,but i need differe

相关标签:
6条回答
  • 2021-01-04 23:09

    I have already posted this answer here and here, but these questions are identical...


    There is a nice solution in Android from now on : HorizontalGridView.

    1. Gradle dependency

    dependencies {
        compile 'com.android.support:leanback-v17:23.1.0'
    }
    

    2. Add it in your layout

    your_activity.xml

    <!-- your stuff before... -->
            <android.support.v17.leanback.widget.HorizontalGridView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:id="@+id/gridView"
                />
    <!-- your stuff after... -->
    

    3. Layout grid element

    Create a layout for your grid element ( grid_element.xml ). I have created a simple one with only one button in it.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button" />
    </LinearLayout>
    

    4. Create an adapter

    Highly inspired by this link : https://gist.github.com/gabrielemariotti/4c189fb1124df4556058

    public class GridElementAdapter extends RecyclerView.Adapter<GridElementAdapter.SimpleViewHolder>{
    
        private Context context;
        private List<String> elements;
    
        public GridElementAdapter(Context context){
            this.context = context;
            this.elements = new ArrayList<String>();
            // Fill dummy list
            for(int i = 0; i < 40 ; i++){
                this.elements.add(i, "Position : " + i);
            }
        }
    
        public static class SimpleViewHolder extends RecyclerView.ViewHolder {
            public final Button button;
    
            public SimpleViewHolder(View view) {
                super(view);
                button = (Button) view.findViewById(R.id.button);
            }
        }
    
        @Override
        public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final View view = LayoutInflater.from(this.context).inflate(R.layout.grid_element, parent, false);
            return new SimpleViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(SimpleViewHolder holder, final int position) {
            holder.button.setText(elements.get(position));
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "Position =" + position, Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public int getItemCount() {
            return this.elements.size();
        }
    }
    

    5. Initialize it in your activity :

    private HorizontalGridView horizontalGridView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity);
        horizontalGridView = (HorizontalGridView) findViewById(R.id.gridView);
        GridElementAdapter adapter = new GridElementAdapter(this);
    
        horizontalGridView.setAdapter(adapter);
    }
    
    0 讨论(0)
  • 2021-01-04 23:11

    I think your best bet is to use a Gallery and implement an adapter that provides a multirow view in its getView method. See Hello Gallery and look at the ImageAdapter implementation within.

    Instead of the ImageView that getView returns in that example, you can, for example, inflate your own custom layout, for example a LinearLayout with vertical orientation.

    You might consider a TableLayout inside a HorizontalScrollView, but the disadvantage there is that everything will be in memory at once, making it difficult to scale to lots of items. The Gallery recycles Views and offers some resource advantages.

    0 讨论(0)
  • 2021-01-04 23:12

    Try to wrap it in HorizontalScrollView

    0 讨论(0)
  • 2021-01-04 23:14

    True about using a Gallery or ListView re: re-using views. But if your list is not too long, you can try a TableLayout with a ViewFlipper. Here's a summary of possible options/solutions.

    0 讨论(0)
  • 2021-01-04 23:19

    how about using a viewPager :

    http://developer.android.com/reference/android/support/v4/view/ViewPager.html

    ?

    0 讨论(0)
  • 2021-01-04 23:20

    This post might get help you out what you wanted to achieve

    Scroll like Shelf View

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