代码之家  ›  专栏  ›  技术社区  ›  Sardar_Persian

转到RecyclerView上选定的项目

  •  1
  • Sardar_Persian  · 技术社区  · 6 年前

    问题:我想转到用户选定的项目 但我选不到它的位置。

    我怎样才能得到我的位置并保持原位??? 我尝试了很多方法,但都没有成功 我的代码有问题吗?

    你有什么办法帮我
    enter image description here

    代码适配器:

     public class Adapter_Badan extends RecyclerView.Adapter<Adapter_Badan.ViewHolder> {
    private Context context;
    private List<Modell_Badan> modells;
    
    public Adapter_Badan(Context context, List<Modell_Badan> modells) {
        this.modells = modells;
        this.context = context;
    
    }
    
    
    @NonNull
    @Override
    public Adapter_Badan.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Adapter_Badan.ViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_badan, parent, false));
    }
    
    @Override
    public void onBindViewHolder(@NonNull final Adapter_Badan.ViewHolder holder, final int position) {
        final Modell_Badan modell = modells.get(position);
        holder.title.setText(modell.getTitle());
        holder.content.setText(modell.getContent());
        Picasso.get().load(modell.getImage()).into(holder.image1);
        Picasso.get().load(modell.getImage2()).into(holder.image2);
    
        holder.itemView.setOnClickListener(new View.OnClickListener() {
    
                   @Override
            public void onClick(View v) {
    
    
                new Handler().postDelayed(new Runnable() {
                    public void run() {
    
                        Intent intent= new Intent(context,Badan.class);
    
                        intent.putExtra(Modell.ID,modell.getId());
                        intent.putExtra(Modell.TITLE,modell.getTitle());
                        intent.putExtra(Modell.CONTENT,modell.getContent());
                        intent.putExtra(Modell.IMAGE1,modell.getImage());
                        intent.putExtra(Modell.IMAGE12,modell.getImage2());
                        intent.putExtra("id", "1");
                        context.startActivity(intent);
    
    
                        ((Activity)context).finish();
    
                        ((Activity)context).overridePendingTransition(R.anim.alpha_f,R.anim.alpha_o);
    
                    }
                }, 20);
    
            }
    
        });
    }
    
    @Override
    public int getItemCount() {
        return modells.size();
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder  {
    
        private ImageView image1;
        private ImageView image2;
        private TextView title;
        private TextView content;
        private RelativeLayout relativeLayout;
        public ViewHolder(View itemView) {
    
    
            super(itemView);
    
            image1 = (ImageView) itemView.findViewById(R.id.image_badan);
            image2 = (ImageView) itemView.findViewById(R.id.image2_badan);
            title = (TextView) itemView.findViewById(R.id.title_badan);
            content = (TextView) itemView.findViewById(R.id.content_badan);
    
        }
    
    
    }
    

    但选中项目后,该职位将被删除 我能证明物品的位置吗?

    代码巴丹:

    public class Badan extends AppCompatActivity  {
    private SharedP_Thems sharedP_thems;
    private ImageView image_layout, image_layout2;
    private TextView title_, content_;
    private SharedPerfern sharedPerfern;
    private Shared_Modell shared_modell;
    private Adapter_Badan adapter_badan;
    int a;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        sharedPerfern = new SharedPerfern(this);
        shared_modell = sharedPerfern.getShared();
        sharedP_thems = new SharedP_Thems(this);
    
        if (sharedP_thems.getLoad().equals(true))
            setTheme(R.style.dark_them);
        else setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_badan);
    
        setUpToolbarLayout();
        setUpView();
    
    
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recy_badanw);
        Adapter_Badan adapter_badan=new Adapter_Badan(this, Data_FackeGen.getModellBadan(this));
        recyclerView.setLayoutManager( new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));
        recyclerView.setAdapter(adapter_badan);
    
    
        getInten();
        title_.setTextSize(shared_modell.getFonSize());
        content_.setTextSize(shared_modell.getFonSize());
    
    
    }
    
    
    private void setUpToolbarLayout() {
    
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.tollbar_badab);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    
    private void setUpView() {
        image_layout = (ImageView) findViewById(R.id.image_badan2w);
        image_layout2 = (ImageView) findViewById(R.id.image2_badanw);
        title_ = (TextView) findViewById(R.id.title_badanw);
        content_ = (TextView) findViewById(R.id.content_badanw);
    }
    
    private void getInten() {
        Intent intent = getIntent();
        int id = intent.getIntExtra(Modell.ID, 0);
        String titel = intent.getStringExtra(Modell.TITLE);
        String content = intent.getStringExtra(Modell.CONTENT);
        int image1 = intent.getIntExtra(Modell.IMAGE1, 2);
        int image2 = intent.getIntExtra(Modell.IMAGE12, 2);
        Picasso.get().load(image1).into(image_layout);
        Picasso.get().load(image2).into(image_layout2);
        title_.setText(titel);
        content_.setText(content);
    }
    

    }

    2 回复  |  直到 6 年前
        1
  •  1
  •   SuperFrog    6 年前

    尝试在单击侦听器中移动以下代码:

     Modell_Badan modell = modells.get(position);
     holder.title.setText(modell.getTitle());
     holder.content.setText(modell.getContent());
     Picasso.get().load(modell.getImage()).into(holder.image1);
     Picasso.get().load(modell.getImage2()).into(holder.image2);
    
        2
  •  0
  •   Sardar_Persian    6 年前

    问题解决了 我使用了以下方法并回答

        @Override
    protected void onPause() {
        super.onPause();
        bundle = new Bundle();
        Parcelable parcelable = recyclerView.getLayoutManager().onSaveInstanceState();
        bundle.putParcelable(KEY_RECYCLER_STATE, parcelable);
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
    
        // restore RecyclerView state
        if (bundle != null) {
            Parcelable parcelable = bundle.getParcelable(KEY_RECYCLER_STATE);
            recyclerView.getLayoutManager().onRestoreInstanceState(parcelable);
        }
    }