gpt4 book ai didi

javascript - 单击行时无法转到新 Activity

转载 作者:行者123 更新时间:2023-12-01 01:42:48 25 4
gpt4 key购买 nike

我一直试图在单击 TextView 时显示一个选项菜单。我已经能够做到这一点。但是现在,当我单击该行时,我无法进入下一个 Activity 。

enter image description here

这是我在 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="&#8942;"
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/

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