小编典典

Android-在网格视图行内单击

java

我正在尝试从gridview项内的按钮启动活动,这意味着
当用户单击gridview项时,它应该执行一项功能,而当
用户单击gridview项内的按钮时,它应该执行另一项
功能,让我详细说明一下,这是我的单个项目布局,正在
我的gridview中填充;

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">

<RelativeLayout
    android:id="@+id/single_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingTop="10dp">

    <com.mikhaellopez.circularimageview.CircularImageView
        android:id="@+id/imgFood"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:civ_border_color="#ffffff"
        app:civ_border_width="2dp"
        app:civ_shadow="true"
        app:civ_shadow_color="#000000"
        app:civ_shadow_radius="10" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/imgFood">

    <TextView
        android:id="@+id/txtName"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="16sp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/currentid"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_below="@+id/txtName"
        android:text="Current ID : "
        android:visibility="visible"
        />

    <TextView
        android:id="@+id/studentid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ID"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="16sp"
        android:layout_below="@+id/txtName"
        android:layout_toRightOf="@+id/currentid"
        android:layout_marginEnd="30dp"
        android:layout_marginRight="30dp"
        />

    </RelativeLayout>

    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="end"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/editit"
        android:background="@android:drawable/btn_dialog"/>


</RelativeLayout>

here is how the above layout looks:

here is a image of the above
layout


and here is my adapter getview from where I try to do the intent/actions
from;

@Override
public View getView(int position, View view, ViewGroup viewGroup) {

    View row = view;
    ViewHolder holder = new ViewHolder();


    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layout, null);

        holder.txtName = (TextView) row.findViewById(R.id.txtName);
        holder.txtPrice = (TextView) row.findViewById(R.id.studentid);
        holder.imageView = (CircularImageView) row.findViewById(R.id.imgFood);
        holder.dit = (Button) row.findViewById(R.id.editit);

        final ViewHolder finalHolder = holder;
        holder.dit.setOnClickListener(new View.OnClickListener() {

            String getname = finalHolder.txtName.getText().toString();
            String gethandicap = finalHolder.txtPrice.getText().toString();

            @Override
            public void onClick(View v) {
                // Do something
                Intent editintent = new Intent(context, MainActivity.class);
                editintent.putExtra("studentname", getname);
                editintent.putExtra("studentid", getID);
                context.startActivity(editintent);
            }
        });

        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    Sudentdb student = studentsList.get(position);

    holder.txtName.setText(student.getName());
    holder.txtPrice.setText(student.getID());


    if (student.getImage() != null && student.getImage().length > 0) {
        Glide.with(context)
                .load(student.getImage())
                .into(holder.imageView);
    } else {
        holder.imageView.setImageBitmap(null);
    }

    return row;

}

我遇到的问题是gridview项内的按钮未
执行任何操作,在日志中它仅表示ACTION_DOWN并且由于我
在项内添加了按钮,因此onclick项也不起作用。

编辑; 添加GRIDVIEW的单击和布局

gridView = (GridView) findViewById(R.id.gridView);
    list = new ArrayList<>();
    adapter = new StudentListAdapter(this, R.layout.food_items, list);
    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            String dataFromGrid;
            dataFromGrid = ((TextView)(view.findViewById(R.id.txtName))).getText().toString();


            Intent i = new Intent(getApplicationContext(), Student.class);

            i.putExtra("unfromstudent",dataFromGrid);
            startActivity(i);

gridview layout (I have tried focusable and clickable)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
     >

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:id="@+id/gridView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:columnWidth="120dp"
        android:gravity="center"
        android:layout_margin="2dp"
        android:numColumns="1"

        />

</RelativeLayout>

阅读 223

收藏
2020-11-30

共1个答案

小编典典

改变你XML,我已经在下面测试了xml它的工作原理GridView

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    android:orientation="horizontal"
    app:cardCornerRadius="5dp"
    app:cardElevation="3dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:orientation="horizontal">


        <com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/imgFood"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            app:civ_border_color="#ffffff"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="#000000"
            app:civ_shadow_radius="10" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_weight="1"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="5dp"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="16sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/currentid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txtName"
                    android:layout_marginLeft="5dp"
                    android:layout_marginStart="5dp"
                    android:text="Current ID : "
                    android:visibility="visible" />

                <TextView
                    android:id="@+id/studentid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txtName"
                    android:layout_marginEnd="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_toRightOf="@+id/currentid"
                    android:text="ID"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textSize="16sp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="30dp"
            android:layout_height="30dp">

            <Button
                android:id="@+id/editit"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="end"
                android:background="@android:drawable/btn_dialog"
                android:focusable="false" />


        </LinearLayout>

    </LinearLayout>


</android.support.v7.widget.CardView>

您的Grid View XML将如下所示。

<GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true">

    </GridView>
2020-11-30