当滚动时,Recyclerview会自动更改项目位置

4

在我的应用程序中,我有自定义适配器,适配器具有图像和文本。当我滚动 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;
        }
}

如果有人能够解决这个特定的问题,请帮忙一下。

提前致谢。

1个回答

1
在你的 switch 语句中加入一个默认值。
 default:
    viewHolder.mImageViewPriority.setBackgroundColor("Default color here for other priorities");
 break;

在所有的if else条件语句中,将默认值放在else部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接