我目前正在实施点击Android应用中RecyclerView
的项目的功能。然而,我遇到了一些奇怪的错误,我只能注册所有项目的点击次数,而不是第一次。
这是我对每个项目的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:onClick="listItemClick">
<TextView
android:id="@+id/spacer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text=" "
android:textSize="8sp" />
<TextView
android:id="@+id/headerTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/spacer2"
android:text="[Auftraggeber] - [Ort]"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textSize="18sp" />
<TextView
android:id="@+id/captionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/headerTextView"
android:text="Erstellt am 21.05.1999" />
<TextView
android:id="@+id/spacer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/captionTextView"
android:text=" "
android:textSize="8sp" />
</RelativeLayout>
我在listItemClick
中使用MainActivity
以便在点击某个项目时执行代码。还有更多我通过其中一个文本视图的标记传递数据(我认为这不是问题)。
这是listItemClick()
:
public void listItemClick (View v){
Toast.makeText(getApplicationContext(), ("You clicked: " + v.findViewById(R.id.headerTextView).getTag()), Toast.LENGTH_LONG).show();
System.out.println("Clicked");
}
这是我为RecyclerView实现的适配器:
public class RapportListAdapter extends RecyclerView.Adapter<RapportListAdapter.ViewHolder> {
private ArrayList<RapportStructure> mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
public RelativeLayout mRelativeLayout;
public ViewHolder(RelativeLayout v) {
super(v);
mRelativeLayout = v;
}
@Override
public void onClick(View view) {
System.out.println("WORKS");
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public RapportListAdapter(ArrayList<RapportStructure> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public RapportListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
RelativeLayout rl = (RelativeLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.celllayout, parent, false);
ViewHolder vh = new ViewHolder(rl);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
TextView headerTextView = (TextView) holder.mRelativeLayout.findViewById(R.id.headerTextView);
TextView captionTextView = (TextView) holder.mRelativeLayout.findViewById(R.id.captionTextView);
RapportStructure rs = this.mDataset.get(position);
headerTextView.setText("ClientID:" + rs.getClientId() + " - " + rs.getPlace());
captionTextView.setText("Erstellt am " + rs.getCreatedOn().toString());
headerTextView.setTag(position);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}
我希望第一个列表项也可以点击。如果您需要更多信息,请询问:)
答案 0 :(得分:0)
这个bug似乎已经自杀了。我不知道为什么。我现在可以使用第一个元素打开一个新的Activity
。谢谢你的帮助!
答案 1 :(得分:0)
我也有当前问题。 删除动画后,我的问题解决了。 ):