gpt4 book ai didi

java - Android-OnClick 在 gridview 行内

转载 作者:行者123 更新时间:2023-12-02 02:47:13 25 4
gpt4 key购买 nike

我正在尝试从 gridview 项目内的按钮启动一个 Activity ,这意味着当用户单击 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 a image of the above layout

<小时/>

这是我的适配器getview,我从中尝试执行 Intent/操作;

@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布局(我尝试过可聚焦和可点击)

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

关于java - Android-OnClick 在 gridview 行内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44407057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com