각 조각으로 표시된 5 개의 탭이있는 TabLayout에서 RecyclerView 및 CardLayout을 사용하고 있습니다. 탭을 클릭 할 때마다 먼저 조각이 표시되는지 확인합니다. 사실이라면, 사용자 정의 어댑터에 의해 RecyclerView에 채워진 DB에서 데이터를 가져 오기 위해 REST 서비스를 호출하는 UI 코드를 실행합니다. 조각에 대한 빈번한 시도가 또 다른 REST 요청을 필요로하지 않도록 ArrayList에 일시적으로 데이터를 캐시합니다. 내 문제는 특정 탭으로 여러 번 이동할 때마다 레이아웃이 작아지고 작을 때마다 작아집니다. 아래는 나의 조각, 어댑터 및 어댑터 레이아웃입니다.RecyclerView Card 새로 고침 후 이미지의 크기가 점점 작아집니다.
public class BeerFragment extends Fragment {
private RecyclerView recyclerView;
ProgressDialog pd;
private View rootView;
private static final String TAG = BeerFragment.class.getSimpleName();
public static boolean isLoadedBeer = false;
public boolean instantiated = false;
public BeerFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
rootView = inflater.inflate(R.layout.activity_main2, container, false);
initViews();
return rootView;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && MaintainState.isLoadedBeer) {
initViews();
} else if (isVisibleToUser && !MaintainState.isLoadedBeer) {
} else if (!isVisibleToUser && MaintainState.isLoadedBeer){
} else if (!isVisibleToUser && !MaintainState.isLoadedBeer){
MaintainState.isLoadedBeer = true;
}
}
private void initViews(){
pd = new ProgressDialog(getActivity());
pd.setMessage("Fetching Beer...");
pd.setCancelable(false);
pd.show();
Log.d("snopww4","noppww");
try {
} catch (Exception e) {
e.printStackTrace();
}
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
ArrayList albumList = new ArrayList<>();
GalleryAdapter adapter = new GalleryAdapter(getActivity(), albumList,2);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, GridSpacingItemDecoration.dpToPx(getActivity(), 10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
if (!isLoadedBeer) {
Log.d("lklk","lklk");
String drink = FilterFragment.sorted;
if (drink.equals("empty")){
loadBeer("beer");
FilterFragment.sorted = "empty";
} else if (drink.equals("lager")){
loadBeer("lager");
FilterFragment.sorted = "empty";
} else if (drink.equals("stout")){
loadBeer("stout");
FilterFragment.sorted = "empty";
} else if (drink.equals("malt")){
loadBeer("malt");
FilterFragment.sorted = "empty";
} else {
pd.hide();
}
} else {
recyclerView.setAdapter(new GalleryAdapter(getActivity(), MaintainState.getItemsBeer(), 1));
recyclerView.smoothScrollToPosition(0);
pd.hide();
}
}
private void loadBeer(final String item){
try{
Client Client = new Client();
Service apiService = Client.getClient().create(Service.class);
Call<GalleryResponse> call = apiService.getBeer(item);
call.enqueue(new Callback<GalleryResponse>() {
@Override
public void onResponse(Call<GalleryResponse> call, Response<GalleryResponse> response) {
List<Gallery> items = response.body().getBeer();
recyclerView.setAdapter(new GalleryAdapter(getActivity(), items, 2));
recyclerView.smoothScrollToPosition(0);
MaintainState.setItemsBeer(items);
isLoadedBeer = true;
pd.hide();
}
@Override
public void onFailure(Call<GalleryResponse> call, Throwable t) {
pd.hide();
}
});
}catch (Exception e){
}
}
}
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {
private Context mContext;
private List<Gallery> albumList;
private int tabPosition;
private LocalStore localStore;
private static final String TAG = GalleryAdapter.class.getSimpleName();
public GalleryAdapter(Context mContext, List<Gallery> albumList, int tabPosition) {
Log.i(TAG, "INIT GalleryAdapter");
this.mContext = mContext;
this.albumList = albumList;
this.tabPosition = tabPosition;
localStore = new LocalStore(mContext);
}
@Override
public GalleryAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.album_card2, viewGroup, false);
CardView cardView = (CardView) view.findViewById(R.id.card_view);
cardView.setMaxCardElevation(GridSpacingItemDecoration.dpToPx(mContext, 10));
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final GalleryAdapter.MyViewHolder viewHolder, int i) {
viewHolder.title.setText(albumList.get(i).getName());
viewHolder.title.setTypeface(Display.displayTypefaceCabin(mContext, 1), Typeface.BOLD);
viewHolder.count.setText("Ksh "+albumList.get(i).getPrice());
viewHolder.count.setTypeface(Display.displayTypefaceWalkWay(mContext, 1), Typeface.BOLD);
viewHolder.tag.setTypeface(Display.displayTypefaceOpenSansCondensed(mContext, 1), Typeface.BOLD);
Glide.with(mContext).
load(albumList.get(i).getThumbnail()).
placeholder(R.drawable.placeholder).
error(R.drawable.placeholder).
fitCenter().
into(viewHolder.thumbnail);
}
@Override
public int getItemCount() {
return albumList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count, tag;
public ImageView thumbnail, favourite;
public MyViewHolder(final View view) {
super(view);
tag = (TextView) view.findViewById(R.id.tag);
title = (TextView) view.findViewById(R.id.title);
count = (TextView) view.findViewById(R.id.count);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
favourite = (ImageView) view.findViewById(R.id.favourite);
thumbnail.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#f2f5f7"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="-10dp"
card_view:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:background="@drawable/image_view_drop_shadow"
android:layout_height="match_parent">
<TextView
android:id="@+id/tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:text="hello"
android:textColor="@color/white"
android:textSize="12dp"/>
<ImageView
android:id="@+id/favourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"/>
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="220dp"
android:clickable="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/thumbnail"
android:gravity="center"
android:textColor="#000000"
android:paddingBottom="5dp"
android:textSize="14sp" />
<TextView
android:id="@+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:gravity="center"
android:paddingBottom="15dp"
android:textSize="14sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
onCreateView는 단편이 처음 호출 될 때 한 번만 호출된다는 것을 알고 있습니다. 프래그먼트를로드하려는 다른 시도는이를 호출하지 않습니다. –