如何使用recyclerview实现滚动功能。找到下面的代码片段。
quiztab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/quiz_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</RelativeLayout>
quiztab_mcq.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<LinearLayout
android:id="@+id/mcq"
android:orientation="vertical"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submitButton"
android:textSize="18sp"
android:textColor="@color/colorWhite"
android:textAllCaps="false"
android:background="@color/colorPrimary"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
QuizTab.java
@SuppressLint("ValidFragment")
public class QuizTab extends Fragment {
private static final String TAG = "QuizTab";
RecyclerView recyclerView;
View view;
LinearLayout linearLayout;
List<Integer> usr_opts;
RadioGroup rg;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.quiztab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
getQuizContent();
setupRecyclerView(recyclerView);
return view;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(),
VersionModel.data));
}
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private String[] mValues;
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public final LinearLayout mLinearlayout;
public ViewHolder(View view) {
super(view);
mLinearlayout = (LinearLayout) view.findViewById(R.id.mcq);
}
}
public String getValueAt(int position) {
return mValues[position];
}
public SimpleStringRecyclerViewAdapter(Context context, String[] items) {
mContext = context;
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quiztab_mcq, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mLinearlayout.addView(rg);
}
}
@Override
public int getItemCount() {
return 1;
}
}
}
上面我试图将quiztab_mcq.xml扩展到quiztab.xml,但页面上的内容不可滚动。我的提交按钮在较小尺寸的手机上不可见。它适用于5.5英寸手机,但尺寸不小。
答案 0 :(得分:2)
尝试更改
@Override
public int getItemCount() {
return 1;
}
到
@Override
public int getItemCount() {
return mValues.length;
}
更新:正如NileshRathod在下面的评论中提到的那样,LinearLayout
中android:layout_height="wrap_content"
根<{1}}的高度应该是android:layout_height="match_parent"
而不是quiztab_mcq.xml
。
此外,关于重复:您将拥有它多次,因为您的mValues中有许多项目,因为您的RecyclerView正在创建其长度大小的列表。
更新:如果要显示内容,则无需在回收站视图中显示。 RecyclerView是列表项的容器,但如果您的内容高度超过屏幕高度,则可以使用简单的ScrollView或NestedScrollView。然后你的内容不会重复,并且可以滚动到它的结尾。