简体   繁体   English

回收者视图选择项目并保存选择

[英]Recycler view select item and save the selection

i have this Recycler view and its working good , but now i need when the user click into item in the Recycler highlight the item and intent to another activity , and when back to the Recycler show the item that selected still highlight and just one item can select ? 我具有此Recycler视图及其正常工作,但是现在我需要当用户单击Recycler中的项目以突出显示该项目并意图进行另一项活动时,以及当返回Recycler显示所选的项目时仍突出显示,并且只有一个项目可以选择 ? any idea ? 任何想法 ?

this is my recycler view adapter 这是我的回收站视图适配器

public class ScreenRecyclerAdapter  extends RecyclerView.Adapter<ScreenRecyclerAdapter.ViewHolder> {
Context context;
int image_list[];
public ScreenRecyclerAdapter(int[] image_list, Context context){
    super();
    this.image_list = image_list;
    this.context = context;}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.screen_items, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Picasso.with(context).load(image_list[position]).into(holder.image_view_screen_item);
   }
@Override
public int getItemCount() {
    return image_list.length;
}

class ViewHolder extends RecyclerView.ViewHolder{
    ImageView image_view_screen_item;
    public ViewHolder(View itemView) {
        super(itemView);
        image_view_screen_item = (ImageView) itemView.findViewById(R.id.image_view_screen_item);
        image_view_screen_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, ImagePager.class);
                context.startActivity(i);
                image_view_screen_item.setSelected(true);
            }
        });
    }}}

and this is the Activity of recycler 这是回收商的活动

public class ScreensActivity extends AppCompatActivity {
RecyclerView image_recyclerView;
RecyclerView.LayoutManager image_recyclerViewlayoutManager;
RecyclerView.Adapter image_recyclerViewadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screens);
    int image_list [] = {R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    image_recyclerView = (RecyclerView) findViewById(R.id.image_recyclerView);
    image_recyclerView.setHasFixedSize(true);
    image_recyclerView.setSelected(true);
    image_recyclerViewlayoutManager = new LinearLayoutManager(this);
    image_recyclerView.setLayoutManager(image_recyclerViewlayoutManager);
    image_recyclerViewadapter = new ScreenRecyclerAdapter(image_list, this);
    image_recyclerView.setAdapter(image_recyclerViewadapter);}
@Override
public void onBackPressed() {
}}

When clicking on an item, save its position and pass it to the other activity. 单击一个项目时,保存其位置并将其传递给其他活动。 Once returning to the original activity return the saved value with it and in onBindViewHolder method if the position equals the saved value highlight the item. 一旦返回到原始活动,则随其返回保存的值,如果位置等于保存的值,则在onBindViewHolder方法中突出显示该项目。

Let's divide the question into 3 sub-parts: 让我们将问题分为3个子部分:

1. Highlight recycler-view item on tap 1.在水龙头上突出显示回收者视图项目
You can implement recycler-view item listener in multiple ways, as describe here . 您可以实现recycler-view item listener以多种方式,如描述在这里 Once you implement it, change view background-color from there like: 实施后,从此处更改view背景颜色:

 @Override 
 public void onItemClick(View view, int position) {
     view.setBackgroundColor(Color.parseColor("#eee"));
     //
  }

2. Redirect to another activity on tap 2.在点击时重定向到另一个活动
Within onClick method you may start a new activity like: onClick方法中,您可以启动新的活动,例如:

@Override 
 public void onItemClick(View view, int position) {
     // check for item
     Intent intent = new Intent(mContext);
     startActivity(intent);
  }

3. When back to the Recycler-view show the selected item as highlighted 3.回到“回收站”视图时,将选中的项目突出显示
Make a public static var . 制作一个public static var When you click an item - which will redirect user to another activity assign that item id to that static var , now in onBindViewHolder always check if the list-item id as same as the static var if it's true then change background of the view as in part 1. 单击项目时-将用户重定向到另一个activity将该项目ID分配给该static var ,现在在onBindViewHolder始终检查列表项ID是否与static var相同(如果为true然后按如下方式更改view背景第1部分。
Make sure not to finish current activity after starting a new activity . 确保没有完成当前activity开始一个新的后activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM