Question

How to implement on item click listener properly in such a case when one .xml file stands for general view of grid and another one for customized item within that grid?

general view 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"
/>

and view of Item cosists of picture and textview 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>

where is it needed to write android:onClick="" - in fragment_main or gridview_item? Are there any other needed properties of gridview to be enabled?

Thank for advices.

Était-ce utile?

La solution

if you are dealing with BaseAdpater or ArrayAdapter in that class you will be having getView method which returns View object you can simply write a setOnClickListerner on that view(converview)

look at the following code this may help you

    @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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top