快速说明:我对Java或android Studio的经验不是很丰富,所以请提出我的笨拙问题。
我正在研究一个带有RAL颜色片段的项目。总共只有280张图像。我正在尝试为此片段创建一个recyclerView。为了避免这个问题比现在长得多,我在情节中剪掉了一些颜色。
我希望颜色的布局看起来像这样:
image1 image2 image3
image4 image5 image6
...
image280 image281
(The last line only has 2 images.)
我看过一些教程(它们中的任何一个都没有与rv一起工作),并试图将它们所做的工作实施到我的项目中,但是出现以下错误:
java.lang.IllegalStateException: RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView{ab3c28d VFED.... ......I. 0,0-0,0 #7f0800a2 app:id/recyclerView}, adapter:null, layout:null, context:com.westcoastpowdercoating.WestCoastPowderCoating.MainActivity@3b25cc8b
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:4304)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:808)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.westcoastpowdercoating.WestCoastPowderCoating.ralfragment.onCreateView(ralfragment.java:23)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
从此错误中推测,layoutmanager或recyclerviewer未连接。我不确定哪里出了问题。我需要在MainActivity.java中做些什么吗?在我观看的教程中,他们在我的ralfragment.java中的MainActivity.java中完成了大部分工作,但是我认为我必须将其移至我的ralfragment.java中,因为这是我需要recycvlerView的片段。 / p>
这是我的ralfragment.java:
public class ralfragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ral, container, false);
ArrayList<ralcolor> ralcolors= new ArrayList<>();
ralcolors.add(new ralcolor(R.drawable.ral1000, R.drawable.ral1001, R.drawable.ral1002));
ralcolors.add(new ralcolor(R.drawable.ral1003, R.drawable.ral1004, R.drawable.ral1005));
ralcolors.add(new ralcolor(R.drawable.ral1006, R.drawable.ral1007, R.drawable.ral1011));
ralcolors.add(new ralcolor(R.drawable.ral1012, R.drawable.ral1013, R.drawable.ral1014));
ralcolors.add(new ralcolor(R.drawable.ral1015, R.drawable.ral1016, R.drawable.ral1017));
ralcolors.add(new ralcolor(R.drawable.ral1018, R.drawable.ral1019, R.drawable.ral1020));
mRecyclerView = view.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new Adapter(ralcolors);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
我的适配器:
public class Adapter extends RecyclerView.Adapter<Adapter.viewHolder> {
private ArrayList<ralcolor> mralcolors;
public static class viewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView1;
public ImageView mImageView2;
public ImageView mImageView3;
public viewHolder(View itemView) {
super(itemView);
mImageView1 = itemView.findViewById(R.id.imageView1000);
mImageView2 = itemView.findViewById(R.id.imageView1001);
mImageView3 = itemView.findViewById(R.id.imageView1002);
}
}
public Adapter(ArrayList<ralcolor> ralcolors) {
mralcolors = ralcolors;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_ral, viewGroup, false);
viewHolder vh = new viewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(@NonNull viewHolder viewHolder, int i) {
ralcolor currentcolor = mralcolors.get(i);
viewHolder.mImageView1.setImageResource(currentcolor.getmImageResource1());
viewHolder.mImageView2.setImageResource(currentcolor.getmImageResource2());
viewHolder.mImageView3.setImageResource(currentcolor.getmImageResource3());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return mralcolors.size();
}
}
我的RAL xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<LinearLayout
android:layout_gravity="center_horizontal"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">
<ImageView
android:id="@+id/imageView1000"
android:layout_height="100dp"
android:layout_width="100dp" />
<ImageView
android:id="@+id/imageView1001"
android:layout_height="100dp"
android:layout_width="100dp" />
<ImageView
android:id="@+id/imageView1002"
android:layout_height="100dp"
android:layout_width="100dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
我的RAL颜色检索器Java:
package com.westcoastpowdercoating.WestCoastPowderCoating;
public class ralcolor {
private int mImageResource1;
private int mImageResource2;
private int mImageResource3;
public ralcolor(int imageResource1, int imageResource2, int
imageResource3) {
mImageResource1 = imageResource1;
mImageResource2 = imageResource2;
mImageResource3 = imageResource3;
}
public int getmImageResource1() {
return mImageResource1;
}
public int getmImageResource2() {
return mImageResource2;
}
public int getmImageResource3() {
return mImageResource3;
}
}
感谢您的帮助。
答案 0 :(得分:1)
您不能将孩子放在XML的RecyclerView中。看起来您当前拥有的是ViewHolder中想要的东西。那不是怎么做。
制作一个新的布局XML文件,并将包装在RecyclerView中的内容移动到该文件中。
然后更改inflate()
中的onCreateViewHolder()
参数以使用正确的布局(更改R.layout.fragment_ral
)。
答案 1 :(得分:-1)
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
manager.setOrientation(LinearLayoutManager.VERTICAL);
myRecycler.setLayoutManager(manager);