1
중첩 된 RecyclerView
을 사용하려고하면 메모리에 약간의 문제가 있습니다.보기를 리사이클링하지 않아서 메모리가 부족하기 때문에 충돌이 발생합니다. 그래서 나는 다른 해결책을 찾아야한다. 나는이 같은 어댑터를 받아 내 자신의 레이아웃을 만들었습니다Recyclerview를 LinearLayout으로 바꾸십시오.
public class CustomTileLayout extends LinearLayout {
private Adapter mAdapter;
private Observer mObserver = new Observer();
public CustomTileLayout(Context context) {
super(context);
}
public CustomTileLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Adapter getAdapter() {
return mAdapter;
}
public void setAdapter(Adapter mAdapter) {
if (mAdapter != null)
mAdapter.unregisterDataSetObserver(mObserver);
this.mAdapter = mAdapter;
mAdapter.registerDataSetObserver(mObserver);
mObserver.onChanged();
}
private class Observer extends DataSetObserver {
@Override
public void onChanged() {
final List<View> oldViews = new ArrayList<View>(getChildCount());
for (int i = 0; i < getChildCount(); i++)
oldViews.add(getChildAt(i));
final Iterator<View> iter = oldViews.iterator();
removeAllViews();
for (int i = 0; i < mAdapter.getCount(); i++) {
View convertView = iter.hasNext() ? iter.next() : null;
View newView = mAdapter.getView(i, convertView, CustomTileLayout.this);
addView(newView);
}
super.onChanged();
}
@Override
public void onInvalidated() {
removeAllViews();
super.onInvalidated();
}
}
}
을 내가 사용하는 동안이 같은 :
tileHolder = (GameCategoriesTileViewHolder) holder;
section = (Section) items.get(position);
tileHolder.customTileLayout.setAdapter(new GameTileAdapter(section.getGames(), context, R.layout.row_small_tile));
모두가 지금까지 잘 작동하지만 문제가있다. 문제는 다음과 같습니다 있다는 것입니다 : 내가처럼 될 수있는 아이의 레이아웃을 지정하는 경우
:
내가이 추천 타일을 할 각 행에 원하는 동안 결과는 동일한 항목이므로 첫 번째 타일 만 채우고 다음 2는 비어 있습니다.
내 어댑터 : 내가 결과를 얻을 수있는 방법의
public class GameTileAdapter implements ListAdapter {
private List<Game> games;
private Context context;
private int resLayout = R.layout.row_small_tile;
private boolean isFavoriteFragmentView = false;
private PopupMenu popupMenu;
public GameTileAdapter(List<Game> games, Context context, int resLayout) {
this.games = games;
this.context = context;
this.resLayout = resLayout;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getCount() {
return games.size();
}
@Override
public Object getItem(int position) {
return games.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
SingleItemViewHolder holder;
if (itemView == null){
LayoutInflater vi;
vi = LayoutInflater.from(context);
itemView = vi.inflate(resLayout, parent, false);
holder = new SingleItemViewHolder(itemView);
holder.game_name.setText(games.get(position).getName());
if (games.get(position).getImageUri().isEmpty()){
holder.game_imageuri.setImageResource(R.drawable.ic_image_placeholder);
}else {
Picasso.with(context).load(games.get(position).getImageUri()).into(holder.game_imageuri);
}
holder.itemLineColor.setBackgroundColor(GameManager.getmInstance(context).getGameColor(games.get(position)));
}else {
holder = (SingleItemViewHolder)itemView.getTag();
}
return itemView;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEnabled(int position) {
return true;
}
private static class SingleItemViewHolder{
CardView cv;
TextView game_name;
TextView game_is_new;
TextView game_jackpot;
ImageView game_imageuri;
View itemLineColor;
Button favorite_button;
Button menu_button;
SingleItemViewHolder(View itemView) {
cv = (CardView)itemView.findViewById(R.id.root_layout);
game_name = (TextView)itemView.findViewById(R.id.game_name_text);
game_is_new = (TextView)itemView.findViewById(R.id.game_new_ribbon);
game_jackpot = (TextView)itemView.findViewById(R.id.game_jackpot_ribbon);
game_imageuri = (ImageView)itemView.findViewById(R.id.game_imageuri);
itemLineColor = (View)itemView.findViewById(R.id.single_line_color);
favorite_button = (Button)itemView.findViewById(R.id.favButton);
menu_button = (Button)itemView.findViewById(R.id.menu_button_tile);
}
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return games.size();
}
@Override
public boolean isEmpty() {
return games.isEmpty();
}
}
어떤 아이디어?
나는이 문제를 이해하지 못한다고 생각합니다. 내 문제는 뷰를 추가하는 방법이 아니라 프로그래밍 방식으로 3 열의 선형 레이아웃을 분리하는 방법입니다 –
3 개의 항목을 연속으로 추가하려면 3 개의 요소가 포함 된 선형 레이아웃을 만들고 가로 레이아웃을 분리하려면 뷰를 추가 할 수 있습니다 레이아웃의 하단에 –
선형 레이아웃에서 가로 방향을 추가합니다. 그러나 부분이 크면 가로 스크롤 뷰 –