我正在尝试使用带有标题的TextViews列表(在RecyclerView中)编写一个简单的应用程序。滚动后标题必须调整大小。如果我srcoll列表,我需要将标题的文本大小设置为更大。
首先我想我会使用带有TextView(标题)和RecyclerView(列表)的LinearLayout。但是当我在我的recyclerview上设置onScrollListener并在我的标题(textview)上使用setTextSize(滚动)后,所有内容都“跳”,因为当标题中的文字太大并且使用了第二行和更多行时,标题和列表的高度发生了变化。
所以我发明了我只会制作物品的第一元素的动态改变尺寸的RecyclerView并且它(大多数)工作但我需要在屏幕的第一个位置滚动之后使其“粘性”离开它。
有谁知道如何使用标题制作recyclelerview? 这是我的第二个想法代码: MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new ItemRecycleAdapter());
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
private int MAX_FONT = 30;
private int MIN_FONT = 15;
private int MAX_HEIGHT = 150;
int currentLineCount = 1;
int totalOffset = 0;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalOffset += dy;
View child = recyclerView.getChildAt(0);
TextView textView = (TextView) child.findViewById(R.id.headerText);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, offsetToFontSize(totalOffset));
}
public int offsetToFontSize(int offset) {
if (offset > MAX_HEIGHT)
return MIN_FONT;
float step = ((float) (MAX_FONT - MIN_FONT)) / MAX_HEIGHT;
return (int) (MIN_FONT + ((float) (MAX_HEIGHT - offset)) * step);
}
});
}
RecyclerAdapter:
public static final List<Item> Items = new ArrayList<Item>();
private int MAX_FONT = 30;
private int MIN_FONT = 15;
private int MAX_HEIGHT = 150;
static {
Items.add(new Item("DUŻY TYTUL n you attach it to the view by calling n you attach it to the view by calling", "", ""));
Items.add(new Item("", "n you attach it to the view by calling n you attach it to the view by calling ", "Lorem ipsum dolar"));
Items.add(new Item("", " by calling n you att by calling n you attach it to the ach it to the ", "Lorem ipsum dolar"));
Items.add(new Item("", "Long text test", "Lorem ipsum dolar"));
Items.add(new Item("", "Long text testLong text test", "Lorem ipsum dolar"));
Items.add(new Item("", "Long text test", "Lorem ipsum dolar"));
Items.add(new Item("", "Long text test", "Lorem ipsum dolar"));
Items.add(new Item("", "Long text test", "Lorem ipsum dolar"));
Items.add(new Item("", "Long text test", "Lorem ipsum dolar"));
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_element, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
Item item = Items.get(position);
if (position > 0){
holder.header.setVisibility(View.GONE);
}
holder.header.setText((item.mHeader));
holder.text1.setText(item.item1);
holder.text2.setText(item.item2);
}
@Override
public int getItemCount() {
return Items.size();
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
Item item = Items.get(position);
if (position > 0){
holder.header.setVisibility(View.GONE);
}
holder.header.setText((item.mHeader));
holder.text1.setText(item.item1);
holder.text2.setText(item.item2);
}
public void cos(ItemViewHolder holder, int totalOffset){
holder.header.setTextSize(TypedValue.COMPLEX_UNIT_DIP, offsetToFontSize(totalOffset));
}
public int offsetToFontSize(int offset) {
if (offset > MAX_HEIGHT)
return MIN_FONT;
float step = ((float) (MAX_FONT - MIN_FONT)) / MAX_HEIGHT;
return (int) (MIN_FONT + ((float) (MAX_HEIGHT - offset)) * step);
}
@Override
public int getItemCount() {
return Items.size();
}