水平滚动网格视图

Piy*_*ush 14 android scroll gridview

我知道在Android中不可能水平滚动网格视图.但我正在做的是在水平滚动视图中动态添加图像按钮,如下所示:

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());
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我将所有图像按钮都放在一行中.如何在类似网格的结构中实现它们?

Jes*_*ers 31

实现水平滚动GridView涉及将一些Android源代码类复制到您的代码库(AdapterView,AbsListView,GridView,ScrollBarDrawable)并添加代码来处理水平代码.这主要是复制一些代码并从上到下,从下到右等进行更改.必须复制而不是扩展的主要原因是这些类的最终性质.

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

  • 图像质量是您在gridview中放置的任何图像.gridview不会改变它. (3认同)

Ale*_*dam 11

您可以

  • 使用TableLayout内部a HorizontalScrollView,或
  • 保持你的方法与水平,LinearLayout但添加垂直LinearLayouts而不是直接图像.例如,LinearLayout纵向每纵向添加三到四个图像,并重绘以在横向中仅添加两个.

我会TableLayout首先尝试这种方法.

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

PS2:请记住,System.out它通常被重定向到/dev/null并因此丢失,因此我强烈建议您使用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);
}
Run Code Online (Sandbox Code Playgroud)

我使用一个非常简单的视图作为示例:

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);
}
}
Run Code Online (Sandbox Code Playgroud)

  • 使用这种方法时要小心.布局不处理视图的回收.放置布局中的许多项目,您可能会遇到内存不足错误.您应该寻找正确实现的水平列表/网格视图(使用适配器类的视图). (2认同)

abh*_*nav 11

有一个非常简单的技巧.

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

这可能对某些人有用!!


Hir*_*tel 7

我这样做了:

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>
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

添加以下方法:

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);
 }
Run Code Online (Sandbox Code Playgroud)

希望这会帮助你.