我目前正以初学者的身份学习Android Studio,因此,如果我提出一个问题,对专业人士来说它们似乎很容易,请原谅我。将StickyHeader添加到recyclerView的正确方法是什么?我正在尝试开发一个示例模板,该模板将具有约25行,但应将这些项目分组在(5)个不同的stickyHeaders下。这就是问题所在。没有明确的方向说明如何完成此操作,并且有些混乱。
当前,共有(5)个与该代码关联的文件,它们是:(1)Adapter.java,(2)Item.java(3)MainActivity.java,(4)activity_main.xml,最后是(5) item.xml。我不确定如何完成stickyHeader编码。
(1)这是Adapter.java文件:
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
(2)这是item.java文件:
public class ExampleItem {
private int mImageResource;
private String mText1;
private String mText2;
public ExampleItem(int imageResource, String text1, String text2) {
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getText2() {
return mText2;
}
}
(3)这是MainActivity.java文件:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 1", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 2", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 3", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 4", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 5", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 6", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 7", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 8", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 9", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 10", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 11", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 12", "SubTitle"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
我想在几个主题之间添加一个stickyHeader,但是不知道如何实现stickyHeader代码。每个人都在说这非常令人困惑。