滚动时,Recyclerview自动更改项目位置 [英] Recyclerview change item position automatically when scrolling

查看:59
本文介绍了滚动时,Recyclerview自动更改项目位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当我滚动RecyclerView时,我具有自定义适配器,适配器具有图像和文本,其中某些项目位置会自动更改

In my application i have custom adapter, adapter has image, and text when I scrolling the RecyclerView some items position automatic change

    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;
        }
}

如果有人有解决该特定问题的方法,请帮助我.

Please help me if anyone have solution for that particular problem .

预先感谢

推荐答案

在switch语句中输入默认值

Put a default in your switch statement

 default:
    viewHolder.mImageViewPriority.setBackgroundColor("Default color here for other priorities");
 break;

并将默认值放在所有if else条件的else部分

and put default values in else part of all your if else conditions

这篇关于滚动时,Recyclerview自动更改项目位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆