- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直试图在单击 TextView 时显示一个选项菜单。我已经能够做到这一点。但是现在,当我单击该行时,我无法进入下一个 Activity 。
这是我在 MainActivity 中的代码
//On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, final int position) {
TextView buttonViewOption = (TextView) view.findViewById(R.id.textViewOptions);
TextView rowItem = (TextView) view.findViewById(R.id.rowItem);
buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(MainActivity.this, buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
//handle menu1 click
break;
case R.id.menu2:
break;
case R.id.menu3:
showLeagueDialog(true, leaguesList.get(position), position);
break;
case R.id.menu4:
deleteLeague(position);
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
/*int leagueId = leaguesList.get(position).getId();
Intent myIntent = new Intent(MainActivity.this, BowlerActivity.class);
myIntent.putExtra("leagueId", leagueId);
startActivity(myIntent);
overridePendingTransition(0, 0);*/
@Override
public void onRowClick(View view, int position) {
int leagueId = leaguesList.get(position).getId();
Intent myIntent = new Intent(MainActivity.this, BowlerActivity.class);
myIntent.putExtra("leagueId", leagueId);
startActivity(myIntent);
overridePendingTransition(0, 0);
}
@Override
public void onLongClick(View view, int position) {
showActionsDialog(position);
}
}));
}
RecyclerTouchListener.java
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private ClickListener clicklistener;
private GestureDetector gestureDetector;
public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener) {
this.clicklistener = clicklistener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recycleView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clicklistener != null) {
clicklistener.onLongClick(child, recycleView.getChildAdapterPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clicklistener != null && gestureDetector.onTouchEvent(e)) {
clicklistener.onClick(child, rv.getChildAdapterPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onRowClick(View view, int position);
void onLongClick(View view, int position);
}
}
我在该网站上搜索了许多不同的文章,并且尝试了成员发布的几种不同的解决方案,但我仍然无法进行新的 Activity 。
如何创建一个 onClickListener() 来监听主行上的点击以及 TextView。
任何帮助将不胜感激。
BolwerAdapter.java
public class BowlerAdapter extends RecyclerView.Adapter<BowlerAdapter.MyViewHolder> {
private List<Bowler> bowlersList;
public void notifyDatasetChanged(List<Bowler> newbowlerlist) {
bowlersList.clear();
bowlersList.addAll(newbowlerlist);
super.notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView bowlerLeagueId;
private TextView name;
private TextView bowlerAverage;
private TextView bowlerHandicap;
private TextView timestamp;
MyViewHolder(View view) {
super(view);
bowlerLeagueId = view.findViewById( R.id.tvLeagueId);
name = view.findViewById(R.id.tvBowlerName);
bowlerAverage = view.findViewById(R.id.tvBowlerAverage);
bowlerHandicap = view.findViewById(R.id.tvBowlerHandicap);
timestamp = view.findViewById(R.id.timestamp);
}
}
BowlerAdapter(Context context, List<Bowler> bowlersList) {
this.bowlersList = bowlersList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from( Objects.requireNonNull( parent ).getContext())
.inflate(R.layout.listview_bowler, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Bowler bowler = bowlersList.get(position);
holder.bowlerLeagueId.setText(bowler.getLeagueId());
holder.name.setText(bowler.getName());
if (bowler.getAverage() != "") {
holder.bowlerAverage.setText(String.format("Bowler Avg: %s", bowler.getAverage()));
} else {
holder.bowlerAverage.setText(String.format("Bowler Avg: %s", "0"));
}
Integer handicap = Integer.parseInt(bowler.getHandicap());
if (handicap < 0) {
holder.bowlerHandicap.setText(String.format("Bowler Handicap: %s", "0"));
}
if (bowler.getHandicap() != "" ){
holder.bowlerHandicap.setText(String.format("Bowler Handicap: %s", bowler.getHandicap()));
} else {
holder.bowlerHandicap.setText(String.format("Bowler Handicap: %s", "0"));
}
//Formatting And Displaying Timestamp
holder.timestamp.setText(formatDate(bowler.getTimestamp()));
}
@Override
public int getItemCount() {
return bowlersList.size();
}
//Formatting TimeStamp to 'EEE MMM dd yyyy (HH:mm:ss)'
//Input : 2018-05-23 9:59:01
//Output : Wed May 23 2018 (9:59:01)
private String formatDate(String dateStr) {
try {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = fmt.parse(dateStr);
SimpleDateFormat fmtOut = new SimpleDateFormat("EEE MMM dd yyyy (HH:mm:ss)");
return fmtOut.format(date);
} catch (ParseException ignored) { }
return "";
}
}
我已经能够通过设置 onClick() 在 TextView 上激活来使其工作。这并不理想,因为我似乎必须双击文本才能切换 Activity 。
我在MainActivity中的代码
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onRowClick(View view, int position) {
TextView listviewClick = (TextView) view.findViewById(R.id.tvSeriesName);
listviewClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int leagueId = leaguesList.get(position).getId();
Intent myIntent = new Intent(MainActivity.this, BowlerActivity.class);
myIntent.putExtra("leagueId", leagueId);
startActivity(myIntent);
overridePendingTransition(0, 0);
}
});
}
@Override
public void onClick(View view, final int position) {
TextView buttonViewOption = (TextView) view.findViewById(R.id.textViewOptions);
buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(MainActivity.this, buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
//handle menu1 click
break;
case R.id.menu2:
break;
case R.id.menu3:
showLeagueDialog(true, leaguesList.get(position), position);
break;
case R.id.menu4:
deleteLeague(position);
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
@Override
public void onLongClick(View view, int position) {
showActionsDialog(position);
}
}));
}
我的 ListView xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
android:id="@+id/rowItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:paddingBottom="@dimen/dimen_10"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/dimen_10">
<me.grantland.widget.AutofitTextView
android:id="@+id/tvSeriesName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:singleLine="true"
android:text="@string/leagueValue"
android:textColor="?attr/colorAccent"
android:textSize="24sp"
android:textStyle="bold"
autofit:minTextSize="16sp" />
<TextView
android:id="@+id/tvLeagueAverage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/tvSeriesName"
android:layout_marginStart="0dp"
android:text="League Average: 300"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="@+id/timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvSeriesName"
android:layout_centerHorizontal="true"
android:text="Fri May 18 2018"
android:textColor="?attr/colorText1"
android:textSize="10sp"
android:visibility="gone" />
<TextView
android:id="@+id/tvLeagueId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/tvSeriesName"
android:layout_below="@+id/timestamp"
android:text="TextView"
android:textColor="?attr/colorText1"
android:visibility="gone" />
<TextView
android:id="@+id/tvBaseScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="?attr/colorText1"
android:visibility="gone" />
<TextView
android:id="@+id/tvBaseScorePercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="?attr/colorText1"
android:visibility="gone" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:background="?attr/colorAccent" />
<TextView
android:id="@+id/textViewOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:text="⋮"
android:textAppearance="?android:textAppearanceLarge" />
</RelativeLayout>
但是当我使用RelativeLayout listviewClick = view.findViewById(R.id.rowItem);我只能单击该行,而不能单击选项菜单。
最佳答案
解决方案:
删除recyclerview.onTouchListener...
..我们不再需要这个了。
点击 holder
上的监听器对象的效果比这更好。请参阅下面的示例:
类似于holder.bowlerLeagueId.setText(bowler.getLeagueId());
你也可以写:
holder.bowlerLeagueId.setOnClickListener(....) {
........ (Write here)
}
然后,在您的(在此处写入)内容中编写代码以导航到下一个 Activity 。
同样,您可以对任何holder.viewid.setOnClick 等执行此操作。
希望有帮助。
关于javascript - 单击行时无法转到新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52263797/
我对 Android 很陌生,如果问题重复,请避免并发送链接。有三个 Activity A、B 和 C。 Activity A 获取一个用户名,我想在 Activity C 中显示该用户名,但我想先运
我正在尝试制作记事本应用程序,因此每次打开新笔记时,布局都会相同。另外, Activity 的数量(新注释)不应定义得尽可能多 最佳答案 如果 Activity 始终相同,您可能应该创建一个适配器,允
我有 3 个 Activity 。 主窗口 5 个按钮 在按钮的主窗口中按下此窗口打开(将其称为父窗口) 在父窗口按钮上按下此窗口打开调用它作为结束子窗口。 现在从子窗口我从父窗口获取值如下:
我遇到了一个 Activity backstack 问题。假设我的后台有 5 个 Activity :比如 Activity A、 Activity B、 Activity C、 Activity D
我正在寻找必须具有以下附加特征的 JMS 提供程序: 采用多代理,所有代理都必须处于事件状态(无单点故障) 仅在两台机器上进行扩展就足以满足我们的需求 能够保证订购(如果 1 个生产者 + 1 个消费
假设,我有一个由 TabHost 组成的选项卡 Activity 。 TabHost 包含 2 个选项卡,每两个选项卡都有一个 Activity 组。每个 Activity 组包含一项 Activit
我正在开发一个应用程序,我需要根据某些操作导航到特定 Activity 。这是一张图片 我的第一个 Activity 是 ReadingActivity。基于某些操作,用户将被带到 NewProjec
我创建了一个与服务器异步通信的应用程序。当应用程序发出服务器请求时,将创建一个带有“正在加载”通知的新对话框( Activity )。主要 Activity 实现了处理服务器响应的方法,我想在主要 A
我想在我的所有应用程序 Activity 中显示相同的选项菜单。我创建了一个实现菜单的通用 Activity ,并且我所有的进一步 Activity 都扩展了它。 问题:当我需要扩展其他特定 Acti
我有四个 Activity ,即 java 文件 - Activity1.java、activity2.java、activity3.java、activity4.java 和 xml 文件 - Ac
我有两个 Activity 。我想将数据从第二个 Activity 发送到上一个 Activity 。第一个 Activity 有自定义 ListView 和 bean 类。当我点击第二个 Activ
根 Activity 是堆栈中当前的第一个 Activity 还是 list 中指定为启动 Activity 的 Activity ? 支持应用程序 P 在启动时启动 Activity A。然后 A
你好 我想知道您在绘制 Activity 图选择“Activity ”时考虑了哪些关键点? 您如何从要建模的问题中选择 Activity ? 谢谢 最佳答案 Activity 图用于对正在开发的系统和
如何从主 Activity 启动 Activity 并在子 Activity 返回主 Activity 中退出操作后返回主 Activity ? 我已将子 Activity 作为启动器 Intent
我的工作流程如下: 登录 Activity -> ActivityB -> ActivityC -> ActivityD 我想将数据从LoginActivity传递到ActivityD,但不直接传递到
我之前曾尝试获得此问题的答案,但找不到可以解决我的问题的答案。我正在制作保存圆盘高尔夫球分数的应用程序。我的 MainActivity 有 4 个按钮。新比赛、恢复比赛、类(class)和球员。 At
我有一个 tts 非 UI 类和 Activity 类。现在在 Activity 类中,我有一个按钮,用户可以从中选择男声或女声,具体取决于我想要将字符串传递给 tts 类的选择,然后一次tts 类根
问题有点复杂,首先, Activity A 和 Activity B 的 list 中都有 android:noHistory = true 。我有一个自定义 serialized 类,假设 MyCl
在我的应用程序中,我有两个 Activity (AuthenticationActivity 和 MainActivity),每个 Activity 都有一个导航图和大量 fragment 。我创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How can i use compose email activity in tabView? 我想在选项
我是一名优秀的程序员,十分优秀!