【问题标题】:How to implement on click listener on Grid Layout with Image View and Text View?如何在带有图像视图和文本视图的网格布局上实现点击监听器?
【发布时间】:2015-08-20 10:57:44
【问题描述】:

我想使用网格布局(不是网格视图),并且在每个单元格中我想要图像视图并在其下方有一个文本视图。我想在网格布局的每个单元格上实现单击侦听器。请有人告诉我该怎么做?

【问题讨论】:

  • 到目前为止你尝试过什么?您能提供您遇到问题的任何代码吗?
  • 在这里查看我的答案,这正是您想要的,如果有帮助,请投票给答案stackoverflow.com/a/31877898/2826147

标签: android android-layout android-intent


【解决方案1】:

假设您的单元格布局的最顶层包装器是RelativeLayout,下面是可能有效的代码:

GridLayout grid = (GridLayout) findViewById(R.id.your_layout_id);
int childCount = grid.getChildCount();

for (int i= 0; i < childCount; i++){
    RelativeLayout container = (RelativeLayout) grid.getChildAt(i);
    container.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            // your click code here
        }
    });
}

【讨论】:

  • 我使用的是线性布局而不是相对布局
  • 那又怎样?只需在我的代码中将 RelativeLayout 更改为 LinearLayout 即可。就这样。 RelativeLayout 仅用于示例。
  • 我很高兴它做到了。请投票接受答案。谢谢。
【解决方案2】:

我相信那么CardView最好用grid layout,在CardView里面可以放图片和文字,直接设置onClickListener。 例如

cardApplication.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,ApplicationList.class);
                    startActivity(intent);
                }
            });

其中 cardApplication 是我的CardView 在网格布局中的名称。

这就是我的CardView 的 xml 的样子:

<android.support.v7.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_columnWeight="1"
                        android:layout_rowWeight="1"
                        android:layout_marginBottom="16dp"
                        android:layout_marginLeft="16dp"
                        android:layout_marginRight="16dp"
                        app:cardElevation="8dp"
                        app:cardCornerRadius="8dp"
                        android:id="@+id/cardApplication"
                        android:clickable="true"
                        >
                        <LinearLayout
                            android:layout_gravity="center_horizontal|center_vertical"
                            android:layout_margin="16dp"
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content">
    
                            <ImageView
                                android:src="@drawable/ic_apps"
                                android:layout_gravity="center_horizontal"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />
                            <TextView
                                android:text="Applications"
                                android:textAlignment="center"
                                android:textColor="#000000"
                                android:textStyle="bold"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />
    
                        </LinearLayout>

【讨论】:

    【解决方案3】:
    gl=(GridLayout) findViewById(R.id.gl);
    
    
        int childCount = gl.getChildCount();
    
        for(int i=0;i<childCount;i++){
    
    
           LinearLayout l = (LinearLayout) gl.getChildAt(i);
            l.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
    
    
    
    
                        Intent i=new      Intent(MainActivity.this,SecondActivity.class);
                        startActivity(i);
    
    
                }
    
            });
    

    }

    【讨论】:

    • 你访问过我上面发布的链接吗?
    【解决方案4】:

    在布局的childview 上实现Clicklistener。 例如在Imagebutton.setClicklistener(this) 上,您可以对每个单元格做出反应 在布局中

    你也可以从 View() 类扩展你自己的砖类扩展 ImageButton

    然后直接在 Object 中构建自己的方法成员变量

    希望这会有所帮助 帕特里克·韦塔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-09
      • 2014-10-30
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多