【发布时间】:2015-02-25 09:32:50
【问题描述】:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:background="@android:color/transparent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/string_for_newsletter"
android:paddingLeft="16dp"
android:id="@+id/textQuestion"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bookmark_star"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:layout_alignParentBottom="true"
android:id="@+id/bookmarkButton"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textQuestion"
android:text="answer"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:textColor="#00ff00"
android:visibility="gone"
android:id="@+id/textAnswer"/>
</RelativeLayout>
这是我的卡片视图布局 以下是 RecyclerView Adapter 的代码 包 com.example.user_2.tcc_app.QuestionAnswer;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.user_2.tcc_app.R;
/**
* Created by USER-2 on 23-Feb-15.
*/
public class QuestionAnswerAdapter extends RecyclerView.Adapter<QuestionAnswerAdapter.QuestionAnswerAdapterViewHolder>{
int length;
int layout_id;
public long item_id;
public QuestionAnswerAdapter(int length, int id_for_layout){
this.length = length;
this.layout_id = id_for_layout;
}
@Override
public QuestionAnswerAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View root = LayoutInflater.from(parent.getContext()).inflate(layout_id, parent, false);
QuestionAnswerAdapterViewHolder questionAnswerAdapterViewHolder = new QuestionAnswerAdapterViewHolder(root);
return questionAnswerAdapterViewHolder;
}
@Override
public void onBindViewHolder(final QuestionAnswerAdapterViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return length;
}
public class QuestionAnswerAdapterViewHolder extends RecyclerView.ViewHolder{
TextView textQuestion;
ImageButton bookmarkButton;
TextView textAnswer;
public QuestionAnswerAdapterViewHolder(View v){
super(v);
textQuestion = (TextView)v.findViewById(R.id.textQuestion);
bookmarkButton = (ImageButton)v.findViewById(R.id.bookmarkButton);
textAnswer = (TextView)v.findViewById(R.id.textAnswer);
textQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textAnswer.setVisibility(View.GONE);
}
});
}
}
}
我在回收站视图中有 10 个项目。我想在单击 textQuestion 时显示带有 id textAnswer 的文本视图,并且到目前为止一切正常,但是当我向下滚动时,我可以看到第九个项目也具有可见的 textAnswer 字段。我可以理解这是由于 onBindViewHolder 方法作为 recyclerView 而发生的,而回收它的项目使用以前可见的项目,这些项目不再可见。但我不知道如何解决它。有人请帮忙
【问题讨论】:
-
对于 onclickListener 中的 textAnswer.setVisibility(View.Visible) 的愚蠢错误,我深表歉意
标签: android android-recyclerview