I'm facing a problem with my Recycler View , Images are not showing properly,
Here is the links where you can see what i'm taking about
when changing Recycler view width to match-parent and height to wrap_content it works but on scrolling down then coming back up it screws up and again only one images shows at a time
Here is code for customlayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/listicon"
android:layout_width="match_parent"
android:layout_height="220dp" />
</LinearLayout>
Here is my fragment_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
3.Here is HomeFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class HomeFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private CustomAdapter adapter;
int[] IMAGES = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
private RecyclerView recyclerView;
ArrayList<HashMap<String, String>> data;
private String mParam1;
private String mParam2;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView =(RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new CustomAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<Information> getData(){
List<Information> data = new ArrayList<>();
int [] icons = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
String[] titles={"Abs","Arms","Back","Chest","full","legs"};
for (int i=0; i<icons.length && i<titles.length;i++){
Information current = new Information();
current.images=icons[i];
current.title=titles[i];
data.add(current);
}
return data;
}
}
when you are using RecyclerView
and will make a xml file for list, in that case you have to use wrap_content
as a height otherwise it will take height of device while you scrolled.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/listicon"
android:layout_width="match_parent"
android:layout_height="220dp />
</LinearLayout>
in java do this :
recycler = (RecyclerView) layout.findViewById(R.id.recycler);
layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recycler.setLayoutManager(layoutManager);
adapter = new CustomAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);