[列表布局我对使用recyclerview不熟悉,我已经看到了一些问题而没有人真正回答我的问题。我有2个布局,一个是带有文本和个人资料头像的自定义列表视图,文本和图像嵌套在一个与宽度匹配的cardview中。另一个是相同的内容(具有相同的ID)但这里的图像和文本被设置为包装内容,以便它们以网格格式出现(如其mainActivity的flash share / xender实现)。 我真正想要做的是在运行时使用:
在这两个布局之间切换RecyclerView.setLayoutManager(new LinearLayout(this);
表示第一个布局,并且:
RecyclerView.setLayoutManager(new GridLayoutManager(ClassList.this, 3));
用于第二个布局,其宽度设置为包装内容,以便在渲染时看起来更好。 这是列表布局1:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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:layout_height="100dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="5dp"
android:padding="5dp"
android:layout_margin="5dp"
android:foreground="?attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:layout_margin="2dp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/student_avatar"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/nothing"
app:border_color="#FF000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/student_name_edit_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chinedum Ofomata Agu"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/student_reg_number_edit_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reg: 2014544402"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
第二个(网格)布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="160dp"
android:padding="5dp"
android:layout_margin="5dp"
android:gravity="center"
android:background="?android:attr/selectableItemBackground"
android:scrollbarStyle="insideInset"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/student_avatar"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/nothing"
app:border_color="#FF000000" />
<TextView
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Chinedum Agu"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/student_reg_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center"
android:text="Reg: 2014544402"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
我实现了onClickListener我的mainActivity,所以我控制了我的主要活动中的Recycler View的点击响应,而不是我的适配器类。我发现的大多数问题和答案都是关于如何像StaggeredGridView一样实现多个视图,但我想要的是一种方法来膨胀两个布局并在运行时切换它们。
继承我的适配器类(还没有任何数据集):
public class ClassListAdapter extends RecyclerView.Adapter<ClassListAdapter.Holder> {
private static final String TAG = ClassListAdapter1.class.getSimpleName();
private static clickListener mClickListener;
View row;
public ClassListAdapter(clickListener listener) {
mClickListener = listener;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
row = LayoutInflater.from(parent.getContext()).inflate(R.layout.class_list_listview_cardview, parent, false);
return new Holder(row);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
}
@Override
public int getItemCount() {
return 10;
}
public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView student_avatar;
private TextView student_name, reg_number;
View mview;
public Holder(View itemView) {
super(itemView);
mview = itemView;
student_avatar = (ImageView) itemView.findViewById(R.id.student_avatar);
student_name = (TextView) itemView.findViewById(R.id.student_name);
reg_number = (TextView) itemView.findViewById(R.id.student_reg_number);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mClickListener.onItemClick(getLayoutPosition());
}
}
public interface clickListener {
void onItemClick(int position);
// void onItemLongClick(int position);
}
}
并且继承了我的mainActivity代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.class_list_recyclerview);
toolbar = (Toolbar) findViewById(R.id.default_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
classListAdapter = new ClassListAdapter(ClassList.this);
configViews();
}
@Override
public void onItemClick(int position) {
Toast.makeText(ClassList.this, "You clicked item" + " " + position, Toast.LENGTH_SHORT).show();
intent = new Intent(ClassList.this, Profile.class);
startActivity(intent);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
}
private void configViews() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(ClassList.this, LinearLayoutManager.VERTICAL, false));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
mRecyclerView.setAdapter(classListAdapter);
}
我不希望一次显示两个布局,而是根据用户的选择显示某个布局。 请任何建议将不胜感激。 感谢。
答案 0 :(得分:1)
最初isList值为true
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mGridLayourManager = new GridLayoutManager(getActivity(),3);
setLayoutManager();
private void setLayoutManager() {
if (isList) {
mRVList.setLayoutManager(mLinearLayoutManager);
}else{
mRVList.setLayoutManager(mGridLayourManager);
}
}
//按列表切换按钮
isList=!isList;
setLayoutManager();
mAdapter.setList(isList);
mRVList.setAdapter(mAdapter);
适配器类更改
@Override
public AdapterRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(isList ? R.layout.row_layout_list : R.layout.row_layout_grid, null);
AdapterRowHolder holder = new AdapterRowHolder(v);
return holder;
}
答案 1 :(得分:0)
您应该一直使用GridLayout
,并为要添加的每个项目使用setSpanSizeLookup
。
在您的情况下,我会将第一项的跨度计数设为2,后面的项目为1
RecyclerView.setLayoutManager(new GridLayoutManager(ClassList.this, 2));
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position == 0) {
return 2;
} else {
return 1;
}
}
});