Android - GridView行内的OnClick [英] Android - OnClick inside gridview row

查看:113
本文介绍了Android - GridView行内的OnClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的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 =horizo​​ntal
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 =当前ID:
android:visibility =visible
/>

TextView $ b $ android 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>






这里是上述布局的样子:






这里是我的适配器 getview ,我从中尝试从intent / actions ;

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

查看行=查看;
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.dit.setOnClickListener(new View.OnClickListener(){
$ b $ String getname = finalHolder.txtName.getText()。toString();
String gethandicap = finalHolder.txtPrice .getText()。toString();

@Override
public void onClick(View v){
//做某事
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());

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

返回行;

}

我遇到的问题是gridview项目没有执行任何操作,只记录 ACTION_DOWN ,因为我在项目中添加了一个按钮,onclick项目也不起作用。






编辑;添加GRIDVIEW ONCLICK和布局

  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 $ b $ 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布局(我已经尝试过focusable和clickable)

 <?xml version =1.0encoding =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_centerHorizo​​ntal =true
android:columnWidth = 120dp
android:gravity =center
android:layout_margin =2dp
android:numColumns =1

/>

< / RelativeLayout>


解决方案


c $ c> 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 =horizo​​ntal
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:取向= 水平 >


< 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/>

$ b< LinearLayout
android:layout_width =match_parent
android:layout_height =wrap_content
android:layout_gravity =center_horizo​​ntal | center_vertical
android:layout_weight =1
android:gravity =center_horizo​​ntal | 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 =horizo​​ntal>

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 =当前ID:
android:visibility =visible/>

TextView $ b $ android 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>




您的Grid View XML将会像这样。




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

< / GridView>


I'm trying to launch a activity from a button inside my gridview item, meaning that when user click on gridview Item it should do one function and when the user click on the button inside the gridview item it should do another function, let me elaborate, here is my single item layout that is being populated inside of my 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:


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;

}

The problem I'm having is that the button inside the gridview item is not performing any actions, in log it only says ACTION_DOWN and since I added a button inside the item the item onclick also doesn't work.


EDIT; ADDING GRIDVIEW ONCLICK AND LAYOUT

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>

解决方案

Change you XML, i have tested below xml its works fine with 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>

Your Grid View XML will be Like this.

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

    </GridView>

这篇关于Android - GridView行内的OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆