我有一个RecycleView
我对单元格有三种不同视图的地方。RView
首次加载时数据显示完美。当我开始滚动时,它会改变视图颜色。尝试了不同的方法,但无法解决。
原来的看法。
错误的看法
public class TimelineRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int DATE_LAYOUT = 1;
private static final int EVEN_LAYOUT = 2;
private static final int ODD_LAYOUT = 3;
private List<TimeLineResponseDO.content> contents = new ArrayList<>();
Context context;
LayoutInflater layoutInflater;
private boolean isVisible = false;
private ConnectionDetector connectionDetector;
private ProgressDialog progressDialog;
private Fragment fragment1;
DateFormat inputFormat;
DateFormat outputFormat;
int counterOnCreate = 0;
int counterOnBind = 0;
public interface OpenSubmissionDetails{
void openSubmissionDetails(String submissionId);
}
public interface IGetTimeLineData {
void getTimeLineData(String date);
void loadSnackBar(String msg);
}
public ProgressDialog getProgressDialog(Context context){
if (progressDialog == null) {
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setMessage(context.getString(R.string.loading_message));
}
return progressDialog;
}
IGetTimeLineData getTimeLineData;
public IGetTimeLineData getGetTimeLineData() {
return getTimeLineData;
}
public void setGetTimeLineData(IGetTimeLineData getTimeLineData) {
this.getTimeLineData = getTimeLineData;
}
OpenSubmissionDetails openSubmissionDetails;
public OpenSubmissionDetails getOpenSubmissionDetails() {
return openSubmissionDetails;
}
public void setOpenSubmissionDetails(OpenSubmissionDetails openSubmissionDetails) {
this.openSubmissionDetails = openSubmissionDetails;
}
public TimelineRecyclerAdapter(List<TimeLineResponseDO.content> contents, Context context, Fragment fragment, IGetTimeLineData iGetTimeLineData) {
this.contents = contents;
this.context = context;
this.fragment1 = fragment;
this.getTimeLineData = iGetTimeLineData;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
connectionDetector = new ConnectionDetector(context);
}
public class TimelineDateHolder extends RecyclerView.ViewHolder{
Button date;
public TimelineDateHolder(View convertView) {
super(convertView);
date = (Button) convertView.findViewById(R.id.date);
}
public void bindViews(TimeLineResponseDO.content response){
Date date1 = null;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("DD-MM-yyyy");
long dateLong = System.currentTimeMillis();
String da = simpleDateFormat.format(dateLong);
Log.e(" Date is :- ", da);
if (response.getDate().equals(da)){
date.setText("Today");
}
else {
if (!TextUtils.isEmpty(response.getDate()))
date.setText(response.getDate());
}
}catch (Exception e){
e.printStackTrace();
if (!TextUtils.isEmpty(response.getDate()))
date.setText(response.getDate());
}
}
}
public class TimelineRecyclerEvenHolder extends RecyclerView.ViewHolder{
RelativeLayout evenContainer;
TextView subjectTypeEven, timingEven, subjectEven, locationEven, teacherEven, cancelTextEven;
Button submissionEven, notificationEven ,submissionEvenDetails ;
ImageView pointerEven, imgCancelBtnEven, imgDelayBtnEven, imgNotificationBtnEven;
View verticalLine;
public TimelineRecyclerEvenHolder(View convertView) {
super(convertView);
evenContainer = (RelativeLayout) convertView.findViewById(R.id.evenContainer);
subjectTypeEven = (TextView) convertView.findViewById(R.id.subjectTypeEven);
timingEven = (TextView) convertView.findViewById(R.id.timingEven);
subjectEven = (TextView) convertView.findViewById(R.id.subjectEven);
locationEven = (TextView) convertView.findViewById(R.id.locationEven);
teacherEven = (TextView) convertView.findViewById(R.id.teacherEven);
pointerEven = (ImageView) convertView.findViewById(R.id.circleEven);
submissionEven = (Button) convertView.findViewById(R.id.submissionEven);
submissionEvenDetails = (Button) convertView.findViewById(R.id.submissionEvenDetails);
imgCancelBtnEven = (ImageView) convertView.findViewById(R.id.cancelBtnEven);//#sp 29-09-2016 added
imgDelayBtnEven = (ImageView) convertView.findViewById(R.id.delayBtnEven);//#sp 30-09-2016 added
imgNotificationBtnEven = (ImageView) convertView.findViewById(R.id.notificationBtnEven);//#sp 30-09-2016 added
notificationEven = (Button) convertView.findViewById(R.id.notificationEven);//#sp 04-10-2016 added
cancelTextEven = (TextView) convertView.findViewById(R.id.cancelTextEven);
subjectTypeEven.setTypeface(NotifyApplication.getExoSemiBold(context));
timingEven.setTypeface(NotifyApplication.getExoSemiBold(context));
subjectEven.setTypeface(NotifyApplication.getExoSemiBold(context));
locationEven.setTypeface(NotifyApplication.getExoSemiBold(context));
teacherEven.setTypeface(NotifyApplication.getExoSemiBold(context));
cancelTextEven.setTypeface(NotifyApplication.getExoSemiBold(context));
}
public void bindViews(TimeLineResponseDO.content response, TimelineRecyclerEvenHolder holder){
holder.evenContainer.setVisibility(View.VISIBLE);
if (!TextUtils.isEmpty(response.getSubjectType())) {
holder.subjectTypeEven.setText(response.getSubjectType());
Log.d("Ifeven", "" + response.getSubjectType());
}
if (response.getLectureSubmission().equals(Enum.LectureSubmission.Submission.toString())) {
holder.subjectTypeEven.setText(Enum.LectureSubmission.Submission.toString());
Log.d("ElseIfeven", "" + response.getSubjectType());
}
Log.d("LectureSubmEven", "" + response.getLectureSubmission());
if (!TextUtils.isEmpty(response.getRoomNo()))
holder.locationEven.setText(response.getRoomNo());
if (!TextUtils.isEmpty(response.getSubject()))
holder.subjectEven.setText(response.getSubject());
if (Boolean.valueOf(StorageService.getInstance().getString("IS_STUDENT")))
{
if (!TextUtils.isEmpty(response.getTeacherAbbreviation()))
holder.teacherEven.setText(response.getTeacherAbbreviation());
}
else if (Boolean.valueOf(StorageService.getInstance().getString("IS_TEACHER")))
{
if (!TextUtils.isEmpty(response.getCourse()))
holder.teacherEven.setText(response.getCourse());
}
if (!TextUtils.isEmpty(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime()))
holder.timingEven.setText(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime());
holder.pointerEven.setBackgroundResource(R.drawable.timeline_pointer_left);
holder.timingEven.setCompoundDrawablesWithIntrinsicBounds(R.drawable.timeline_timing, 0, 0, 0);
Log.d("resp isSubmission", "" + response.isSubmission());
if (response.isSubmission()) {
holder.submissionEven.setVisibility(View.VISIBLE);
holder.submissionEven.setText(response.getSubmissionCount() + " Submission");
} else {
holder.submissionEven.setVisibility(View.GONE);
}
Log.d("resp Noti", "" + response.isNotiification());
if (response.isNotiification()) {
holder.notificationEven.setVisibility(View.VISIBLE);
} else {
holder.notificationEven.setVisibility(View.GONE);
}
if (response.getLectureSubmission().equals("Submission")) {
holder.submissionEvenDetails.setVisibility(View.VISIBLE);
} else {
holder.submissionEvenDetails.setVisibility(View.GONE);
}
if (response.isCancelled()) {
ChangeFontColorEven(holder);
}
if (response.getSubmissionStatus() != null) {
if (response.getSubmissionStatus().equals("No")) {
ChangeFontColorEven(holder);
}
}
if (response.isEvenLayout()) {
if (response.getLectureSubmission().equals("Submission")) {
holder.imgDelayBtnEven.setImageResource(R.drawable.pencil);
} else {
holder.imgDelayBtnEven.setImageResource(R.drawable.delay);
}
}
if (!TextUtils.isEmpty(response.getTrnLectureId())) {
submissionLoadId(response);
}
holder.notificationEven.setText(response.getNotificationCount() + " Notifications");
if (isVisible) {
if (!response.isDateContainer()) {
if (response.getLectureSubmission().equals("Submission") && !TextUtils.isEmpty(response.getLectureSubmission())) {
if (response.getSubmissionStatus().equals("No")) {
ShowHideEditLectureIconsEven(holder, View.GONE);
} else {
ShowHideEditLectureIconsEven(holder, View.VISIBLE);
}
} else {
if (response.isCancelled()) {
ShowHideEditLectureIconsEven(holder, View.GONE);
} else {
ShowHideEditLectureIconsEven(holder, View.VISIBLE);
}
}
}
}else {
ShowHideEditLectureIconsEven(holder, View.GONE);
}
}
}
public class TimelineRecyclerHolder extends RecyclerView.ViewHolder{
RelativeLayout oddContainer;
TextView subjectTypeOdd, timingOdd, subjectOdd, locationOdd, teacherOdd, tvCancelTextOdd;
Button submissionOdd, notificationOdd , submissionOddDetails;
ImageView pointerOdd, imgCancelBtnOdd, imgDelayBtnOdd, imgNotificationBtnOdd;
View verticalLine;
public TimelineRecyclerHolder(View convertView) {
super(convertView);
oddContainer = (RelativeLayout) convertView.findViewById(R.id.oddContainer);
subjectTypeOdd = (TextView) convertView.findViewById(R.id.subjectTypeOdd);
timingOdd = (TextView) convertView.findViewById(R.id.timingOdd);
subjectOdd = (TextView) convertView.findViewById(R.id.subjectOdd);
locationOdd = (TextView) convertView.findViewById(R.id.locationOdd);
teacherOdd = (TextView) convertView.findViewById(R.id.teacherOdd);
pointerOdd = (ImageView) convertView.findViewById(R.id.circleOdd);
submissionOdd = (Button) convertView.findViewById(R.id.submissionOdd);
submissionOddDetails = (Button) convertView.findViewById(R.id.submissionOddDetails);
imgCancelBtnOdd = (ImageView) convertView.findViewById(R.id.cancelBtnOdd);
imgDelayBtnOdd = (ImageView) convertView.findViewById(R.id.delayBtnOdd);
imgNotificationBtnOdd = (ImageView) convertView.findViewById(R.id.notificationBtnOdd);
notificationOdd = (Button) convertView.findViewById(R.id.notificationOdd);
tvCancelTextOdd = (TextView) convertView.findViewById(R.id.cancelTextOdd);
subjectTypeOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
timingOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
subjectOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
locationOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
teacherOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
tvCancelTextOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
}
public void bindViews(TimeLineResponseDO.content response, TimelineRecyclerHolder holder){
//holder.verticalLine.setVisibility(View.VISIBLE);
holder.oddContainer.setVisibility(View.VISIBLE);
if (!TextUtils.isEmpty(response.getSubjectType())) {
holder.subjectTypeOdd.setText(response.getSubjectType());
}
if (response.getLectureSubmission().equals(Enum.LectureSubmission.Submission.toString())) {
holder.subjectTypeOdd.setText(Enum.LectureSubmission.Submission.toString());
}
if (!TextUtils.isEmpty(response.getRoomNo()))
holder.locationOdd.setText(response.getRoomNo());
if (!TextUtils.isEmpty(response.getSubject()))
holder.subjectOdd.setText(response.getSubject());
if (Boolean.valueOf(StorageService.getInstance().getString("IS_STUDENT")))
{
if (!TextUtils.isEmpty(response.getTeacherAbbreviation()))
holder.teacherOdd.setText(response.getTeacherAbbreviation());
}
else if (Boolean.valueOf(StorageService.getInstance().getString("IS_TEACHER")))
{
if (!TextUtils.isEmpty(response.getCourse()))
holder.teacherOdd.setText(response.getCourse());
}
if (!TextUtils.isEmpty(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime()))
holder.timingOdd.setText(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime());
holder.pointerOdd.setBackgroundResource(R.drawable.timeline_pointer_right);
holder.timingOdd.setCompoundDrawablesWithIntrinsicBounds(R.drawable.timeline_timing, 0, 0, 0);
if (response.isSubmission()) {
holder.submissionOdd.setVisibility(View.VISIBLE);
holder.submissionOdd.setText(response.getSubmissionCount() + " Submission");
} else {
holder.submissionOdd.setVisibility(View.GONE);
}
if (response.isNotiification()) {
holder.notificationOdd.setVisibility(View.VISIBLE);
} else {
holder.notificationOdd.setVisibility(View.GONE);
}
if (response.getLectureSubmission().equals("Submission")) {
holder.submissionOddDetails.setVisibility(View.VISIBLE);
} else {
holder.submissionOddDetails.setVisibility(View.GONE);
}
if (response.isCancelled()) {
ChangeFontColor(holder, Enum.LayoutType.Odd);
}
if (response.getSubmissionStatus() != null) {
if (response.getSubmissionStatus().equals("No")) {
ChangeFontColor(holder, Enum.LayoutType.Odd);
}
}
if (response.getLectureSubmission().equals("Submission")) {
holder.imgDelayBtnOdd.setImageResource(R.drawable.pencil);
} else {
holder.imgDelayBtnOdd.setImageResource(R.drawable.delay);
}
if (!TextUtils.isEmpty(response.getTrnLectureId())) {
submissionLoadId(response);
}
holder.notificationOdd.setText(response.getNotificationCount() + " Notifications");
if (isVisible) {
if (!response.isDateContainer()) {
if (response.getLectureSubmission().equals("Submission") && !TextUtils.isEmpty(response.getLectureSubmission())) {
if (response.getSubmissionStatus().equals("No")) {
ShowHideEditLectureIcons(holder, View.GONE);
} else {
ShowHideEditLectureIcons(holder, View.VISIBLE);
}
} else {
if (response.isCancelled()) {
ShowHideEditLectureIcons(holder, View.GONE);
} else {
ShowHideEditLectureIcons(holder, View.VISIBLE);
}
}
}
}else {
ShowHideEditLectureIcons(holder, View.GONE);
}
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case DATE_LAYOUT:
View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.timeline_date_header, parent, false);
return new TimelineDateHolder(convertView);
case EVEN_LAYOUT:
View convertView1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.timeline_even_layout_child, parent, false);
return new TimelineRecyclerEvenHolder(convertView1);
default:
View convertView2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.timeline_odd_layout_child, parent, false);
return new TimelineRecyclerHolder(convertView2);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final int itemType = getItemViewType(position);
TimeLineResponseDO.content content = contents.get(position);
switch (itemType){
case DATE_LAYOUT:
((TimelineDateHolder) holder).bindViews(content);
break;
case EVEN_LAYOUT:
((TimelineRecyclerEvenHolder) holder).bindViews(content, (TimelineRecyclerEvenHolder) holder);
break;
default:
((TimelineRecyclerHolder) holder).bindViews(content, (TimelineRecyclerHolder) holder);
break;
}
if (position == (contents.size() - 1)) {
if (connectionDetector.isConnectingToInternet()) {
getTimeLineData.getTimeLineData(content.getContentDate());
}
else {
getTimeLineData.loadSnackBar(context.getString(R.string.no_internet_connection_schedule));
}
}
}
@Override
public int getItemCount() {
return contents.size();
}
@Override
public int getItemViewType(int position) {
if (contents.get(position).isDateContainer()){
return DATE_LAYOUT;
}
else if (contents.get(position).isEvenLayout()){
return EVEN_LAYOUT;
}
else {
return ODD_LAYOUT;
}
}
private void ChangeFontColor(TimelineRecyclerHolder holder, Enum.LayoutType layoutType) {
switch (layoutType){
case Even:
break;
case Odd:
holder.subjectTypeOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.timingOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.pointerOdd.setBackgroundResource(R.drawable.timeline_pointer_right_red);
holder.subjectOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.locationOdd.setVisibility(View.GONE);
holder.teacherOdd.setVisibility(View.GONE);
holder.tvCancelTextOdd.setVisibility(View.VISIBLE);
holder.tvCancelTextOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.submissionOdd.setVisibility(View.GONE);
holder.submissionOddDetails.setVisibility(View.GONE);
break;
}
}
private void ChangeFontColorEven(TimelineRecyclerEvenHolder holder) {
holder.subjectTypeEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.timingEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.pointerEven.setBackgroundResource(R.drawable.timeline_pointer_left_red);
holder.subjectEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.locationEven.setVisibility(View.GONE);
holder.teacherEven.setVisibility(View.GONE);
holder.cancelTextEven.setVisibility(View.VISIBLE);
holder.cancelTextEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.submissionEven.setVisibility(View.GONE);
holder.submissionEvenDetails.setVisibility(View.GONE);
}
private void ShowHideEditLectureIcons(TimelineRecyclerHolder holder, int visibility) {
holder.imgCancelBtnOdd.setVisibility(visibility);
holder.imgDelayBtnOdd.setVisibility(visibility);
holder.imgNotificationBtnOdd.setVisibility(visibility);
}
private void ShowHideEditLectureIconsEven(TimelineRecyclerEvenHolder holder, int visibility) {
holder.imgCancelBtnEven.setVisibility(visibility);
holder.imgDelayBtnEven.setVisibility(visibility);
holder.imgNotificationBtnEven.setVisibility(visibility);
}
public void editEvent(boolean isVisible) {
this.isVisible = isVisible;
Log.d("Is Visible", String.valueOf(isVisible));
notifyDataSetChanged();
}
public void submission(final TimeLineResponseDO.content timeLineModel) {
final IGetSubmissionDetails iGetSubmissionDetails = new GetSubmissionDetailsBO();
iGetSubmissionDetails.getSubmissionByTrn(context, timeLineModel.getTrnLectureId(), new Response<ArrayList<String>>() {
@Override
public void onSuccess(ArrayList<String> callback) throws IOException {
StorageService.getInstance().putString("SubmissionId", callback.get(0));
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
NoticeBoard noticeBoard = new NoticeBoard();
Bundle bundle = new Bundle();
bundle.putStringArrayList("submissionIds", callback);
bundle.putString("submissionId",callback.get(0));
bundle.putString("isFrom","submission_view");
noticeBoard.setArguments(bundle);
fragmentTransaction.replace(R.id.container, noticeBoard, NoticeBoard.class.getSimpleName());
fragmentTransaction.addToBackStack(NoticeBoard.class.getSimpleName());
fragmentTransaction.commit();
}
@Override
public void onError(Exception e, int i, String msg) {
Log.d("trnId error",e.toString());
}
});
}
public void submissionLoadId(final TimeLineResponseDO.content timeLineModel) {
final IGetSubmissionDetails iGetSubmissionDetails = new GetSubmissionDetailsBO();
iGetSubmissionDetails.getSubmissionByTrn(context, timeLineModel.getTrnLectureId(), new ResponseNew<ArrayList<String>>() {
@Override
public void onSuccess(ArrayList<String> callback, String id) throws IOException {
for (int i = 0; i < callback.size(); i++){
Log.e("trnId error", id + " " + callback.get(i));
if (AppConstant.initializeDbORReturn(context).isSubmissionIdExists(id, callback.get(i))){
Log.e("Already Exists :- ", id + " " + callback.get(i));
}
else {
AppConstant.initializeDbORReturn(context).insertSubmissionId(id, callback.get(i));
}
}
}
@Override
public void onError(Exception e, int i, String msg) {
}
});
}
private void localTimeLinedb(List<TimeLineResponseDO> callback, Context context) {
for (int i = 0; i < callback.size(); i++) {
TimeLineResponseDO timeLineResponseDO = callback.get(i);
for (int j = 0; j < timeLineResponseDO.getContentList().size(); j++) {
TimeLineResponseDO.content content = timeLineResponseDO.getContentList().get(j);
if (AppConstant.initializeDbORReturn(context).isTimeLineExits(timeLineResponseDO.getDate(),
content.getStaticLectureId(), content.getDate(),
content.getTrnLectureId(), content.getSlotId(), content.getSubject(),
content.getSubmissionId(), content.getLectureSubmission())) {
AppConstant.initializeDbORReturn(context).updateTimeLine(content, timeLineResponseDO.getDate());
} else {
AppConstant.initializeDbORReturn(context).insertIntoTimeLine(content, timeLineResponseDO.getDate());
}
}
}
}
public void getTimeLineData(String date, final Context context, final TimelineRecyclerHolder holder, final Enum.LayoutType even_odd, final Enum.EditLectureType editLectureType) {
inputFormat = new SimpleDateFormat("dd-mm-yyyy");
outputFormat = new SimpleDateFormat("yyyy-mm-dd");
ITimeLine timeLine = new TimeLineBO();
TimeLinePostDO timeLinePostDO = new TimeLinePostDO();
if (date == null) {
if (!TextUtils.isEmpty(StorageService.getInstance().getString("Running_Teacher_Id"))){
timeLinePostDO.setUserId(StorageService.getInstance().getString("Running_Teacher_Id"));
}
else {
timeLinePostDO.setUserId(StorageService.getInstance().getString("NEWUSER_ID"));
}
timeLinePostDO.setUserRoles(StorageService.getInstance().getString("USER_ROLES"));
} else {
if (!TextUtils.isEmpty(StorageService.getInstance().getString("Running_Teacher_Id"))){
timeLinePostDO.setUserId(StorageService.getInstance().getString("Running_Teacher_Id"));
}
else {
timeLinePostDO.setUserId(StorageService.getInstance().getString("NEWUSER_ID"));
}
timeLinePostDO.setUserRoles(StorageService.getInstance().getString("USER_ROLES"));
try {
timeLinePostDO.setDate(outputFormat.format(inputFormat.parse(date)));
} catch (ParseException e) {
e.printStackTrace();
}
}
timeLine.getTimeLineData(context, timeLinePostDO, new Response<List<TimeLineResponseDO>>() {
@Override
public void onSuccess(List<TimeLineResponseDO> callback) {
localTimeLinedb(callback, context);
getProgressDialog(context).dismiss();
switch (editLectureType){
case Cancel:
ChangeFontColor(holder, even_odd);
Toast.makeText(context, "Lecture Cancelled", Toast.LENGTH_SHORT).show();
ShowHideEditLectureIcons(holder, View.GONE);
break;
case Delay:
Toast.makeText(context, "Notification sent for delay", Toast.LENGTH_SHORT).show();
break;
case CreateNotification:
Toast.makeText(context, "Notification sent", Toast.LENGTH_SHORT).show();
break;
}
if (fragment1 instanceof TimeLineFragment){
((TimeLineFragment) fragment1).refreshList();
}
}
@Override
public void onError(Exception e, int i, String msg) {
}
});
}
}
提前致谢 !