在我的应用程序中,我有自定义适配器,适配器有图像和文本,当我滚动RecyclerView时某些项目位置自动更改
public class MyPhotosAdapter extends RecyclerView.Adapter<MyPhotosAdapter.ViewHolder> implements View.OnClickListener {
private PojoClasses.DashboardPhotos[] dashboardPhotoses;
private static final int TWEET_COMPOSER_REQUEST_CODE = 500;
Activity mActivity;
private ProgressDialog pDialog;
private static final String IMAGE_DIRECTORY_NAME = "My City Tag";
static File mediaFile;
static File mediaStorageDir;
static String mediaName = null;
static Integer twCurrent, fbCurrent;
ShareDialog shareDialog;
CallbackManager callbackManager;
private static final String CURRENT_USER_VIOLATION_ID = "twuserVId";
private static final String PREF_KEY_FACEBOOK_LOGIN = "facebook_loggedin";
private static final String PREF_KEY_TWITTER_LOGIN = "twitter_loggedin";
private static final String PREF_NAME = "my city tag";
private static SharedPreferences mSharedPreferences;
boolean isLoggedInTW;
boolean isLoggedInFB;
int isShareFb = 0;
DBHelper dbHelper;
public MyPhotosAdapter(PojoClasses.DashboardPhotos[] dashboardPhotoses) {
this.dashboardPhotoses = dashboardPhotoses;
}
public MyPhotosAdapter(Activity activity, PojoClasses.DashboardPhotos[] dashboardPhotoses) {
this.dashboardPhotoses = dashboardPhotoses;
mActivity = activity;
}
// Create new views (invoked by the layout manager)
@Override
public MyPhotosAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
FacebookSdk.sdkInitialize(mActivity);
callbackManager = CallbackManager.Factory.create();
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY,TWITTER_SECRET);
Fabric.with(mActivity, new com.twitter.sdk.android.Twitter(authConfig));
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_photo, null);
dbHelper = new DBHelper(mActivity);
Fabric.with(mActivity, new TweetComposer());
shareDialog=new ShareDialog(mActivity);
/* Initialize application preferences */
mSharedPreferences = mActivity.getSharedPreferences(PREF_NAME, 0);
isLoggedInFB = mSharedPreferences.getBoolean(PREF_KEY_FACEBOOK_LOGIN, false);
isLoggedInTW = mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.mImageViewCapUser.setImageBitmap(dashboardPhotoses[position].getThumbnail());
viewHolder.txtDescription.setText(dashboardPhotoses[position].getDescription());
viewHolder.txtDate.setText(dashboardPhotoses[position].getCreatedDate());
viewHolder.txtViolation.setText(dashboardPhotoses[position].getViolation());
final int FB = dashboardPhotoses[position].getIsShareFb();
final int TW = dashboardPhotoses[position].getIsShareTw();
final int priority = dashboardPhotoses[position].getPriority();
switch (priority) {
case 2:
viewHolder.mImageViewPriority.setBackgroundColor(Color.parseColor("#D50000"));
break;
case 3:
viewHolder.mImageViewPriority.setBackgroundColor(Color.parseColor("#FF9800"));
break;
case 4:
viewHolder.mImageViewPriority.setBackgroundColor(Color.parseColor("#FFEB3B"));
break;
}
viewHolder.mImageViewIcon.setTag(dashboardPhotoses[position]);
if (FB == 0) {
viewHolder.mImageViewIcon.setImageResource(R.drawable.ic_action_share);
viewHolder.mImageViewIcon.setFocusable(false);
viewHolder.mImageViewIcon.setFocusableInTouchMode(false);
viewHolder.mImageViewIcon.setClickable(true);
viewHolder.mImageViewIcon.setOnClickListener(this);
} else if (TW == 0) {
viewHolder.mImageViewIcon.setImageResource(R.drawable.ic_action_share);
viewHolder.mImageViewIcon.setFocusable(false);
viewHolder.mImageViewIcon.setFocusableInTouchMode(false);
viewHolder.mImageViewIcon.setClickable(true);
viewHolder.mImageViewIcon.setOnClickListener(this);
} else {
viewHolder.mImageViewIcon.setImageResource(R.mipmap.ic_send);
}
viewHolder.txtDescription.setTag(dashboardPhotoses[position]);
if (FB == 0) {
viewHolder.txtDescription.setFocusable(false);
viewHolder.txtDescription.setFocusableInTouchMode(false);
viewHolder.txtDescription.setClickable(true);
viewHolder.txtDescription.setOnClickListener(this);
} else if (TW == 0) {
viewHolder.txtDescription.setFocusable(false);
viewHolder.txtDescription.setFocusableInTouchMode(false);
viewHolder.txtDescription.setClickable(true);
viewHolder.txtDescription.setOnClickListener(this);
} else {
}
}
@Override
public void onClick(View v) {
PojoClasses.DashboardPhotos dashboardPhotos;
switch (v.getId()){
case R.id.imgv_Icon_list:
dashboardPhotos = (PojoClasses.DashboardPhotos) v.getTag();
int fb = dashboardPhotos.getIsShareFb();
int tw = dashboardPhotos.getIsShareTw();
if(fb == 0 && tw == 0) {
selectSocial(dashboardPhotos);
}
else if(fb == 0){
selectSocialfb(dashboardPhotos);
}
else if(tw == 0){
selectSocialtw(dashboardPhotos);
}
break;
case R.id.txt_tag_des:
dashboardPhotos = (PojoClasses.DashboardPhotos) v.getTag();
Intent i = new Intent(mActivity,EditViolationActivity.class);
i.putExtra("UserViolationId",dashboardPhotos.getUserViolationId());
mActivity.startActivity(i);
break;
}
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public static TextView txtDescription;
public ImageView mImageViewCapUser;
public TextView txtDate;
public TextView txtViolation;
public static ImageView mImageViewIcon;
public View mImageViewPriority;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtDescription = (TextView) itemLayoutView.findViewById(R.id.txt_tag_des);
txtDate = (TextView) itemLayoutView.findViewById(R.id.txt_date);
txtViolation = (TextView) itemLayoutView.findViewById(R.id.txt_violation);
mImageViewCapUser = (ImageView) itemLayoutView.findViewById(R.id.imgv_capture_user);
mImageViewIcon = (ImageView) itemLayoutView.findViewById(R.id.imgv_Icon_list);
mImageViewPriority = itemLayoutView.findViewById(R.id.imgv_priority);
}
}
@Override
public int getItemCount() {
return dashboardPhotoses.length;
}
}
如果有人能解决该特定问题,请帮助我。
提前致谢
答案 0 :(得分:1)
在switch语句中加上默认值
default:
viewHolder.mImageViewPriority.setBackgroundColor("Default color here for other priorities");
break;
并将默认值放在else
条件的if else
部分