MSe*_*iz5 2 android android-recyclerview
我正在规划一个带有 recyclerviews 的小应用程序。\n有时我只需加载 4 个项目,有时甚至超过 200 个。\n现在我不明白,如何将自定义 recyclerviews 项目居中!
\n\n我有一个自定义的 recyclerview (网格),我希望其中有 4 (2x2) 个项目居中。
\n\n我拥有的:
\n\n\n\n我想要的是:
\n\n\n\n那么我下面的代码错误在哪里呢?
\n\nfragment_single_pokemon.xml
\n\n<?xml version="1.0" encoding="utf-8"?>\n<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n app:layout_behavior="@string/appbar_scrolling_view_behavior"\n tools:context=".MainActivity">\n\n\n <android.support.constraint.ConstraintLayout\n android:id="@+id/cLayTypOne"\n android:layout_width="50dp"\n android:layout_height="25dp"\n android:layout_marginBottom="8dp"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:background="@drawable/customborder_typ"\n app:layout_constraintBottom_toTopOf="@+id/cLayTypTwo"\n app:layout_constraintEnd_toStartOf="@+id/civSprite"\n app:layout_constraintStart_toStartOf="parent">\n\n <TextView\n android:id="@+id/txtTypOne"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:text="TextView"\n android:textColor="#FFFFFF"\n android:textSize="8sp"\n android:textStyle="bold"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent" />\n </android.support.constraint.ConstraintLayout>\n\n <View\n android:id="@+id/vLine2"\n android:layout_width="50dp"\n android:layout_height="1dp"\n android:layout_marginBottom="8dp"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:layout_marginTop="8dp"\n android:background="#000000"\n app:layout_constraintBottom_toBottomOf="@+id/civSprite"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toEndOf="@+id/civSprite"\n app:layout_constraintTop_toTopOf="@+id/civSprite">\n\n </View>\n\n <TextView\n android:id="@+id/txtPokemonName"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:layout_marginTop="8dp"\n android:text="Pok\xc3\xa9ball"\n android:textAlignment="center"\n android:textSize="24sp"\n android:textStyle="bold"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent" />\n\n <de.hdodenhof.circleimageview.CircleImageView\n android:id="@+id/civSprite"\n android:layout_width="148dp"\n android:layout_height="148dp"\n android:layout_alignParentTop="true"\n android:layout_centerHorizontal="true"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:layout_marginTop="16dp"\n android:src="@mipmap/ic_launcher"\n app:civ_border_width="2dp"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toBottomOf="@+id/txtPokemonName" />\n\n <View\n android:id="@+id/vLine"\n android:layout_width="50dp"\n android:layout_height="1dp"\n android:layout_marginBottom="8dp"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:layout_marginTop="8dp"\n android:background="#000000"\n app:layout_constraintBottom_toBottomOf="@+id/civSprite"\n app:layout_constraintEnd_toStartOf="@+id/civSprite"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="@+id/civSprite">\n\n </View>\n\n <ImageView\n android:id="@+id/imgIsRaidBoss"\n android:layout_width="50dp"\n android:layout_height="50dp"\n android:layout_marginBottom="8dp"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n app:layout_constraintBottom_toTopOf="@+id/vLine2"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toEndOf="@+id/civSprite"\n app:srcCompat="@drawable/raid_sprite" />\n\n <ImageView\n android:id="@+id/imgShinyExists"\n android:layout_width="50dp"\n android:layout_height="50dp"\n android:layout_marginBottom="4dp"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n app:layout_constraintBottom_toTopOf="@+id/imgIsRaidBoss"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toEndOf="@+id/civSprite"\n app:srcCompat="@drawable/ic_shiny" />\n\n <android.support.constraint.ConstraintLayout\n android:id="@+id/cLayTypTwo"\n android:layout_width="50dp"\n android:layout_height="25dp"\n android:layout_marginBottom="8dp"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:background="@drawable/customborder_typ"\n app:layout_constraintBottom_toTopOf="@+id/vLine"\n app:layout_constraintEnd_toStartOf="@+id/civSprite"\n app:layout_constraintStart_toStartOf="parent">\n\n <TextView\n android:id="@+id/txtTypTwo"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:text="TextView"\n android:textColor="#FFFFFF"\n android:textSize="8sp"\n android:textStyle="bold"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent" />\n </android.support.constraint.ConstraintLayout>\n\n <Button\n android:id="@+id/btnPokeDexEnty"\n android:layout_width="match_parent"\n android:layout_height="wrap_content"\n android:layout_marginEnd="48dp"\n android:layout_marginStart="48dp"\n android:layout_marginTop="16dp"\n android:background="@drawable/boarder_shape"\n android:text="Pok\xc3\xa9dex Eintrag"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toBottomOf="@+id/civSprite" />\n\n <TextView\n android:id="@+id/txtPokeDexEntry"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:layout_marginTop="8dp"\n android:text="TextView"\n android:textAlignment="center"\n android:visibility="gone"\n app:layout_constraintEnd_toEndOf="@+id/btnPokeDexEnty"\n app:layout_constraintStart_toStartOf="@+id/btnPokeDexEnty"\n app:layout_constraintTop_toBottomOf="@+id/btnPokeDexEnty" />\n\n <android.support.v7.widget.RecyclerView\n android:id="@+id/rViewPokeInfo"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:layout_gravity="center_vertical"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:layout_marginTop="16dp"\n android:foregroundGravity="center_vertical"\n android:orientation="vertical"\n android:textAlignment="center"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toBottomOf="@+id/txtPokeDexEntry" />\n\n\n</android.support.constraint.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\n\ngridview_pokedex_pokeinfo.xml
\n\n<?xml version="1.0" encoding="utf-8"?>\n\n<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n android:id="@+id/rLayMainContainer"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:layout_marginBottom="8dp"\n android:layout_marginEnd="8dp"\n android:layout_marginStart="8dp"\n android:layout_marginTop="8dp"\n android:gravity="center"\n android:orientation="vertical">\n\n <TextView\n android:id="@+id/txtPokeInfoTitle"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:layout_alignParentTop="true"\n android:layout_centerHorizontal="true"\n android:text="TextView"\n android:textStyle="bold" />\n\n <TextView\n android:id="@+id/txtPokeInfoText"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:layout_below="@+id/txtPokeInfoTitle"\n android:layout_centerHorizontal="true"\n android:text="TextView" />\n</RelativeLayout>\nRun Code Online (Sandbox Code Playgroud)\n\n回收者查看代码
\n\nRecyclerView recyclerViewPokeInfo = (RecyclerView)rLayout_mainFragment.findViewById(R.id.rViewPokeInfo);\nRecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),2);\nrecyclerViewPokeInfo.setLayoutManager(layoutManager);\n\nCustomRecyclerAdapterPokeInfoGrid adapter = new CustomRecyclerAdapterPokeInfoGrid(getActivity(), db.getPokeInfo(intPokemonID));\nrecyclerViewPokeInfo.setAdapter(adapter);\nRun Code Online (Sandbox Code Playgroud)\n\n回收者视图适配器
\n\npackage com.spicysoftware.goexpert;\n\nimport android.content.Context;\nimport android.support.constraint.ConstraintLayout;\nimport android.support.v7.widget.RecyclerView;\nimport android.util.Log;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\nimport android.widget.TextView;\n\nimport java.util.ArrayList;\n\npublic class CustomRecyclerAdapterPokeInfoGrid extends RecyclerView.Adapter<CustomRecyclerAdapterPokeInfoGrid.ViewHolder> {\n private final ArrayList<String> strPokeInfo;\n private Context context;\n MySQLiteHelper db;\n int locationSpriteNo;\n String strCatchRate = "";\n String strEscapeRate = "";\n String strInEggs = "";\n String strKmAsFriend = "";\n\n public CustomRecyclerAdapterPokeInfoGrid(Context context, ArrayList<String> strPokeInfo) {\n this.strPokeInfo = strPokeInfo;\n this.context = context;\n }\n\n\n @Override\n public CustomRecyclerAdapterPokeInfoGrid.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {\n View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.gridview_pokedex_pokeinfo, viewGroup, false);\n\n return new ViewHolder(view);\n }\n\n @Override\n public void onBindViewHolder(final CustomRecyclerAdapterPokeInfoGrid.ViewHolder viewHolder, final int i) {\n\n\n viewHolder.setClickListener(new ItemClickListener() {\n @Override\n public void onClick(View view, int position, boolean isLongClick) {\n\n }\n });\n\n // load image\n try {\n db = new MySQLiteHelper(context);\n\n String currentString = strPokeInfo.get(i);\n\n if(i == 0){\n viewHolder.txtPokeInfoTitle.setText("Fangchance");\n }else if(i == 1){\n viewHolder.txtPokeInfoTitle.setText("Fluchtchance");\n }else if(i == 2){\n viewHolder.txtPokeInfoTitle.setText("In Eiern");\n }else if(i == 3){\n viewHolder.txtPokeInfoTitle.setText("Distanz als Kumpel");\n }\n\n viewHolder.txtPokeInfoText.setText(currentString);\n\n\n }\n catch(Exception ex) {\n Log.v("Error: ", "-> "+ex);\n }\n\n }\n\n @Override\n public int getItemCount() {\n return strPokeInfo.size();\n }\n\n public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{\n private TextView txtPokeInfoTitle,txtPokeInfoText;\n private ItemClickListener clickListener;\n private ConstraintLayout cLayoutSingleItem;\n\n public ViewHolder(View view) {\n super(view);\n\n txtPokeInfoTitle = (TextView)view.findViewById(R.id.txtPokeInfoTitle);\n txtPokeInfoText = (TextView)view.findViewById(R.id.txtPokeInfoText);\n\n\n cLayoutSingleItem = (ConstraintLayout)view.findViewById(R.id.cLayoutSingleItem);\n itemView.setOnClickListener(this);\n itemView.setOnLongClickListener(this);\n\n }\n\n public void setClickListener(ItemClickListener itemClickListener) {\n this.clickListener = itemClickListener;\n }\n\n @Override\n public void onClick(View view) {\n clickListener.onClick(view, getPosition(), false);\n }\n\n @Override\n public boolean onLongClick(View view) {\n clickListener.onClick(view, getPosition(), true);\n return true;\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
小智 6
将列表项布局设置为 match_parent,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rLayMainContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/txtPokeInfoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textStyle="bold" />
<TextView
android:id="@+id/txtPokeInfoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPokeInfoTitle"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9398 次 |
| 最近记录: |