我正在关注[this] [1]教程,我已经成功创建了专辑,我现在想做的就是4张专辑后的例子。我想添加一些空格,所以我可以在这里添加一个TextField我的问题是:
这是我尝试过的。示例代码有一个准备相册的方法,我已将方法分为两个方法,如下所示:
private void prepareAlbums() {
int[] covers = new int[]{
R.drawable.album1,
R.drawable.album2,
R.drawable.album3,
R.drawable.album4,
R.drawable.album5,
R.drawable.album6,
R.drawable.album7,
R.drawable.album8,
R.drawable.album9,
R.drawable.album10,
R.drawable.album11};
Album a = new Album("True Romance", 13, covers[0]);
albumList.add(a);
a = new Album("Xscpae", 8, covers[1]);
albumList.add(a);
a = new Album("Maroon 5", 11, covers[2]);
albumList.add(a);
a = new Album("Born to Die", 12, covers[3]);
albumList.add(a);
rootView.setPadding(0,100,0,0);
adapter.notifyDataSetChanged();
}
和...
private void prepareAlbums2(){
int[] covers = new int[]{
R.drawable.album1,
R.drawable.album2,
R.drawable.album3,
R.drawable.album4,
R.drawable.album5,
R.drawable.album6,
R.drawable.album7,
R.drawable.album8,
R.drawable.album9,
R.drawable.album10,
R.drawable.album11};
Album a = new Album("Honeymoon", 14, covers[4]);
albumList.add(a);
a = new Album("I Need a Doctor", 1, covers[5]);
albumList.add(a);
a = new Album("Loud", 11, covers[6]);
albumList.add(a);
a = new Album("Legend", 14, covers[7]);
albumList.add(a);
a = new Album("Hello", 11, covers[8]);
albumList.add(a);
a = new Album("Greatest Hits", 17, covers[9]);
albumList.add(a);
adapter.notifyDataSetChanged();
}
}
然后在我的onCreate方法中,我做了以下内容:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
prepareAlbums();
rootView.setPadding(0,100,0,0);
prepareAlbums2();
...
return rootView;
}
然而,当我这样做时似乎没有任何事情发生,我也不知道如何在创建间距后在该位置添加文本,我们非常感谢任何帮助。
如果需要任何其他信息,请询问并更新此帖子。
答案 0 :(得分:0)
修改GridSpacingItemDecoration
类,在位于X和X + 1的两张牌的底部添加额外的空格。
<强>更新强>
鉴于你想要一个文本视图,结果并不太难。 There is a great example here
解决方案涉及创建回调以根据位置动态确定列数。您必须修改跨度大小,以便在您到达第4个位置时返回一个。
GridLayoutManager manager = new GridLayoutManager(this, 3);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (3 - position % 3);
}
});
recyclerView.setLayoutManager(manager);
您还需要在适配器中创建一个案例来呈现TextView而不是普通的卡片视图。
更新8/19/2016添加了详细信息:
在MainActivity中,添加一个回调以根据位置返回跨度大小。我们将通过向数据集添加虚拟值来确定位置。
((GridLayoutManager)mLayoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if ( albumList.get(position).getName().length() == 0)
return 2;
return 1;
}
});
添加数据时,在您希望文本移动的位置添加虚拟值。理想情况下,您可以更改类或向类添加属性。为简单起见,没有名字的专辑将是文本行。
a = new Album("Born to Die", 12, covers[3]);
albumList.add(a);
albumList.add(new Album("",0, 0));
a = new Album("Honeymoon", 14, covers[4]);
albumList.add(a);
最后,在适配器代码中,覆盖getItemViewType()
方法,以便RecyclerView可以支持多种视图类型。
// Change the extending class type
public class AlbumsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// Add this
public class TextViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public TextViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text3);
}
}
// Add this
@Override
public int getItemViewType(int position) {
if ( albumList.get(position).getName().length() == 0)
return 1;
return 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = null;
switch ( viewType ) {
case 0:
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.album_card, parent, false);
return new MyViewHolder(itemView);
case 1:
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.text_layout, parent, false);
return new TextViewHolder(itemView);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Album album = albumList.get(position);
if ( album.getName().length() == 0) {
TextViewHolder text = (TextViewHolder)holder;
text.textView.setText("New Text");
} else {
final MyViewHolder myHolder = (MyViewHolder)holder;
myHolder.title.setText(album.getName());
myHolder.count.setText(album.getNumOfSongs() + " songs");
// loading album cover using Glide library
Glide.with(mContext).load(album.getThumbnail()).into(myHolder.thumbnail);
myHolder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(myHolder.overflow);
}
});
}
}
}
您还需要TextView行的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text3"/>
</LinearLayout>