使用回收视图的Android textview可见性

时间:2017-06-13 16:31:57

标签: android android-recyclerview adapter visibility android-viewholder

我已经设置了一个应用程序,它使用来自SQL数据库的信息填充循环视图,例如日期和时间等...我最近设置了一个小部分,根据上面的文本视图是否相同,显示或隐藏文本视图。例如,它将使用13/06/2017填充第一个Textview并使其可见,如果它要显示的下一个日期相同,则它将简单地隐藏textview,允许具有相同日期的多个结果彼此堆叠。

问题 我的代码应该正常工作并正确堆叠。我遇到的问题是,当我向下滚动页面时,所有标题都会消失。这是因为我没有正确设置我的viewHolder。你能否就这种情况给我一些指导。

绑定视图

允许堆叠多个重复结果的代码

if (position - 1 >= 0) {

String currentDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));

cursor.moveToPosition(position - 1 );

String previousDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));


 Log.i("storedDates", currentDate);    

 if (currentDate.equalsIgnoreCase(previousDate)) {

 txtHeader.setVisibility(View.GONE);
 cardView.setVisibility(View.GONE);

 } else {


 txtHeader.setVisibility(View.VISIBLE);
 txtHeader.setText(currentDate);

 }

 } else {

 txtHeader.setVisibility(View.VISIBLE);
 txtHeader.setText(cursor.getString(cursor.getColumnIndexOrThrow("complete_date")));
}

启动ViewHolder

public static class Viewholder extends RecyclerView.ViewHolder{

  TextView header;
  public Viewholder(View itemView) {
       super(itemView);

       header = (TextView) itemView.findViewById(R.id.txtHeader);
    }
}

OncreateViewholder和OnBindViewHolder

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = mCursorAdapter.newView(mContext,mCursorAdapter.getCursor(), parent);

    return new Viewholder(view);



}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    mCursorAdapter.getCursor().moveToPosition(position);




    mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());

}






@Override
public int getItemCount() {
    return mCursorAdapter.getCount();
}

感谢任何建议

1 个答案:

答案 0 :(得分:0)

您缺少 cardView 可见性,因此在您设置 txtHeader 可见性的同时还设置 cardView 可见性

    if (position - 1 >= 0) {

       String currentDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));

       cursor.moveToPosition(position - 1 );

       String previousDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));


       Log.i("storedDates", currentDate);    

       if (currentDate.equalsIgnoreCase(previousDate)) {

          txtHeader.setVisibility(View.GONE);
          cardView.setVisibility(View.GONE);

        } else {

          cardView.setVisibility(View.VISIBLE);
          txtHeader.setVisibility(View.VISIBLE);
          txtHeader.setText(currentDate);

       }

     } else {
       cardView.setVisibility(View.VISIBLE);
       txtHeader.setVisibility(View.VISIBLE);
     txtHeader.setText(cursor.getString(cursor.getColumnIndexOrThrow("complete_date")));
    }