0

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 ? 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() {
}}
meladandroid
  • 49
  • 2
  • 9

2 Answers2

0

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.

David.K
  • 61
  • 3
0

Let's divide the question into 3 sub-parts:

1. Highlight recycler-view item on tap
You can implement recycler-view item listener in multiple ways, as describe here. Once you implement it, change view background-color from there like:

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

2. Redirect to another activity on tap
Within onClick method you may start a new activity like:

@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
Make a 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.
Make sure not to finish current activity after starting a new activity.

Community
  • 1
  • 1
isamirkhaan1
  • 749
  • 7
  • 19
  • its working Highlight recycler-view item , but step 3 i cant understand it ! – meladandroid Oct 16 '16 at 09:56
  • when user tap on an `item` save that item `id` either in `SharedPreferences` or in static variable... within `getView()` or `bindHindler()` check for that item `id`, if itemId is equal to that last-selected-item-id then change background-color like
    ` if (viewHolder.itemId==myItemId){view.backgroundColor(Color.parseColor("#eee")); }`
    – isamirkhaan1 Oct 16 '16 at 13:23