水平滚动网格视图

| 我知道在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());
            }
        }
    }
}
但我将所有图像按钮都放在一行中。如何在类似网格的结构中实现它们?
已邀请:
您可以 在
HorizontalScrollView
中使用
TableLayout
,或者 保持水平
LinearLayout
不变,但增加垂直
LinearLayout
而不是直接添加图像。例如,纵向每3ѭ纵向添加三到四个图像,然后重新绘制以横向仅添加两个。 我将首先尝试“ 1”法。 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);
}
我以一个非常简单的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
有一个很简单的把戏。 将网格视图旋转270度,并将列数设置为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中有一个不错的解决方案: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)创建一个布局。我创建了一个只有一个按钮的简单按钮。
<?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.创建一个适配器 受到此链接的极大启发: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并将其gridlayout设置为布局管理器并将其设置为水平滚动
your recycle view.setLayoutManager(new GridLayoutManager(getActivity(),2, LinearLayoutManager.HORIZONTAL, false))
这里2是网格的列跨度

要回复问题请先登录注册