水平滚动网格视图

我知道这是不可能的Android横向滚动网格视图。 但我所做的是在水平滚动视图中dynamic添加图像button,如下所示:

public class HorizontalScroller extends Activity { static int l=0; private Rect mTempRect = new Rect(); static int r1=0; static int t=0; static int b=0; static int x=0; static int y=0; //Button[] b1 = new Button[100]; ImageButton btn[][] = new ImageButton[10][10]; //ImageButton b1 = new ImageButton(this); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout rl = (LinearLayout)findViewById(R.id.widget92); LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); for(int i=0;i<4;i++) { for(int j=0;j<10;j++) {System.out.println("helo"); /* l=l+100; r1=r1+100; t=t+100; b=b+100;*/ //button(); //ImageButton btn=new ImageButton(this); /* Rect r = mTempRect; r.left=10; r.top=10; r.right=10; r.bottom=10; btn[i][j].getDrawingRect(r);*/ //btn[i][j].setId(j); Rect r = mTempRect; r.set(0,0,0,0); Rect r2 = mTempRect; r2.set(0,20,0,20); btn[i][j]=new ImageButton(this); btn[i][j]. setBackgroundResource(R.drawable.icon); btn[i][j].setMinimumWidth(20); btn[i][j].setMinimumHeight(20); params1.setMargins(5, 5, 5,5); rl.addView(btn[i][j],params1); System.out.println("1="+btn[i][j].getTop()); System.out.println("2="+btn[i][j].getLeft()); System.out.println("3="+btn[i][j].getRight()); System.out.println("4="+btn[i][j].getBottom()); } } } } 

但我得到的所有图像button在一个单一的行。 我怎样才能实现他们像一个网格结构?

您可以

  • HorizontalScrollView使用TableLayout ,或者
  • 保持你的方法与水平LinearLayout但添加垂直LinearLayout s,而不是直接的图像。 例如,每个垂直LinearLayout在纵向上添加三到四个图像,并重新绘制以在横向上添加两个。

我会先尝试TableLayout方法。

PS1:下次尝试删除所有不相关的代码(代码越less,越容易理解你所做的)。

PS2:请记住, System.out通常会redirect到/dev/null ,因此我强烈build议您使用Log.d

完整的例子

适应这个onCreate()方法或任何你需要它:

 public void horizontalScrollGalleryLayout () { HorizontalScrollView sv = new HorizontalScrollView(this); LinearLayout llh = new LinearLayout(this); llh.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(40, 40); LinearLayout.LayoutParams layoutParamsLL = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); for (int i=0; i<20; i++) { LinearLayout llv = new LinearLayout(this); llv.setOrientation(LinearLayout.VERTICAL); TestView testView1 = new TestView(this, Color.rgb(i*12, 0, 0)); TestView testView2 = new TestView(this, true, Color.rgb(i*12, i*12, 0)); TestView testView3 = new TestView(this, true, Color.rgb(0, i*12, 0)); llv.addView(testView1, layoutParamsTV); llv.addView(testView2, layoutParamsTV); llv.addView(testView3, layoutParamsTV); llh.addView(llv, layoutParamsLL); } sv.addView(llh, layoutParamsLL); setContentView(sv); } 

我用一个非常简单的View作为例子:

 public class TestView extends View { Context context; int color; public TestView(Context context, int color) { super(context); this.context = context; this.color = color; } @Override public void onDraw (Canvas canvas) { super.onDraw(canvas); this.setBackgroundColor(Color.LTGRAY); Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG); paint.setColor(color); canvas.drawCircle(20, 20, 20, paint); } } 

实现一个水平滚动的GridView涉及到将一些Android源代码类复制到您的代码库(AdapterView,AbsListView,GridView,ScrollBarDrawable)并添加代码来处理横向代码。 这主要是复制一些代码,从顶部到左侧,底部到右侧等。不得不复制而不是扩展的主要原因是这些类的最终性质。

前一段我实现了一个水平滚动的GridView,最后到达推送到github: https : //github.com/jess-anders/two-way-gridview

有一个非常简单的伎俩。

  1. 将网格视图旋转270度,并将列数设置为2。
  2. 将每个项目旋转90度(以便项目显示为原始方向)。

这可能对一些有用!

我这样做了:

activity_main.xml

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

MainActivity.java

 GridView gridView = (GridView) findViewById(R.id.gridView); gridView.setNumColumns(arrayList.size()); GridViewAdapter gridViewAdapter = new GridViewAdapter(mContext, arrayList); gridView.setAdapter(gridViewAdapter); // Set dynamic width of Gridview setDynamicWidth(gridView); 

以下添加方法

 private void setDynamicWidth(GridView gridView) { ListAdapter gridViewAdapter = gridView.getAdapter(); if (gridViewAdapter == null) { return; } int totalWidth; int items = gridViewAdapter.getCount(); View listItem = gridViewAdapter.getView(0, null, gridView); listItem.measure(0, 0); totalWidth = listItem.getMeasuredWidth(); totalWidth = totalWidth*items; ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.width = totalWidth; gridView.setLayoutParams(params); } 

希望这会帮助你。

我已经在这里发布了这个答案,但是两个问题都是相同的。


从现在开始,Android中有一个很好的解决scheme: Horizo​​ntalGridView 。

1. Gradle依赖

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

2.将其添加到您的布局

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.布局网格元素

为您的网格元素( grid_element.xml )创build一个布局。 我创build了一个简单的只有一个button。

 <?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.创build一个适配器

受此链接高度启发: 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.在你的活动中初始化它:

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

使用recyclerview将其网格布局设置为布局pipe理器,并将其设置为水平滚动

 your recycle view.setLayoutManager(new GridLayoutManager(getActivity(),2, LinearLayoutManager.HORIZONTAL, false)) 

这里2是网格的列跨度