【问题标题】:On click item in gridview在gridview中单击项目
【发布时间】:2014-06-28 23:37:38
【问题描述】:

当一个 .xml 文件代表网格的一般视图而另一个 .xml 文件代表该网格内的自定义项目时,如何正确实现项目单击侦听器?

概览fragment_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:numColumns="2"
/>

以及图片和文字视图的项目视图gridview_item.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.myapp.gridview.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"

    />
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:layout_gravity="bottom"
    android:textColor="@android:color/white"
    android:background="#55000000"
    />
</FrameLayout>

需要在哪里写android:onClick="" - 在fragment_maingridview_item?是否还有其他需要启用的 gridview 属性?

感谢您的建议。

【问题讨论】:

  • 使用gridview_item.setOnItemClickListener

标签: android xml gridview layout onclicklistener


【解决方案1】:

如果您在该类中处理BaseAdpaterArrayAdapter,您将拥有返回View 对象的getView 方法,您只需在该视图上写一个setOnClickListerner(converview)

看看下面的代码可能对你有帮助

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view=inflater.inflate(R.layout.group_row, null);
    view.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // your logic goes here 
       }
    });
    return view;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多