How to add images in RecyclerView from GridView in android

I want to add images in RecyclerView from GridView. I am getting position of images in GridView, pass it to fragments, and again pass it from fragment to that corresponding adapter. Now, I am unable to add that images in RecyclerView. That is, if I try to add image, it gets added for all the rows. But I want to add it in this format. If I tap first row in recycler view, it should be like (Row 1, imageview 1) (Row 1 , imageview 2), like that. If I tap second row in recycler view, it should be like (Row 2, imageview 1) (Row 2, imageview 2), like that for all the rows.

DemoFragment.java

public class DemoDragment extends Fragment implements SurfaceHolder.Callback, View.OnClickListener {  private RecyclerView recyclerView; private OnFragmentInteractionListener mListener; public static HomeVideoListener homeVideoListener; public static SurfaceView surfaceView; public static ImageView playIcon; public static boolean homePlayClicked; public static LastSeenChannelHomeResponse.RootEntity rootEntity; public static String playURL = ""; SharedPreferences sharedpreferences; boolean checkRate = false; SharedPreferences.Editor editor; private String customerUniqueId, version; private JSONCallback updateCall; private Context context; private Activity activityContext; private TextView logoutTV, backButtonTV, titleTV, mainHomeTV; private Uri contentUri; private ImageView titleImageView; private ImageView logoImageButton; private RelativeLayout actionbarLayout; private RelativeLayout mainRelativeLayout; private GestureDetector gestureDetector; private View.OnTouchListener gestureListener; private PercentRelativeLayout surfaceLayout; public static boolean playingVideo;  private boolean checkNowClick = false, isCancelled = false,           playerNeedsPrepare; public static final int TYPE_HLS = 2; private int contentType = TYPE_HLS; private CustomPlayer player; private AudioCapabilitiesReceiver audioCapabilitiesReceiver; private long playerPosition; private EventLogger eventLogger; public static MediaController.MediaPlayerControl mediaControl; private long timeToPlay = 0; private LinearLayout channelControls; public static String countryCodeValue; private String mcc, mnc, ipAddress, strDate, getChannelDataURLNew, timeZone; private int swipeGesturePosition = 0; private boolean isPaused = false, isPlaying = true; GestureDetector mGestureDetector;    private ImageButton onDemandBack;  public DemoFragment() {     // Required empty public constructor }     @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     if (getArguments() != null) {      }  }  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,                          Bundle savedInstanceState) {     // Inflate the layout for this fragment     View view = inflater.inflate(R.layout.fragment_demo, container, false);      playingVideo = false;      context = getActivity();     activityContext = getActivity();     homeVideoListener = new HomeVideoListener(activityContext);     version = android.os.Build.VERSION.RELEASE;     sharedpreferences = PreferenceManager             .getDefaultSharedPreferences(context);      editor = sharedpreferences.edit();     customerUniqueId = sharedpreferences.getString(Constants.PREF_CUSTOMER_UNIQ, null);     playIcon = (ImageView) view.findViewById(R.id.play_icon);      surfaceLayout = (PercentRelativeLayout) getActivity().findViewById(R.id.surface_view);      surfaceView = (SurfaceView) view.findViewById(R.id.home_image);      recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);     channelControls = (LinearLayout)view.findViewById(R.id.linear_recycler);      DemoFragment = (DemoFragment) this;     Bundle args = fragment.getArguments();     int pos = 0;     int appPos=0;     if (args != null) {         pos = args.getInt("id");         appPos = args.getInt("id");         // Toast.makeText(getActivity(), "Getting position in DemoFragment:" + pos, Toast.LENGTH_SHORT).show();     }      // this is data fro recycler view     ItemData itemsData[] = {new ItemData(R.drawable.app_launch_icon),             new ItemData(R.drawable.app_launch_icon),             new ItemData(R.drawable.blank_box),             new ItemData(R.drawable.blank_box)};      // 2. set layoutManger     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));     // 3. create an adapter     MyAdapter mAdapter = new MyAdapter(itemsData, (AutomateActivity) getActivity(), pos, appPos);     // 4. set adapter     recyclerView.setAdapter(mAdapter);      gestureListener = new View.OnTouchListener() {         public boolean onTouch(View v, MotionEvent event) {             playIcon.setVisibility(View.VISIBLE);             new Handler().postDelayed(new Runnable() {                 public void run() {                     if (homePlayClicked) {                         playIcon.setVisibility(View.GONE);                     }                 }             }, 5 * 1000);             return true;         }     };      surfaceView.setOnTouchListener(gestureListener);     playIcon.setOnClickListener(this);     homePlayClicked = true;    return view; }    MyAdapter.java  public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {  private ItemData[] itemsData; static AutomateActivity activity; static int pos; static int rowPosition;  static int appPos;   static int roomAreaPos; static int grieviewImagePosition[] = {-1, -1, -1, -1, -1, -1};   public MyAdapter(ItemData[] itemsData, AutomateActivity activity, int pos, int appPos) {     this.itemsData = itemsData;     this.activity = activity;     this.pos = pos;     this.appPos = appPos;   }   // Create new views (invoked by the layout manager) @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,                                                int viewType) {     // create a new view     View itemLayoutView = LayoutInflater.from(parent.getContext())             .inflate(R.layout.listview_layout, null);      // create ViewHolder      ViewHolder viewHolder = new ViewHolder(itemLayoutView);     return viewHolder; }  // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(final ViewHolder viewHolder, final int position) {      // - get data from your itemsData at this position     // - replace the contents of the view with that itemsData      if (roomAreaPos == position) {           grieviewImagePosition[position] = pos;       }      if (grieviewImagePosition[position] != -1) {         viewHolder.imgViewIcon.setVisibility(View.VISIBLE);          viewHolder.imgViewIcon.setImageResource(AutomateRoomsFragment.automateRooms[grieviewImagePosition[position]]);           if (roomAreaPos == position) {               grieviewImagePosition[position] = pos;         }     }       viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener() {         public void onClick(View view) {             switch (position) {                 case 0:                     break;             }         }     });      viewHolder.rLayout.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             // viewHolder.imgViewIcon.setVisibility(View.VISIBLE);             switch (position) {                 case 0:                     roomAreaPos = position;                     Fragment fragment = new AutomateRoomsFragment();                     FragmentManager fragmentManager = activity.getSupportFragmentManager();                     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();                     fragmentTransaction.replace(R.id.main_container, fragment);                     fragmentTransaction.addToBackStack(null);                     fragmentTransaction.commit();                     break;                  case 1:                     roomAreaPos = position;                      Fragment fragment1 = new AutomateRoomsFragment();                     FragmentManager fragmentManager1 = activity.getSupportFragmentManager();                     FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();                     fragmentTransaction1.replace(R.id.main_container, fragment1);                     fragmentTransaction1.addToBackStack(null);                     fragmentTransaction1.commit();                     break;                  case 2:                     roomAreaPos = position;                      Fragment fragment2 = new AutomateRoomsFragment();                     FragmentManager fragmentManager2 = activity.getSupportFragmentManager();                     FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();                     fragmentTransaction2.replace(R.id.main_container, fragment2);                     fragmentTransaction2.addToBackStack(null);                     fragmentTransaction2.commit();                     break;                  case 3:                     roomAreaPos = position;                     Fragment fragment3 = new AutomateRoomsFragment();                     FragmentManager fragmentManager3 = activity.getSupportFragmentManager();                     FragmentTransaction fragmentTransaction3 = fragmentManager3.beginTransaction();                     fragmentTransaction3.replace(R.id.main_container, fragment3);                     fragmentTransaction3.addToBackStack(null);                     fragmentTransaction3.commit();                     break;               }           }      });  }   // inner class to hold a reference to each item of RecyclerView public static class ViewHolder extends RecyclerView.ViewHolder {      public ImageView imgViewIcon;     public RelativeLayout rLayout;     private MyAdapter mParent;      public ImageView appImgOne, appImgTwo, appImageThree, appImageFour;      static boolean DeviceDisplay = false;      public ViewHolder(View itemLayoutView) {         super(itemLayoutView);          itemView.setOnClickListener(new View.OnClickListener() {               @Override             public void onClick(View v) {                  int itemPosition = getAdapterPosition();                  rowPosition = itemPosition;                    Toast.makeText(activity, "Row position inside switch case:" + rowPosition, Toast.LENGTH_SHORT).show();                      Fragment fragment = new AutomateAppliancesFragment();                 FragmentManager fragmentManager = activity.getSupportFragmentManager();                 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();                 fragmentTransaction.replace(R.id.main_container, fragment);                 fragmentTransaction.addToBackStack(null);                 fragmentTransaction.commit();               /*  appImgOne.setVisibility(View.VISIBLE);                 appImgOne.setImageResource(AutomateAppliancesFragment.appliances[pos]);*/              }         });           this.imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.icon);         this.rLayout = (RelativeLayout) itemLayoutView.findViewById(R.id.btn_circle_one);         Resources res = itemLayoutView.getResources(); //resource handle         Drawable drawable = res.getDrawable(R.drawable.blank_box); //new Image that was added to the res folder         rLayout.setBackground(drawable);          this.appImgOne = (ImageView) itemLayoutView.findViewById(R.id.appliance_one);         this.appImgTwo = (ImageView) itemLayoutView.findViewById(R.id.appliance_two);         this.appImageThree = (ImageView) itemLayoutView.findViewById(R.id.appliance_three);         this.appImageFour = (ImageView) itemLayoutView.findViewById(R.id.appliance_four);         switch (rowPosition) {              case  0:                  if ( appImgOne.getVisibility() == View.GONE) { appImgOne.setImageResource(AutomateAppliancesFragment.appliances[appPos]); appImgOne.setVisibility(View.VISIBLE);    }  }    }    // Return the size of your itemsData (invoked by the layout manager) @Override public int getItemCount() {     return itemsData.length;  }}  listview_layout.xml  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:clickable="true" android:focusable="true">         <RelativeLayout         android:id="@+id/btn_circle_one"         android:layout_width="60dp"         android:layout_height="50dp"         android:layout_marginTop="3dp">          <ImageView             android:id="@+id/icon"             android:layout_width="40dp"             android:layout_height="40dp"             android:layout_alignParentTop="true"             android:layout_marginBottom="@dimen/margin_1"             android:layout_marginLeft="@dimen/margin_medium"             android:layout_marginTop="@dimen/margin_1"             android:adjustViewBounds="false"             android:background="@drawable/image_background"             android:paddingBottom="@dimen/margin_2"             android:paddingLeft="@dimen/margin_small"             android:paddingRight="@dimen/margin_small"             android:paddingTop="@dimen/margin_2"             android:scaleType="fitCenter"             android:visibility="gone"/>         </RelativeLayout>       <View         android:layout_width="match_parent"         android:layout_height="@dimen/line_height"         android:layout_below="@id/btn_circle_one"         android:layout_marginLeft="@dimen/margin_four"         android:layout_marginRight="@dimen/margin_four"         android:layout_marginTop="12dp"         android:background="@color/separator_color"         android:id="@+id/view" /> <RelativeLayout     android:id="@+id/relative"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_above="@+id/view"     android:layout_alignLeft="@+id/view"     android:layout_alignStart="@+id/view">      <ImageView         android:layout_width="50dp"         android:layout_height="50dp"         android:id="@+id/appliance_one"         android:src="@drawable/app_launch_icon"         android:layout_toLeftOf="@+id/appliance_two"         android:layout_toStartOf="@+id/appliance_two"         android:layout_marginLeft="33dp"         android:visibility="gone"     />       <ImageView         android:layout_width="50dp"         android:layout_height="50dp"         android:id="@+id/appliance_two"         android:src="@drawable/app_launch_icon"         android:layout_alignParentTop="true"         android:layout_centerHorizontal="true"         android:visibility="gone"/>      <ImageView         android:layout_width="50dp"         android:layout_height="50dp"         android:id="@+id/appliance_three"         android:src="@drawable/app_launch_icon"         android:layout_alignParentTop="true"         android:layout_toRightOf="@+id/appliance_two"         android:layout_toEndOf="@+id/appliance_two"         android:visibility="gone"/>      <ImageView         android:layout_width="50dp"         android:layout_height="50dp"         android:id="@+id/appliance_four"         android:src="@drawable/app_launch_icon"         android:layout_alignParentTop="true"         android:layout_toRightOf="@+id/appliance_three"         android:layout_toEndOf="@+id/appliance_three"         android:visibility="gone"/>    </RelativeLayout> 
         Can someone please help me to resolve this issue ASAP. 

Thanks in advance.

Replay

Category: android Time: 2016-07-30 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.230 (s). 12 q(s)